1

I am new to Java, and I want to create a simple program that allows users to choose between 3 options (t,r and q). q meaning quit. I want the program to keep prompting them to choose a different option until they choose to quit. Below is my code, it doesn't work. I get the error that q,r and t need to be resolved to a variable, but when I set them as strings, it still does not work. Any help will be appreciated.

Scanner input3= new Scanner(System.in);
String choice;
boolean valid;

do 
{
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice==t)
    {
        System.out.println("You chose triangle");
        valid=true;             
    }
    else if(choice==r)
    {
        System.out.println("You chose rectangle");
        valid=true;
    }
    else if (choice==q)
    {
        System.out.println("You chose to quit.");
        valid=false;
    }
    else
    {
        System.out.println("You chose wrong.");
        valid=true;
    }

}
while( valid==true);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
FProlog
  • 173
  • 1
  • 1
  • 9

4 Answers4

3

You should put them arround quotes " because choice is string and you have to compare it with string, e.g :

do {
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice=="t"){
         System.out.println("You chose triangle");
         valid=true;             
    }

    else if(choice=="r"){
         System.out.println("You chose rectangle");
         valid=true;
    }

    else if (choice=="q"){
         System.out.println("You chose to quit.");
         valid=false;
    }
    else{
         System.out.println("You chose wrong.");
         valid=true;
    }

}
while( valid==true);

And you could use equals() instead of == to compare two strings :

do {
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice.equals("t")){
         System.out.println("You chose triangle");
         valid=true;             
    }

    else if(choice.equals("r")){
         System.out.println("You chose rectangle");
         valid=true;
    }

    else if (choice.equals("q")){
         System.out.println("You chose to quit.");
         valid=false;
    }
    else{
         System.out.println("You chose wrong.");
         valid=true;
    }

}
while( valid==true);

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • 1
    Comparing strings using == will compare whether they are the same string. Use String.equals() to compare if the strings have the same letter. – David Glickman Feb 09 '16 at 17:27
3

Check out beginner tutorials on the web (and this SO question: Java String.equals versus ==)

t, r and q need to be set as String variables first.

e.g:

String choice;
String t = "t";
String r = "r";
String q = "q";

You should also use the equals method for string equality.

e.g.:

choice.equals(t);
Community
  • 1
  • 1
hazra
  • 400
  • 1
  • 6
  • 15
  • These variables seem pretty pointless, you better use the literal directly or make a contant: `"t".equals(choice)`. – Clashsoft Feb 09 '16 at 17:44
3
import java.util.Scanner;
public class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner input3= new Scanner(System.in);
        String choice;
        boolean valid;


             do {
                 System.out.println("Please pick an option. t, r or q");
                 choice= input3.next();
             if(choice.equals("t")){

                 System.out.println("You chose triangle");
                 valid=true;             
             }

             else if(choice.equals("r")){
                 System.out.println("You chose rectangle");
                 valid=true;
             }

             else if (choice.equals("q")){
                 System.out.println("You chose to quit.");
                 valid=false;
             }
             else{
                 System.out.println("You chose wrong.");
                 valid=true;
             }

             }
             while( valid==true);


    }
}

Enjoy!!!

Anurag
  • 51
  • 4
1

Strings need to be compared with the function string1.equals(string2). Change all of your if and if else statements to something like this:

choice.equals("t");
TangledUpInBlue
  • 746
  • 4
  • 13