10 Replies - 5860 Views - Last Post: 20 September 2010 - 04:56 AM Rate Topic: -----

#1 Guest_Predicon*


Reputation:

If Statements & Strings

Posted 19 September 2010 - 05:25 AM

Hey I am quite new to java was wondering why java won't let me use a statement like the one below, I have programmed in vb.net before and it seems ok with a similar styled statment.
import java.io.*;

public class calculator
{
    public static void main (String[] args) throws IOException{

InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
     String choice;
     int n1, n2, n3;
      

    System.out.println("What function Would You Like To Use?");
     System.out.println("1. Plus +");
      System.out.println("2. Minus -");
       System.out.println("3. Multiply *");
        System.out.println("4. divide /");
         System.out.println("5. Exit Programe");

          choice = br.readLine();

if(choice = "1")
    {System.out.println("Choice 1 Selected");}
        }
}




Many Thanks

edit: the error I am looking at is the if statement.
thanks

Is This A Good Question/Topic? 0

Replies To: If Statements & Strings

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: If Statements & Strings

Posted 19 September 2010 - 05:30 AM

= is for assignment, == is for comparison.

Bad answer...sorry about that.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: If Statements & Strings

Posted 19 September 2010 - 05:33 AM

This is Java... string comparisons are done with if(choice.equals("1")), not "=="

"==" will compare objects.

See equals

This post has been edited by n8wxs: 19 September 2010 - 05:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: If Statements & Strings

Posted 19 September 2010 - 05:37 AM

And that's why I probably shouldn't answer Java questions...sorry :)
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: If Statements & Strings

Posted 19 September 2010 - 05:47 AM

also remove "" in 1
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: If Statements & Strings

Posted 19 September 2010 - 06:19 AM

View PostJackOfAllTrades, on 19 September 2010 - 04:37 AM, said:

And that's why I probably shouldn't answer Java questions...sorry :)


No need to apologize, the range of stuff you cover is phenomenal! :)
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: If Statements & Strings

Posted 19 September 2010 - 06:48 AM

View Postn8wxs, on 19 September 2010 - 06:04 PM, said:

View PostJackOfAllTrades, on 19 September 2010 - 04:37 AM, said:

And that's why I probably shouldn't answer Java questions...sorry :)


No need to apologize, the range of stuff you cover is phenomenal! :)



True!
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,123
  • Joined: 27-December 08

Re: If Statements & Strings

Posted 19 September 2010 - 08:13 AM

View Postsmohd, on 19 September 2010 - 08:47 AM, said:

also remove "" in 1

The readLine() method returns a String, not an int. So the "" should stay. :)
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: If Statements & Strings

Posted 19 September 2010 - 08:44 PM

So, to compile the information.

In if statements, you use "==" to compare, not "=". Unlike VB, "=" means it is assigning a value not querying whether the two are equal. This leads to rather unpredictable results if you use "=". however, the "==" when used on Objects asks if they are in the same memory location (same object not same contents). For most cases, when comparing objects, you should use .equals(). (For those cases where it's not, look at my tutorial on the String Pool).
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,456
  • Joined: 20-September 08

Re: If Statements & Strings

Posted 20 September 2010 - 03:29 AM

Probably better to use switch:

char choice = br.readLine().charAt(0);
switch(choice) {
    case 1:
    System.out.println("Choice 1 Selected");
    break;

    case 2:
    System.out.println("Choice 2 Selected");
    break;

}
      


Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: If Statements & Strings

Posted 20 September 2010 - 04:56 AM

View Postn8wxs, on 19 September 2010 - 07:19 AM, said:

View PostJackOfAllTrades, on 19 September 2010 - 04:37 AM, said:

And that's why I probably shouldn't answer Java questions...sorry :)


No need to apologize, the range of stuff you cover is phenomenal! :)


Thanks, but obviously you've got me beat! :) I've just been venturing in on occasion to see if I can pick up a little Java, and I managed to learn from this topic, so that's a plus ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1