473,382 Members | 1,247 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,382 software developers and data experts.

findInLine

Can anyone tell me why this code doesn't compile? It has something to do with the line that reads: reply = myScanner.findInLine(".").charAt(0);
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. class TicketPriceWithDiscount {
  4.  
  5.     public static void main(String args[]) {
  6.         Scanner myScanner = new Scanner(System.in);
  7.         int age;
  8.         double price = 0.00;
  9.         char reply;
  10.  
  11.         System.out.print("How old are you? ");
  12.         age = myScanner.nextInt();
  13.  
  14.         System.out.print("Have a coupon? (Y/N) ");
  15.         reply = myScanner.findInLine(".").charAt(0);
  16.  
  17.         if (age >= 12 && age < 65) {
  18.             price = 9.25;
  19.         }
  20.         if (age < 12 || age >= 65) {
  21.             price = 5.25;
  22.         }
  23.  
  24.         if (reply == 'Y' || reply == 'y') {
  25.             price -= 2.00;
  26.         }
  27.         if (reply != 'Y' && reply != 'y' &&
  28.         reply != 'N' && reply != 'n') {
  29.         System.out.println("Huh?");
  30.         }
  31.  
  32.         System.out.print("Please pay $");
  33.         System.out.print(price);
  34.         System.out.print(". ");
  35.         System.out.println("Enjoy the show!");
  36.     }
  37. }
  38.  
Feb 15 '07 #1
3 5060
horace1
1,510 Expert 1GB
compiles OK but has nullpointer exception - try replacing
Expand|Select|Wrap|Line Numbers
  1.      reply = myScanner.findInLine(".").charAt(0);
  2.  
with
Expand|Select|Wrap|Line Numbers
  1.      reply = myScanner.findWithinHorizon(".",0).charAt(0);
  2.  
Feb 15 '07 #2
neils
1
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...
Jul 2 '08 #3
Kid Programmer
176 100+
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
Jul 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.