Reputation: 0 Apprentice
- New Members
- Active Posts:
- 10 (0.01 per day)
- 22-February 13
- Profile Views:
- Last Active:
- Mar 14 2013 08:10 AM
- Dream Kudos:
Posts I've Made
Posted 14 Mar 2013
Scanner kbReader = new Scanner(System.in); Scanner kbReader1 = new Scanner(System.in); Scanner kbContinue = new Scanner(System.in);
When you have a phone conversation with a friend do you hangup and redial after each sentence ?
Have a single channel assigned to System.in at a time and use it every time
I use nextLine with one and the other two im honestly not sure why i have two.
Posted 12 Mar 2013Okay thanks! and is it possible to not be limited just to holding ints and strings in one array,
n say i was writing a program where I wanted to type in an employe's employe number and then it would come up with their name and how many hours they have worked? So the out put would be something like
ENTER EMPLOYE #: (user enters number)2
EMPLOYE SALLY JONES HAS WORKED 2 HOURS
<the hours would be put in at the end of each work day or something and would stay in an array>
Posted 6 Mar 2013Obviously the user simply enter a <ENTER> and the String length is 0 so getCharAt(0) fails
And why 3 Scanner assigned to System.in ?
When you talk on the phone with a friend do you hang up the phone and redial at each sentence ? Same thing here. Have only one Scanner with one channel assigned to System.in
Well, for one reason thats all ive been taught, and two i beleive because i have all three scanners accepting input they should all be (System.in), correct?
QuoteException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Ah, thank you. This is useful. So you're trying to access the first character of some String, and it's out of bounds. This means that there's nothing in that String. This is happening at line 70 of your program, which is:
choice = getChoice.charAt(0);
So what is getChoice, and how did it get that way? Ah, this is a classic one - it's actually not likely you'd figure this out on your own.
What is happening here is that the Scanner is playing you false. There's a subtle not-exactly-bug-but-not-what-you-expect in the "convenience" methods of Scanner which always trips people up.
The nextInt method calls next() to get a token and then tries to parse that token as an int. If it manages to do so, it returns the int, otherwise and exception is thrown. So far, so good, but what you don't realize is that this only takes one token from the line you entered. The line you entered was something like 5\n, and the next() call took only the 5 part. The \n is still there.
Now when you call nextLine(), that grabs everything up to the next newline character, discards the newline, and returns the rest. This means you get \n, minus the "\n" part: the result is "". So you have a String of length zero, and hence your exception.
To kill this, call nextLine() and discard the result after you call nextInt(), if you're going to call a nextLine() on that scanner.
Ugly, I know. I usually just avoid the "convenience" methods and parse the input myself - easier that way, I find.
wow, i honestly cant believe mixed a "nextLine()" with a "nextInt()"
- Member Title:
- New D.I.C Head
- Age Unknown
- Birthday Unknown
- Click here to e-mail me
nickerb2 hasn't added any friends yet.