• Post Reply Bookmark Topic Watch Topic
  • New Topic

If statement with strings?  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a simple calculator. I want the user to input either string "add" or "subtract" and then it will take two numbers and either add or subtract them. I cannot get the if statement to work with a string!

import java.util.Scanner;
public class newcalc {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.println("Enter add or subtract");

String line = input.nextLine();

if input = "add";
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
You have:
if input = "add";

I see a few problems. First, what is your String variable name supposed to be? (It's not input).

Second, Strings are compared using str.equals("add") or "add".equals(str). The = operator is for assignment. Finally, you are missing mandatory parens and have an extra semi-colon.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David.

Remember that when comparing strings you should use the .equals method rather than the == sign.
In fact it is such a common thing we even have a FAQ Question for it


 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying this but getting errors :

import java.util.Scanner;
public class newcalc {
public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.println("Enter add or subtract");
String userinput = input.nextLine();

if(userinput.equals("add")){
System.out.println("adding numbers ");
} else if(userinput.equals("subtract")){
System.out.println("subtracting numbers ");
}
}
}

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's much better. Can you post the error message?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
resource leak, input is never closed or something. The funny thing is that in other programs, I never get this error and I never close the input
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok now that it is working, I want to take two numbers and add them. Can I do this right in the sys.out.print section or do I need to declare variables for it?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the new code, and I am getting an error using the arithmetic operators :

import java.util.Scanner;
public class newcalc {
public static void main(String[] args) {

double numOne = 1;
double numTwo = 4;

Scanner input = new Scanner(System.in);
System.out.println("Enter add or subtract");
String userinput = input.nextLine();

if(userinput.equals("add")){
System.out.println("adding numbers " + numOne+numTwo);
} else if(userinput.equals("subtract")){
System.out.println("subtracting numbers " + numOne - numTwo);
}
}
}
 
J Hill
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(numOne + numTwo); does work when there is nothing else being concatenated (same goes for the subtraction).

Put parentheses around the addition/subtraction operation, like this ("text " + (numOne + numTwo));
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also when posting code, please UseCodeTags <-- (Click me to find out how)

It formats the code so it is much easier to read on this forum.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For output, you may want to look into System,out.printf() or System.out.format() statement. These are equivalent.

An example


In the printf statement %d means a number and %n a new line. The stuff after the first comma is the variables you have used.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:resource leak, input is never closed or something. . . .
Another of the drawbacks of using IDEs at an early stage of learning. You don't know which of Eclipse's warnings to believe and which to ignore.

You must ignore that warning if you are reading from the keyboard. You must never close Scanners (or Readers or similar) pointing to System.in.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you know that in Java7 they changed the switch statement so you can use Strings?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still having difficulty with this. I managed to get it to work, but my input statements do not work so well, meaning when the user inputs the numbers, in the console, the cursor ends up being at the beginning of the "Enter add of subtract:" at the "E in Enter" rather then at the end with the colon " : "
Maybe this has been corrected in the code you worked on, I am sorry I did not copy it but worked from the original code. I am trying to get better but this is taking time! Thanks for your help.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Might I suggest you read our FAQ on how to ask questions? There are a few simple things you can do that make it much easier for people to help you, such as UseCodeTags and TellTheDetails.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I will do, that code tags for sure next time. I was in a rush, but no excuses. Is there something else I forgot in order to post properly
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the code tags for you this time, as well as the last two }.

More about the code:
  • 1: Spread your code out. You are right to put spaces around signs like = but also separate every { from any text preceding it with spaces.
  • 2: Check formats for class names: they should have CapitalLettersIn.
  • 3: Only use one Scanner pointing to System.in; you don't need two. You will only have problems if you call nextLine after nextSomethingElse.
  • The Java® Language Specification can be difficult to read, but that link gives you a completely different way to add two numbers. It might be of interest.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!