473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

8 New Member
/** 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 9901
r035198x
13,262 MVP
/** 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 New Member
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 MVP
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 New Member
No, I get one long string
Sep 18 '07 #5
r035198x
13,262 MVP
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 New Member
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

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
1
by: yaru22 | last post by:
Hi. I'm using python-mode in emacs. I happened to execute the code that contains an infinite loop. The system is hung and it doesn't stop at all. lol How do I manually stop it in emacs?
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
2
by: Dragonxpert | last post by:
I seem to be having an overflow problem every time I plug in a value. The program is supposed to give the different combinations that a giving amount of money can be sorted into, with care given to...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
3
by: BlueroY | last post by:
hi, I'm working on an exercise, i did a lot of work already and i just can't figure where I'm going wrong, this is what I'm trying to achieve Sample IO...
4
by: chetah | last post by:
bd.txt D2333 How to program in Java r IN 454554 Dietal & Dietal Prentice Hall G4547 Java in a Nut Shell c 343345 David Flagman O'Reilly 03-02-08 END 678764 John Smith...
7
by: schrum312005 | last post by:
Hey guys, I'm a college student in my first programming class and I'm having trouble with an assignment. The requirements to the assignment can be found here:...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.