Results 1 to 13 of 13
- 07-16-2007, 10:22 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
I need help on a practice code for java
// below is wat comes up when i hit execute (i type the 34 and hit enter, and the next thing that comes up should make me chose between 'Y' and 'N' but it doesn't)Java Code:import java.util.Scanner; class Helpful { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); int age; double price = 0.00; char reply; boolean isKid, isSenior, hasCoupon, hasNoCoupon; System.out.print("How old are you? "); age = myScanner.nextInt(); System.out.print("Have a coupon? (Y/N) "); reply = myScanner.findInLine(".").charAt(0); isKid = age < 12; isSenior = age >= 65; hasCoupon = reply == 'Y' || reply == 'y'; hasNoCoupon = reply == 'N' || reply == 'n'; if (!isKid && !isSenior) { price = 9.25; } if (isKid || isSenior) { price = 5.25; } if (hasCoupon) { price -= 2.00; } if (!hasCoupon && !hasNoCoupon) { System.out.println("Huh?"); } System.out.print("Please pay $"); System.out.print(price); System.out.println("."); System.out.println("Enjoy the movie!"); } }
How old are you? 34
Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
at Helpful.main(Helpful.java:16)
Process completed.Last edited by JavaBean; 07-16-2007 at 10:31 PM.
- 07-16-2007, 10:23 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.
- 07-16-2007, 10:32 PM #3
You just put it inside [code] tag like this: [ code ] your code .. [ / code] (without spaces!)also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.
- 07-16-2007, 10:34 PM #4
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
how do i fix wat i have up there?
- 07-16-2007, 10:38 PM #5
You can try:
instead of:Java Code:String reply = myScanner.nextLine(); if (reply.charAt(0) == 'Y') hasCoupon = true; else hasCoupon = false;
Java Code:reply = myScanner.findInLine(".").charAt(0); hasCoupon = reply == 'Y' || reply == 'y'; hasNoCoupon = reply == 'N' || reply == 'n';
- 07-16-2007, 10:46 PM #6
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
didn't work, but ty anyway
- 07-16-2007, 10:47 PM #7
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
is there any way to get rid of the nullpointerexception? i think that's wat it is
- 07-16-2007, 11:00 PM #8
In this line, if findInLine method returns null then calling charAt method of a null object reference will return you a NullPointerException! This is why i suggested you to do that in another way.Java Code:reply = myScanner.findInLine(".").charAt(0);
- 07-16-2007, 11:03 PM #9
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
ooo, i see
- 07-16-2007, 11:05 PM #10
Member
- Join Date
- Jul 2007
- Posts
- 10
- Rep Power
- 0
well, i just took out that line and everything that goes with it with /*-*/ and it still has the nullpointerexception, so im thinking that maybe it isn't the problem
- 07-16-2007, 11:23 PM #11
Try this:
Java Code:if (myScanner.findInLine(".") == null) System.out.println("This line is the problem");
- 07-13-2010, 03:59 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
replace beline insteadof myScanner.findInline(".").charAt(0);
reply = myScanner.next().charAt(0);
it workds.
enjoy.
- 07-13-2010, 04:00 PM #13
Congrats! You missed being three years late by only a few days :p
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
Building small web application in java for practice.
By Saurabh321 in forum New To JavaReplies: 1Last Post: 02-01-2008, 03:38 PM -
java code
By sobhalr in forum New To JavaReplies: 0Last Post: 12-28-2007, 10:25 AM -
Need a java code
By vissu007 in forum New To JavaReplies: 1Last Post: 07-11-2007, 10:06 PM -
Web Services - IBM Expands SOA Management Practice
By Felissa in forum Web FrameworksReplies: 0Last Post: 06-25-2007, 04:08 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks

Bookmarks