Results 1 to 5 of 5
  1. #1
    Suriman is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Unhappy Help, java problem

    First of all, i'm new to the comunity, so hello.

    I have a problem, i'm writing a problem that reads input from the console and then needs to calculate the average, the input is a student number and a student grade, so i created a class "student" and made an arraylist of those objects, the problem is that some grades come in String format(like R to reproved students), here is an input example:

    1001 12
    1002 I
    1003 16
    1004 13
    1005 R


    grades t = new grades();
    Scanner in = new Scanner (System.in);
    while (in.hasNextInt()) {
    int x = in.nextInt();

    if (in.hasNextInt())
    {
    int y = in.nextInt();
    Student a= new Student(x, y, "P"); //creates a student //object, the letter "P" is for "Present"

    t.read(a);

    }
    else
    if (in.hasNextLine())
    {
    String k=in.nextLine();
    Student a=new Student(x, 0, k);
    t.read(a);
    }
    if(!in.hasNext())
    break;

    }
    My code never stops reading input, i don't know what else to do :(.

    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Suriman is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    What sould i do then? This is driving me nuts :(

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ok, if I correct what you have done is check for the next int in a while loop at the start. Then you set the condition to exit the code if int is not found, the last if condition. Is it? So what can be happen in your application is that code block is never call, or that not properly exit the code. Just try it first and see. This is just a tip.

  5. #5
    Suriman is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Yup, you're right, that last if is useless, thank you:).

    But it remains in a loop :(

Similar Threads

  1. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 08:28 PM
  2. problem with java nio
    By andrei stoiculescu in forum Networking
    Replies: 3
    Last Post: 02-02-2009, 03:35 PM
  3. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-18-2008, 11:44 PM
  4. JAVA if problem
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:58 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •