Results 1 to 7 of 7
Thread: input nextLine error
- 02-19-2011, 10:20 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 13
- Rep Power
- 0
input nextLine error
Someone please help me with my delemma. My following code works fine but there is one major flaw that I am seeing. I am sure there are many if you are advanced level coder. This is for a beginner course. So please help me with the simplest solution possible because the class has not dealt with advance level syntax.
My input is supposed to be "band name 6", "band name 8" and so forth.
In the first line within the While loop, I really need to use nextLine because the band name may be 2 words. When I do change it to nextLine, I get input mistmatch exception error.
Java Code:public class Main { private static int totalCD; public static void main(String[] args) { Map<String, Integer> listBand = new TreeMap<String, Integer>(); Scanner in = new Scanner(System.in); boolean done = false; System.out.print("Enter band name and # of CD (q to quit): "); while (!done) { String bandName = in.next(); [B]//if I change it to in.nextLine(), I get runtime error when I run the program. It is a input mismatch exception error[/B] if (bandName.equalsIgnoreCase("Q")) { listBand.remove("q"); done = true; } else { int numberOfCD = in.nextInt(); listBand.put(bandName, numberOfCD); } } Set<String> keySet = listBand.keySet(); int count = 0; for (String key : keySet) { int value = listBand.get(key); System.out.printf("%-9s%d\n", key, value); count++; totalCD = totalCD + value; } System.out.println("There are a total of " + count + " bands"); System.out.println("total number of CDs " + totalCD); } }Last edited by scoobyrox; 02-19-2011 at 11:04 PM.
- 02-19-2011, 10:26 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
please put in code form! its hard to read.
use this , but without spaces. [code] your code here [/ code]
- 02-19-2011, 11:05 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 13
- Rep Power
- 0
thanks
I just changed it to code format. Thanks. I am new to this forum so I didn't know.
- 02-19-2011, 11:15 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Do you understand why you get that runtime error? It's because nextLine() eats the entire line (including the number which for all it knows is part of the band name, like U2 or whatever).
If you ask me (or even if not) your teacher is being a bit of a b@stard here.
next() won't work - it will fail for band names with a space in them. (It is too timid)
nextLine() won't work - it will fail because it grabs the number. (It is too greedy)
You want some sort of Goldilocks solution... Suppose you read the entire line but, instead of assigning this to the band name, you attempt to break it up into two pieces.
Try and find a precise step by step recipe you could use to get the split "just right". It will probably involve some String methods - but try and express it in plain English first.
- 02-25-2011, 12:32 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 13
- Rep Power
- 0
my changes
So here is my new code but I am getting this new runtime error which I am not sure why. The first while loop runs fine but during the 2nd loop run, both questions are asked at the same time.
Enter band name: Springsteen
Enter # of CD for the band: 3
Are you done with your entry? (Y or N) n
Enter band name: Enter # of CD for the band:
Java Code:public class Main { private static int totalCD; public static void main(String[] args) { Map<String, Integer> listBand = new TreeMap<String, Integer>(); Scanner in = new Scanner(System.in); boolean done = false; while (!done) { System.out.print("Enter band name: "); String bandName = in.nextLine(); System.out.print("Enter # of CD for the band: "); int numberOfCD = in.nextInt(); listBand.put(bandName, numberOfCD); System.out.print("Are you done with your entry? (Y or N) "); String answer = in.next(); if (answer.equalsIgnoreCase("Y")) { done = true; } } Set<String> keySet = listBand.keySet(); int count = 0; for (String key : keySet) { int value = listBand.get(key); System.out.printf("%-9s%d\n", key, value); count++; totalCD = totalCD + value; } System.out.println("There are a total of " + count + " bands"); System.out.println("Total of" + totalCD + "CDs"); }
- 02-25-2011, 12:44 AM #6
Your new problem is beacause nextInt doesn't eat the newline. So if you type 6ENTER nextInt reads the 6 but leaves the ENTER. Then a call to nextLine returns the empty String between 6 and ENTER. Solution: simply add a call to nextLine after the call to nextInt and throw away the return value.
- 02-25-2011, 01:08 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Need help with input/output program error
By stefan2892 in forum New To JavaReplies: 2Last Post: 02-07-2011, 07:57 PM -
Input stream error
By Johnny68 in forum New To JavaReplies: 10Last Post: 08-05-2010, 06:20 PM -
Problem Of Scanner Object with its method nextLine()
By Cluster Storm in forum AWT / SwingReplies: 12Last Post: 06-17-2010, 05:40 PM -
input.nextLine();
By Cass29 in forum New To JavaReplies: 3Last Post: 12-21-2009, 07:43 PM -
.nextLine(); only picks up first word
By ethanemc505 in forum New To JavaReplies: 1Last Post: 10-08-2009, 07:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks