0

I am very, very new at programming so sorry if this is a stupid question, but I can't find an answer anywhere.

In a program that I am working on the user can choose one of five options for problems to practice: addition, subtraction, multiplication, division, and remainders. There is a scanner that asks them to put in the fist letter of the name of the types of problems they want to practice (A or a, S or s, M or m, D or d, R or r). I want to make an if/else statement that will print out different problems depending on which one they choose.

The problem is, from what I can tell if/else statements will only work with boolean variables, but boolean variables don't like strings or string variables. I have seen ways to convert specific strings to variables, but since the user is deciding on the string, I have no way to know what they are going to choose every time. Is there anyway to convert a string variable to a boolean variable?? (i.e. the boolean variable is true when the string variable = "A")

0

5 Answers 5

2

if(s) can take boolean expressions (and use boolean operators, such as or). For example, String.equals(Object) (or String.equalsIgnoreCase(String)). Something like,

if (string.equals("A") || string.equals("a")) {
    // ...
} else if (string.equalsIgnoreCase("B")) {// <-- or equalsIgnoreCase(String).
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

Personally, my favorite way is to use a switch statement when you're dealing with input, and while it doesn't use boolean values as you are use to in an if statement, for me it feels cleaner.

Your code would look like something such as:

switch(userInput.toLowerCase())
{
    case "a":
    // addition code
    break;
    case "s":
    // subtraction code
    break;
    case "m":
    // multiplication code
    break;
    case "d":
    // division code
    break;
    case "r":
    // remainder code
    break;
    default: // every other option besides (a, s, m, d, and r)
    // print some error, user put wrong input
    break;
}

Comments

0

Ypu can use

java.lang.Boolean(String s)

Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Comments

0

Here's what you can do.You're dealing with a fairly simple problem.

Pseudo-Code

if(input=="A")
{
Do Addition
}
if(input=="M")
{
Do Multiplication
}
else
{
Give Error
}

Essentially, you want to check if the user input is equal to a particular string. You use the double equal to operator (==) here to perform the check.If the condition is TRUE and it will enter the block.

Comments

0

The Point is that the if block is evaluated on the basis of any expression that results into boolean values(i.e. true or false).

So there is no need to convert a String to Boolean.

All you have to do is just check if input string meets your criteria using a method provided by String Class in java called String.equals(String) or String.equalsIgnoreCase(String);

The return type of above methods is boolean (primitive) which will be evaluated by if statement as per your requirements.

To explore String Class in java run "javap java.lang.String" on shell/bash/command prompt for programmer's guide to check out the contents of String Class

So your code will be something like

if(input.equalsIgnoreCase("a"))
{
  // Calculations for addition
}
else if(input.equalsIgnoreCase("s")) 
{
  // Calculations for Substraction
}

Instead you can use switch-case as support for String in switch-case has been included from jdk7.

Also switch-case should be faster in this case as it will not check all cases as done by else if.

switch(input)
{
    case "a":
    case "A":
        // Calculations for addition
        break;
    case "s":
    case "S"
        // Calculations for subtraction
        break;
    case "m":
    case "M":
        // Calculations for multiplication
        break;
    case "d":
    case "D":
        // Calculations for division
        break;
    case "r":
    case "R":
        // Calculations for remainder
        break;
    default: // for any other option besides above
        // whatever handling you wanna do for wrong input
        break;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.