Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 11:22 PM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
I need help on a practice code for 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!"); } }
// 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)


Quote:
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 11:31 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2007, 11:23 PM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-16-2007, 11:32 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
also, i didn't know how to put it on forums without the indents being taken out, so sorry about that.
You just put it inside [code] tag like this: [ code ] your code .. [ / code] (without spaces!)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-16-2007, 11:34 PM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
how do i fix wat i have up there?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-16-2007, 11:38 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
You can try:

Code:
String reply = myScanner.nextLine(); if (reply.charAt(0) == 'Y') hasCoupon = true; else hasCoupon = false;
instead of:

Code:
reply = myScanner.findInLine(".").charAt(0); hasCoupon = reply == 'Y' || reply == 'y'; hasNoCoupon = reply == 'N' || reply == 'n';
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-16-2007, 11:46 PM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
didn't work, but ty anyway
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-16-2007, 11:47 PM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
is there any way to get rid of the nullpointerexception? i think that's wat it is
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-17-2007, 12:00 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Code:
reply = myScanner.findInLine(".").charAt(0);
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-17-2007, 12:03 AM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
ooo, i see
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-17-2007, 12:05 AM
Member
 
Join Date: Jul 2007
Posts: 10
Sageinquisitor is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-17-2007, 12:23 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Try this:

Code:
if (myScanner.findInLine(".") == null) System.out.println("This line is the problem");
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building small web application in java for practice. Saurabh321 New To Java 1 02-01-2008 04:38 PM
java code sobhalr New To Java 0 12-28-2007 11:25 AM
Need a java code vissu007 New To Java 1 07-11-2007 11:06 PM
Web Services - IBM Expands SOA Management Practice Felissa Web Frameworks 0 06-25-2007 05:08 AM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM


All times are GMT +3. The time now is 12:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org