jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes How Java reads a program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Java reads a program" Watch "How Java reads a program" New topic
Author

How Java reads a program

Austin Connor
Greenhorn

Joined: Aug 10, 2009
Posts: 1
I wrote a program and the part with the char variable isnt working. so i asked my bro for help he said he doesnt know how to fix it but he knows whats wrong. he said that it is reading: reply = myScanner.findInLine(".").charAt(0); firs before it reads some other thing .


I need to know how to make it work and also i get this message when i run it:
Have a coupon? (Y/N)
Exception in thread "main" java.lang.NullPointerException
at Book1.TicketPriceWithDiscount.main(TicketPriceWithDiscount.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

findInLine(".") must be returning null. The reason it is doing this is nextInt() matches the next integer value then advances passed it. It does not match the line seperator that follows the int. So when you call findInLine(".") there is nothing to find, since as the documentation for this method says:

If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
The findInLine() method seems too complicated for what you require. Try simply next().

Note with Scanners you may occasionally have to invoke nextLine() and discard the line because you have got out of step with your line ends.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

if you use next(), then reply will be a string, which is probably better.
If so, you could use a pattern [YyNn] to check it instead of your if statement.
also,
A more concise way to check the input could be if(reply.toUpperCase().equals("Y"))
(anyway, that is the way I usually see it done)

giving:
Brenda Orth
Greenhorn

Joined: Aug 11, 2009
Posts: 1
FYI

When using nextInt(), Java input grabs the integer, but when the next input is a String it may read the Enter Key as input.
Sometimes it helps to use a nextLine() to clear this out of the buffer before reading a String input. Using nextLine will read the entire line, including the Enter Key. I did not repeat all the code, but here is an example:

String dummy= "";

System.out.print("How old are you? ");
age = myScanner.nextInt();
dummy = myScanner.nextLine();
System.out.println("Have a coupon? (Y/N) ");
reply = myScanner.nextLine("[YyNn]");

of course it would still be good to use a try catch block
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Welcome to JavaRanch Brenda Orth.

Do you really need a try-catch when using a Scanner on System.in?
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Campbell Ritchie wrote:
Do you really need a try-catch when using a Scanner on System.in?



there are a lot of ways to do it, but if I'm using a pattern with next(), and if input fails to match the pattern it will throw InputMismatchException, and I can't see from the API another way to work with next(pattern)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
I thought you only get an InputMismatchException (IME) from Scanner using its nextXXX() methods. If you want inScan.nextInt() and pass "123.456" or "Campbell" you will suffer an IME. I always thought the next() method simply returns the next token as a String. You might get EndOfFileExceptions if you pass ctrl-Z (Windows) or ctrl-D (other operating systems) however.
Anyway, as Rob Prime pointed out recently, you can always avoid the IME by putting the nextXXX() call in a loop, like this but possibly with a politer error message:
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Campbell Ritchie wrote:I thought you only get an InputMismatchException (IME) from Scanner using its nextXXX() methods. If you want inScan.nextInt() and pass "123.456" or "Campbell" you will suffer an IME. I always thought the next() method simply returns the next token as a String. You might get EndOfFileExceptions if you pass ctrl-Z (Windows) or ctrl-D (other operating systems) however.
Anyway, as Rob Prime pointed out recently, you can always avoid the IME by putting the nextXXX() call in a loop, like this but possibly with a politer error message:


You are right. This is much better. Now why doesn't Scanner have nextChar methods?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Tim McGuire wrote: . . . You are right.
I do get it right just once in a while, but it was actually Rob Prime who taught me that trick.
Now why doesn't Scanner have nextChar methods?
You don't need a nextChar(). The token input is a String and you can always get chars out of a String with its charAt() and toCharArray() methods. The nextXXX() methods don't simply return a value, but parse the String as well. You don't pass 123 to nextInt(); you pass "123". Note the subtle difference.

Beware: you can get Exceptions if you try those methods on a zero-length String, but I find it difficult to see how you can get a Scanner to return a zero-length String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
I wrote: . . . I find it difficult to see how you can get a Scanner to return a zero-length String.
I have worked out how to get a zero-length String. Set up a Scanner with "," as its delimiter, then pass this sort of input:

a, b, c,, d, e
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Java reads a program