Results 1 to 6 of 6
  1. #1
    Ritter is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default Please help in this program

    Hello,
    I'm learning java but I don't know much.
    I was trying to make a small program in which you enter two number and the sign between them. And it prints the answer.
    import cs1.Keyboard;

    class GreatestNo {


    public static void main(String[] args){

    int no1, no2;

    String mathsign;

    System.out.print("Enter two numbers and the sign between them: \n1st no: ");

    no1 = Keyboard.readInt();

    System.out.print("sign: ");

    mathsign = Keyboard.readString();

    System.out.print("2nd no: ");

    no2 = Keyboard.readInt();

    String plus = "+";

    if (mathsign = plus){

    System.out.println(no1 + "+" + no2 + "=" + (no1 + no2));

    }


    }
    }
    I tried to compile it but I found that when I enter the plus sign it doesn't give a solution or make anything. Instead it prints:
    1st no: 2
    sign: +
    2nd no: 6
    and that's it.
    why it doesn't make this statement:
    System.out.println(no1 + "+" + no2 + "=" + (no1 + no2));
    and prints
    2+6= 8
    please can anyone help and tell me where is the problem?:confused:

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Wrong compare sign

    Ritter... a couple of things...
    • When you want to compare "int"s and "chars" you have to use the "==" operator (don't use "=" for comparing):

    Java Code:
    If (count == 10)
    • When you want to compare strings, you have to use the string method ".equals":

    Java Code:
    if (mathsign.equals(plus))
    Some useful links:

    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Ritter is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for your help.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default no problem

    You're welcome... did it work?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Ritter is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    yes of course it worked!

  6. #6
    sp_29 is offline Member
    Join Date
    Feb 2009
    Location
    philippines
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •