Simple Java Question!

Not quite sure if I'm allowed to post this here, but great community on this site, so I'm going to try anyways. This is a simple program I wrote up that I'm still playing with to practice my knowledge of input/output, and using if/else. I ask the user which type of computer they want, apple, dell, etc... it repeats back their choice. I ask their budget, but instead of returning the correct budget, it tell's me my else statement "Select a valid option". There's probably a way to do it writing less code, but I'm a noob, and am not aware of it, only been at Java for about a week now :P You will also notice for each price, I have two options. That's in case the user puts in for example 250 rather than $250. Not quite sure why I'm having this problem, thank you!



import java.util.Scanner;

public class Memo{
	
	public static void main(String[] args){
		
		System.out.println("***Welcome to the computer selector!***");
		System.out.println();
		System.out.println("***Answer various questions to find the perfect computer for you!***");
		System.out.println();
		System.out.println("~ Which brand of computer would you prefer?: ");
		System.out.println("  - Dell");
		System.out.println("  - HP");
		System.out.println("  - Compaq");
		System.out.println("  - Apple");
		Scanner scan1 = new Scanner(System.in);
		String system = scan1.nextLine();
		if (system .equals("Dell")){
			System.out.println("You have choosen Dell as your computer manufacturer!");
		}
		else if (system .equals("HP")){
			System.out.println("You have choosen HP as your computer manufacturer!");
		}
		else if (system .equals("Compaq")){
			System.out.println("You have choosen Compaq as your computer manufacturer!");
		}
		else if (system .equals("Apple")){
			System.out.println("You have choosen Apple as your computer manufacturer!");
		}
		else{
		System.out.println("This is not a valid option, please enter a valid option.");
		}
		System.out.println();
		System.out.println("~ Select one of the options closest to your budget for your new computer: ");
		System.out.println("  - $250");
		System.out.println("  - $500");
		System.out.println("  - $750");
		System.out.println("  - $1,000");
		System.out.println("  - $1,250");
		System.out.println("  - $1,500");
		System.out.println("  - $1,750");
		System.out.println("  - $2,000");
		System.out.println("  - $2,250");
		System.out.println("  - $2,500");
		System.out.println("  - $2,750");
		System.out.println("  - $3,000");
		String budget = scan1.nextLine();
		
		if (budget .equals("$250")){
			System.out.println("You have choosen $250 as your current budget for your new computer!");
		}
		else if (budget .equals("250")){
			System.out.println("You have choosen $250 as your current budget for your new computer!");
		}
		else if (system .equals("$500")){
			System.out.println("You have choosen $500 as your current budget for your new computer!");
		}
		else if (system .equals("500")){
			System.out.println("You have choosen $500 as your current budget for your new computer!");
		}
		else if (system .equals("$750")){
			System.out.println("You have choosen $750 as your current budget for your new computer!");
		}
		else if (system .equals("750")){
			System.out.println("You have choosen $750 as your current budget for your new computer!");
		}
		else if (system .equals("$1,000")){
			System.out.println("You have choosen $1,000 as your current budget for your new computer!");
		}
		else if (system .equals("1,000")){
			System.out.println("You have choosen $1,000 as your current budget for your new computer!");
		}
		else if (system .equals("$1,250")){
			System.out.println("You have choosen $1,250 as your current budget for your new computer!");
		}
		else if (system .equals("1,250")){
			System.out.println("You have choosen $1,250 as your current budget for your new computer!");
		}
		else if (system .equals("$1,500")){
			System.out.println("You have choosen $1,500 as your current budget for your new computer!");
		}
		else if (system .equals("1,500")){
			System.out.println("You have choosen $1,500 as your current budget for your new computer!");
		}
		else if (system .equals("$1,750")){
			System.out.println("You have choosen $1,750 as your current budget for your new computer!");
		}
		else if (system .equals("1,750")){
			System.out.println("You have choosen $1,750 as your current budget for your new computer!");
		}
		else if (system .equals("$2,000")){
			System.out.println("You have choosen $2,000 as your current budget for your new computer!");
		}
		else if (system .equals("2,000")){
			System.out.println("You have choosen $2,000 as your current budget for your new computer!");
		}
		else if (system .equals("$2,250")){
			System.out.println("You have choosen $2,250 as your current budget for your new computer!");
		}
		else if (system .equals("2,250")){
			System.out.println("You have choosen $2,250 as your current budget for your new computer!");
		}
		else if (system .equals("$2,500")){
			System.out.println("You have choosen $2,500 as your current budget for your new computer!");
		}
		else if (system .equals("2,500")){
			System.out.println("You have choosen $2,500 as your current budget for your new computer!");
		}
		else if (system .equals("$2,750")){
			System.out.println("You have choosen $2,750 as your current budget for your new computer!");
		}
		else if (system .equals("2,750")){
			System.out.println("You have choosen $2,750 as your current budget for your new computer!");
		}
		else if (system .equals("$3,000")){
			System.out.println("You have choosen $3,000 as your current budget for your new computer!");
		}
		else if (system .equals("3,000")){
			System.out.println("You have choosen $3,000 as your current budget for your new computer!");
		}
		else{
		System.out.println("This is not a valid option, please enter a valid option.");
		}
		
	}
Last edited on
You are comparing system instead of budget in many of the else-ifs.
Hah, knew it would be something stupid that I didn't catch. Thank you Peter87.
closed account (N36fSL3A)
You can post java questions here, we really don't mind.

Design tip:

I believe you should use switch statements when dealing with so many if statements.

I believe the are like such in Java

1
2
3
4
5
6
7
8
9
switch(myvariable)
{
    case '1':
        // Code
    
    case '2':
        // Code
    // Etc
}
closed account (18hRX9L8)
Fredbill30 wrote:
I believe you should use switch statements when dealing with so many if statements.

I believe the are like such in Java

1
2
3
4
5
6
7
8
9
switch(myvariable)
{
    case '1':
        // Code
    
    case '2':
        // Code
    // Etc
}


How are you supposed to use switch statements with strings?
GMPoision wrote:
1
2
3
4
5
//....
		else if (system .equals("$2,500")){
			System.out.println("You have choosen $2,500 as your current budget for your new computer!");
		}
//.... 
Last edited on
closed account (N36fSL3A)
Whoops, sorry. I didn't know those rules applied to Java.
closed account (18hRX9L8)
Fredbill30 wrote:
Whoops, sorry. I didn't know those rules applied to Java.

??? Those rules don't apply to C++ switch statements either...
closed account (N36fSL3A)
I know.
closed account (18hRX9L8)
Fredbill30 wrote:
I know.

You're killing me. :|


[EDIT] OH LOL! I'm stupid... [/EDIT]
Last edited on
Wouldn't it be better to just concatenate the strings instead of having so many ifs? Though I don't know any Java string manipulation surely there's a way. As for the dollar sign at the front couldn't you take a peak at the front of the string and add it if it's not there?
WTF?

Replace all the ifs with the following single line:
 
System.out.printf("You have choosen %s as your current budget for your new computer!\n", budget);


BTW: Java 7 does support switch on strings.


closed account (18hRX9L8)
rapidcoder wrote:
Java 7 does support switch on strings.

Thank you!!! Now I don't have to waste all my time with ifs!!!!! :)
closed account (N36fSL3A)
rapidcoder wrote:
Java 7 does support switch on strings.


Another great reason to learn Java as well.
Topic archived. No new replies allowed.