Results 1 to 3 of 3
Thread: Problem with Scanners
- 11-11-2011, 04:56 AM #1
Member
- Join Date
- Nov 2011
- Location
- Australia
- Posts
- 28
- Rep Power
- 0
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.
- 11-11-2011, 05:02 AM #2
Re: Problem with Scanners
Google "comparing Strings in Java"
- 11-11-2011, 05:17 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Strings, scanners and IFs
By JavaGame in forum New To JavaReplies: 4Last Post: 09-07-2011, 11:58 PM -
Random automation of sports teams (things) using arrays, scanners, and randomNumbers
By Sizzlewump in forum New To JavaReplies: 6Last Post: 08-22-2011, 05:32 AM -
Buffered Reader and scanners
By nevermiind in forum New To JavaReplies: 8Last Post: 05-04-2010, 06:22 PM -
Scanners / Printers
By cytech in forum New To JavaReplies: 0Last Post: 03-17-2008, 05:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks