Results 1 to 4 of 4
  1. #1
    Nave is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default user input- Strings

    hey,
    i have been trying to work this out for awhile, it compiles with no problems at all but when i run it, it skips over the part that i want to read in the operator.

    import java.util.Scanner;
    public class calculator
    {
    public static void main(String[]args)
    {
    Scanner keyboard = new Scanner(System.in);

    double total;

    System.out.println("Enter your first number: ");
    double num1 = keyboard.nextDouble();

    //Everything is working fine except for when im reading in the String
    //when I run the program it prints out the System.out.print
    //but doesn't let me enter my operator is this the way to go about it,
    //or do i have to go a completly different way

    System.out.println("Enter your chosen operator: ");
    String oper = keyboard.nextLine();

    System.out.println("Enter your second number: ");
    double num2 = keyboard.nextDouble();

    if(oper == "+")
    {
    total = num1+num2;
    System.out.println(num1+" + "+num2+" = "+total);
    }

    else if(oper == "-")
    {
    total = num1-num2;
    System.out.println(num1+" - "+num2+" = "+total);
    }

    else if (oper == "*")
    {
    total = num1*num2;
    System.out.println(num1+" * "+num2+" = "+total);
    }

    else if (oper == "/")
    {
    total = num1/num2;
    System.out.println(num1+" / "+num2+" = "+total);
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    The nextInt() and nextDouble() methods read ints and doubles from the input and nothing more; when you type your number you press <enter> which is a character in the input stream and it stays there (those methods don't consume it) so you should add a dummy nextLine() call following the reading of your number to get rid of that <enter> key in your input.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nave is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    cheers, i'll give that a shot now

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    May not directly be related to your current problem, but if you want to compare string equality, use the equals() method, not == (which compares the String object, not its value)

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 02:52 PM
  2. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  3. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 09:27 PM
  4. While loop comparing strings from user
    By N3VRMND in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 08:18 AM
  5. If statements, input, and strings
    By evolvepwnz in forum New To Java
    Replies: 16
    Last Post: 10-22-2009, 09:58 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
  •