Results 1 to 10 of 10
  1. #1
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default if else if statements

    How do you use an if and else statement with a string? I'm trying to do this

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class Tax_Calculations
    {
    	public static void main(String []args)
    	{
    		String status, income;
    		double INCOME;
    		
    		Scanner keyboard = new Scanner(System.in);
    		JOptionPane.showInputDialog("Please state your marital status. (SINGLE or MARRIED)");
    		status = keyboard.nextLine();
    		
    		if (status == "SINGLE"){
    		
    			JOptionPane.showInputDialog("What is your annual income?");
    			income = keyboard.nextLine();
    			INCOME = Double.parseDouble(income);
    			
    			JOptionPane.showMessageDialog(null, INCOME);
    			System.exit(0);
    			}
    		else if (status == "MARRIED"){
    		System.exit(0);
    		}
    		}
    	}
    I ran it in debug mode and it displays the first dialog window but then it stops at status = keyboard.nextLine();. It doesn't continue to run the program after that. Help! Thank you!
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  2. #2
    MadJack is offline Member
    Join Date
    Sep 2010
    Location
    Qc
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Allspark View Post
    How do you use an if and else statement with a string? I'm trying to do this

    Java Code:
    		if (status == "SINGLE"){
    The only right way to do string comparison in java is with:

    Java Code:
    if (status.equals("SINGLE"))

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    Also you should add an ending else statement to catch the case where none of the above if tests were true.
    Same idea as a default in a switch statement

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Are you mixing command line input and Swing?
    You have a JOptionPane prompting for input and yet seem to be taking your input from System.in...that doesn't sound right to me.

  5. #5
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Thank you for saying that! That's why it doesn't read the input. I'm using System.in! Thanks
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  6. #6
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Ok so I looked for what input method to use and I can't find any that I am able to use! :( What input method should I use? I'm guessing that instead of importing java.util.Scanner I should import something else, but what?
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Are you supposed to be using Swing?
    If not I'd suggest simply doing this with a command prompt.
    System.out.println() for your user prompts.
    Scanner, using System.in, for reading the user input.

  8. #8
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    ok i got the input, now i need help with the if else statment. I'm trying to make this code work with these four statements in red but it doesn't compile. Why?

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class Tax_Calculations
    {
    	public static void main(String []args)
    	{
    		String status, income;
    		double INCOME;
    				
    		Scanner keyboard = new Scanner(System.in);
    		status = JOptionPane.showInputDialog("Please state your marital status. (SINGLE or MARRIED)");
    		
    		if (status.equals("SINGLE"))
    		{
    			income = JOptionPane.showInputDialog("What is your annual income?");
    			INCOME = Double.parseDouble(income);
    [COLOR="Red"][COLOR="Red"]			if (INCOME <= 28000.0){
    				double sin_taxes1;
    				sin_taxes1 = INCOME * .15;
    				JOptionPane.showMessageDialog(null, "With an income of " + INCOME + ", you owe " + sin_taxes1 + " of taxes.");
    
    			}
    			else if (INCOME < 60000.00);{
    				double sin_taxes2;
    				sin_taxes2 = ((INCOME - 28000) * .25) + (28000 * .15);
    				JOptionPane.showMessageDialog(null, "With an income of " + INCOME + ", you owe " + sin_taxes2 + " of taxes.");
    			}
    			if (INCOME > 60000.00);{
    				double sin_taxes3;
    				sin_taxes3 = ((INCOME - 60000.0) * .3) + ((60000.0 - 28000.0) * .25) + (28000 * .15);
    				JOptionPane.showMessageDialog(null, "With an income of " + INCOME + ", you owe " + sin_taxes3 + " of taxes.");
    			}
    			else if (INCOME > 120400.00){
    				double din_taxes4;
    				sin_taxes4 = ((INCOME-120400.0) * .35) + ((120400.0 - 60000) * .3) + ((60000 - 28000) * .25) + (28000 * .15);
    				JOptionPane.showMessageDialog(null, "With an income of " + INCOME + ", you owe " + sin_taxes4 + " of taxes.");[/COLOR][/COLOR]
    				
    			
    			System.exit(0);
    			}
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    it doesn't compile. Why?
    Does the compiler give any error messages?
    Please copy the full text and paste them here.

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What is the exact error message you get? "doesn't compile" isn't a helpful statement.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:25 AM
  2. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 08:02 PM
  3. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 10:50 PM
  4. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 05:24 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •