Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Gotten easy code by proffessor that I don't understand, do you?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Gotten easy code by proffessor that I don't understand, do you?

    Hi everyone,

    I'm very, very new to java, but since i'm a girl studying a master in technologies I've been assigned to create several computer codes to solve optimization problems in a course of applied optimization. My problem is, I don't even understand some of the code given to us... :/ Can you help me? The only help I have gotten is that I have to use the functions readGraph, readLine, readInteger and readString. Can you comment it and tell me what the functions do? The meaning of them is to read in a example file of the format below. This represents a network directed graph. The first row (8) is the number of nodes. The second row and down represents bows/arcs between nodes. Eg, from node 1 goes an arc to node 3. Thank you so very much, would really appreciate it!

    8
    1 3
    1 4
    1 7
    1 8
    3 2
    3 7
    4 3
    5 1
    5 4
    6 1
    6 5
    7 2
    7 8
    8 6



    private static MyDigraph readGraph(String fileName)
      {
        MyDigraph G = null;
     
        int lineNum = 0;
        String line = null;
        StringTokenizer temp;
        BufferedReader inFile = null;
     
        try {
          inFile = new BufferedReader(new FileReader(fileName));
        }
        catch (FileNotFoundException e) {
          System.out.println("?? file not found");
          return null;
        }
     
        if ((line = readLine(inFile)) == null) {
          System.out.println("?? file is empty");
          return null;
        }
        else {
          lineNum++;
          temp = new StringTokenizer(line);
          try {
            if (temp.countTokens() != 1) {
              throw new IllegalArgumentException();
            }
            int n = Integer.parseInt(temp.nextToken());
            G = new MyDigraph(n);
          }
          catch (IllegalArgumentException e) {
            System.out.println("?? format error on line #" + lineNum);
            return null;
          }
        }
     
        while ((line = readLine(inFile)) != null) {
          lineNum++;
          temp = new StringTokenizer(line);
          try {
            if (temp.countTokens() != 2) {
              throw new IllegalArgumentException();
            }
            int u = Integer.parseInt(temp.nextToken());
            int v = Integer.parseInt(temp.nextToken());
            G.insertEdge(u, v);
          }
          catch (IllegalArgumentException e) {
            System.out.println("?? format error on line #" + lineNum);
          }
        }
        return G;
      }
     
      private static int readInteger(String prompt)
      {
        int temp = 0;
     
        while (true) {
          try {
            temp = Integer.parseInt(readString(prompt));
            break;
          }
          catch (NumberFormatException e) {
            System.out.println("?? format error");
          }
        }
        return temp;
      }
     
      private static String readString(String prompt)
      {
        StringTokenizer temp = null;
        BufferedReader stdin = new BufferedReader
    			  (new InputStreamReader(System.in));
     
        while (true) {
          System.out.print(prompt);
          System.out.flush();
          temp = new StringTokenizer(readLine(stdin));
          if (temp.countTokens() == 1) break;
          System.out.println("?? format error");
        }
        return temp.nextToken();
      }
     
      private static String readLine(BufferedReader inFile)
      {
        String line = null;
     
        try {
          line = inFile.readLine();
        }
        catch (IOException e) {
          System.err.println("?? beats me ...");
          System.exit(0);
        }
        return line;
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    That isn't really how this works. How about you tell us what the functions do and we'll tell you how close you are. Have you stepped through this with a debugger? Have you stepped through it with a piece of paper and a pencil, manually tracing what it's doing? If not, those are your first steps.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    matitorn (October 8th, 2012)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    Why it took such mental gymnastics to do what shouldn't need to be this complicated, though, I don't know.
    Last edited by jps; October 9th, 2012 at 10:41 AM. Reason: Spoonfeeding

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    Quote Originally Posted by Fazan View Post
    Why it took such mental gymnastics to do what shouldn't need to be this complicated, though, I don't know.
    This was the point of the assignment. I have edited your above post and removed the spoilers.

  6. The Following User Says Thank You to jps For This Useful Post:

    KevinWorkman (October 9th, 2012)

  7. #5
    Member
    Join Date
    Oct 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    Fair enough. Apologies for breaking the rules. I didn't think I said too much, but I defer to your judgement.

  8. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    I think I have managed to solve this one on my own now Tried to find where I can put this thread as [SOLVED] so that noone puts energy into it anymore. Where can I find it?

  9. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    The announcements page describes how to mark your thread as solved as well as other forum related useful information.

  10. The Following User Says Thank You to jps For This Useful Post:

    matitorn (October 9th, 2012)

  11. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Gotten easy code by proffessor that I don't understand, do you?

    Great, thanks!

Similar Threads

  1. [SOLVED] Need Help ASAP with my code. should be an easy one for you guys!
    By GalBenH in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2011, 08:27 PM
  2. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  3. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  4. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 12:06 PM
  5. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM

Tags for this Thread