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 February 1st, 2013, 12:25 PM
generaltso78 generaltso78 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 generaltso78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 56 sec
Reputation Power: 0
Homework - JOption input validation

I have this very simple beginner program for my java class. I cannot seem to grasp why my if statement is not being executed when the correct input is entered. I know I could probably get the user to enter a number which I could convert and run through a switch but I really have to know why this doesn't work, I've been reading through JOption forums for the past hour and I guess I'm not seeing my solution.

I am probably not comprehending something so simple and fundamental. I rightfully deserve any tongue/keyboard lashing.

Thanks in advance,

Mike
Code:
import javax.swing.*;

public class Week04_InternetServiceProvider_michaelBrooks {

	
	public static void main(String[] args) {
		String input;
		String internetType;
		double hours = 0;
		double overages = 0;
		double internetPlanA = 9.95;
		//double internetPlanB = 13.95;
		//double internetPlanC = 19.95;
		
		
		input = JOptionPane.showInputDialog("What type of internet package do you you have? Enter A, B or C").toUpperCase();
		internetType = input;
		
		input = JOptionPane.showInputDialog("How many hours did you use?");
		hours = Double.parseDouble(input);
		
		if (internetType == "A"){
			if (hours >10) {
				overages = hours - 10;
				overages *= 2;
				internetPlanA += overages;
			}
		
			JOptionPane.showMessageDialog(null, "Your total monthly bill is: $" + internetPlanA);
		}
				
		System.exit(0);
	
		
	}
		
}

Reply With Quote
  #2  
Old February 1st, 2013, 03:09 PM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Change the line:

Code:

		if (internetType == "A"){


to:


Code:
		if (internetType.equals("A")){


The reason is that a String is handled like an Object. To perform equality checks with a String (and any other Object or subclass of Object) you use the 'equals'-method. Frankly, the == operator is also an equality operator for objects, but it checks if the references are equal (or in other words: if both objects are on the same memory position).
This only holds for objects. If you want to test for equality with primitive types, such as int, byte, boolean, char, then you can use the == operator

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - JOption input validation

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