findInLine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noisepoet
    New Member
    • Feb 2007
    • 3

    findInLine

    Can anyone tell me why this code doesn't compile? It has something to do with the line that reads: reply = myScanner.findI nLine(".").char At(0);
    Code:
    import java.util.Scanner;
    
    class TicketPriceWithDiscount {
    
    	public static void main(String args[]) {
    		Scanner myScanner = new Scanner(System.in);
    		int age;
    		double price = 0.00;
    		char reply;
    		
    		System.out.print("How old are you? ");
    		age = myScanner.nextInt();
    		
    		System.out.print("Have a coupon? (Y/N) ");
    		reply = myScanner.findInLine(".").charAt(0);
    		
    		if (age >= 12 && age < 65) {
    			price = 9.25;
    		}
    		if (age < 12 || age >= 65) {
    			price = 5.25;
    		}
    		
    		if (reply == 'Y' || reply == 'y') {
    			price -= 2.00;
    		}
    		if (reply != 'Y' && reply != 'y' &&
    		reply != 'N' && reply != 'n') {
    		System.out.println("Huh?");
    		}
    		
    		System.out.print("Please pay $");
    		System.out.print(price);
    		System.out.print(". ");
    		System.out.println("Enjoy the show!");
    	}
    }
    Last edited by horace1; Feb 15 '07, 08:23 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    compiles OK but has nullpointer exception - try replacing
    Code:
         reply = myScanner.findInLine(".").charAt(0);
    with
    Code:
         reply = myScanner.findWithinHorizon(".",0).charAt(0);

    Comment

    • neils
      New Member
      • Jul 2008
      • 1

      #3
      haha this is from java for dummies right? even i've been trying to figure out the same problem, but its probably coz the author has worked on an older version of java, and that makeshift method of his is no longer valid...gonna try out the suggestion put here and lets see if it works...
      the problem has something to do with the 0 in charAt, coz that doesnt seem to accept null values any more...

      UPDATE : ok i tested the suggestion here and it does work...thanks a lot...

      Comment

      • Kid Programmer
        New Member
        • Mar 2008
        • 176

        #4
        Originally posted by neils
        haha this is from java for dummies right? even i've been trying to figure out the same problem, but its probably coz the author has worked on an older version of java, and that makeshift method of his is no longer valid...gonna try out the suggestion put here and lets see if it works...
        the problem has something to do with the 0 in charAt, coz that doesnt seem to accept null values any more...

        UPDATE : ok i tested the suggestion here and it does work...thanks a lot...
        Begging Programming with Java For Dummies 2nd Edition uses Java 5.0

        Comment

        Working...