469,362 Members | 2,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Fixing Scnanner scan.hasNext();, infinite loop Java

8
/** I have to use the scanner class (no alternatives), to record a dynamic amount of grades. I use the hasNext() method to get the next value on the same line. For some reason when I am done entering numbers and I press enter the curser waites on the next line and does not exit the program. I want to be able to exit the program once I have entered multable grades and then pressed enter. */


public void enterGrades()
{
Scanner scan = new Scanner(System.in);

ArrayList<String> al = new ArrayList<String>();

System.out.print("Enter grades: ");

while(scan.hasNext())
{
al.add(scan.next() );

System.out.println(al);
}

}
Sep 18 '07 #1
6 9533
r035198x
13,262 8TB
/** I have to use the scanner class (no alternatives), to record a dynamic amount of grades. I use the hasNext() method to get the next value on the same line. For some reason when I am done entering numbers and I press enter the curser waites on the next line and does not exit the program. I want to be able to exit the program once I have entered multable grades and then pressed enter. */


public void enterGrades()
{
Scanner scan = new Scanner(System.in);

ArrayList<String> al = new ArrayList<String>();

System.out.print("Enter grades: ");

while(scan.hasNext())
{
al.add(scan.next() );

System.out.println(al);
}

}
So you want to enter the grades on one line separated by a space?
Don't use the while loop then. Just get the grades as one String using scanner.nextLine(). You should visit the API docs for the Scanner class to understand the differences between the methods.
Note that if you read them all as one string, you would need to split that string first to be able to enter each individual grade into the arraylist.
Sep 18 '07 #2
ibo3
8
So you want to enter the grades on one line separated by a space?
Don't use the while loop then. Just get the grades as one String using scanner.nextLine(). You should visit the API docs for the Scanner class to understand the differences between the methods.
Note that if you read them all as one string, you would need to split that string first to be able to enter each individual grade into the arraylist.

Then how would I pull out each value from the arraylist?
Sep 18 '07 #3
r035198x
13,262 8TB
Then how would I pull out each value from the arraylist?
See the API docs for the ArrayList class. It has all sorts of methods for putting and getting stuff from an ArrayList.
Have you managed to add the grades individually into the ArrayList(not as one long string)?
Sep 18 '07 #4
ibo3
8
No, I get one long string
Sep 18 '07 #5
r035198x
13,262 8TB
No, I get one long string
You can split that using the String.split method.
Check it's specs in API docs again. It can give you an array containing the individual grades.
Sep 18 '07 #6
ibo3
8
You can split that using the String.split method.
Check it's specs in API docs again. It can give you an array containing the individual grades.

thank you for your help!!!

I will check into the String.split method
Sep 18 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

43 posts views Thread by Gremlin | last post: by
1 post views Thread by yaru22 | last post: by
44 posts views Thread by James Watt | last post: by
2 posts views Thread by Dragonxpert | last post: by
1 post views Thread by mshroom12 | last post: by
4 posts views Thread by chetah | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.