Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 20th, 2012, 08:09 AM
Connall Connall is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 Connall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 37 sec
Reputation Power: 0
Java Variable Error

Hey there, so I'm 16 and decided I wanted to teach myself Java after some success with other languages, I picked up a book to help me get on my way. While doing one of the exercises I came across a problem and made sure I had done the code correctly in the book, it appears I have but an error is still popping up.

Here's the code:
Code:
package ticketpricewithdiscount;

import java.util.Scanner;

public class TicketPriceWithDiscount {

    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int age;
        double price;
        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.print(". ");
          System.out.println("Enjoy the show!");
        
                
    }
}


Now the error comes up at the

"System.out.print(price)" as price has a red line underneath it and the editor's reasoning to do this was:

"Initialize Variable price"

I thought I had already done that personally. Anyway, whatever help I could get is appreciated since I don't want to move on in case this pops up further down the road.

(I also apologise for what is probably stupidity.)

EDIT:

When I try to run it this comes up

Exception in thread "main" java.lang.NullPointerException
at ticketpricewithdiscount.TicketPriceWithDiscount.main(TicketPriceWithDiscount.java:18)

Reply With Quote
  #2  
Old August 20th, 2012, 09:11 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,130 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 23 h 46 m 38 sec
Reputation Power: 1736
price is only assigned in if-block. You should give it a default value:
Code:
double price = 0.0;


Regarding NPE at line 18, you can use next() instead of findInLine():
Code:
reply = myScanner.next().charAt(0);

Reply With Quote
  #3  
Old August 20th, 2012, 09:59 AM
Connall Connall is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 Connall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 37 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
price is only assigned in if-block. You should give it a default value:
Code:
double price = 0.0;


Regarding NPE at line 18, you can use next() instead of findInLine():
Code:
reply = myScanner.next().charAt(0);


Huh never thought of that. Wonder why my book doesn't mention it. Might be because it's from 2005 or something. Will give it a shot tomorrow since i am away from my main computer.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Java Variable Error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap