473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 9900
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

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.