Results 1 to 3 of 3
  1. #1
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Problem with Scanners

    Hi,
    I'm having troubles with a scanner, I have it so that the input is saved to a variable and then when I try to use the variable in a boolean it's as if the input from the scanner is not saved to the variable. It looks like this:
    public void reorderStock()
    {
    Scanner in = new Scanner(System.in);
    String answer;
    double costUsual, costNew;
    int extra;
    System.out.print("Do you want to reorder the usual amount or a new amount? ");
    answer = in.nextLine();
    if (answer=="Usual")
    {
    costUsual = reorder(usualReorderAmount);
    if (costUsual<cashInStore)
    {
    updatenumAvailable(usualReorderAmount);
    updatecashInStore(-costUsual);
    System.out.println("The amount of sandwiches order is " +usualReorderAmount+ " and it cost $" +costUsual+".");
    }
    }
    else if (answer=="New")
    {
    System.out.println("How many extra sandwiches do you want to order? ");
    extra = in.nextInt();
    updateextraRequired(extra);
    costNew = reorder(extraRequired);
    if (costNew<cashInStore)
    {
    updatenumAvailable(extraRequired);
    updatecashInStore(-costNew);
    System.out.println("The amount of sandwiches order is " +extraRequired+ " and it cost $" +costNew+".");
    updateextraRequired(-extra);
    }
    }
    else
    {
    System.out.println("ERROR. You must input Usual or New.");
    }
    Instead of detecting Usual when it is input, its goes straight to the error down the bottom. Any help would be appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Problem with Scanners

    Google "comparing Strings in Java"

  3. #3
    Amit460021 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Re: Problem with Scanners

    In the if statement use "equals()" instead of "==".Like if (answer.equals(Usual)).Both the String variables should be initialized. :-)

Similar Threads

  1. Strings, scanners and IFs
    By JavaGame in forum New To Java
    Replies: 4
    Last Post: 09-07-2011, 11:58 PM
  2. Replies: 6
    Last Post: 08-22-2011, 05:32 AM
  3. Buffered Reader and scanners
    By nevermiind in forum New To Java
    Replies: 8
    Last Post: 05-04-2010, 06:22 PM
  4. Scanners / Printers
    By cytech in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 05:54 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
  •