d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > While Loops + Switch Statements
12Next
Add Reply New Topic New Poll
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 09:43pm
Long story short. I need to code this so that if the user does not have 2.00, it prompts them a message saying so and ask them to pick a different type of roll.. however, I cannot figure out how to allow the user to input something that will be used for the switch statement.. the switch statement is switch(uc) if that helps.. I've tried half a dozen things, and nothing can work. This is what i currently have.

Code
case "double":
   if (userCash < 2.00){
    System.out.println("You do not have enough money. Choose a cheaper bet!");
    input.next().equals(uc);
   }else {


I had a while loop, but wouldnt work. I had something like

Code
while (userCash < 2.00) {
System.out.println("blah");
input.next.equals(uc);


This post was edited by Malexir on Feb 22 2013 09:44pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 22 2013 09:48pm
input.next().equals(uc);

you realize that doesn't actually do anything right?
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 09:53pm
Quote (carteblanche @ Feb 22 2013 09:48pm)
input.next().equals(uc);

you realize that doesn't actually do anything right?


:( no. i honestly thought it would set the user's input equal to the string uc.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Feb 22 2013 10:08pm
Quote (Malexir @ Feb 22 2013 07:53pm)
:( no. i honestly thought it would set the user's input equal to the string uc.


nvm

This post was edited by SelfTaught on Feb 22 2013 10:09pm
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 10:10pm
Quote (SelfTaught @ Feb 22 2013 10:08pm)
What about, uc.equals(input.next());
??


doesn't work. I need to somehow turn the input, into one of the five case statements.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 22 2013 11:04pm
Quote (Malexir @ Feb 22 2013 11:10pm)
doesn't work. I need to somehow turn the input, into one of the five case statements.


how do you know what the 5 case statements are? what are your possible inputs? then figure out how to map them
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 22 2013 11:37pm
Quote (carteblanche @ Feb 22 2013 11:04pm)
how do you know what the 5 case statements are? what are your possible inputs? then figure out how to map them


I have tried several different ways to get the user's input register as a string. as the case statements, as the switch itself.. is there any tip you could give.. Am I trying to use the user's input as a string? as a case? Im just :wallbash: at the moment.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 22 2013 11:53pm
do something like that

uc = input.next().trim().toLower();
System.out.println("uc=" + uc);
System.out.println("uc.equals double=" + uc.equals("double"));
System.out.println("uc == double=" + (uc == "double"));
switch (uc){
...
}

what does the print statement say?

i'm new to the switch with strings in java. i think this was just a recent addition in java 7 maybe? i learned java 5 and haven't kept up with the new stuff much. back when i learned it, you could only use it with primitive integer values (byte, short, char, etc) and not with objects. does switch use the == comparison or does it invoke .equals? if the latter, does it work for all objects or only strings? if the former and it's coming from user input, probably the == doesn't work. so if you're having problems with switch, try using if/else

This post was edited by carteblanche on Feb 22 2013 11:57pm
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 23 2013 12:02am
Ugh... I just think its a simple 1-2 line that I need.. Your above statement is too much. Lots of println's i dont need. The code is as it needs to be, with the one exception that at this section of code :

Code
case "double":
   if (userCash < 2.00){
    System.out.println("You do not have enough money. Choose a cheaper bet!");
    input.next();
   } else{


There has to be some way to take user's input and translate it into the switch that enables the cases.

/e The part input.next(); I just have a feeling its something very simple.
as far as using if/else statements goes.. I did the entire thing originally with if / else and it was a nightmare.. switch with cases makes the code cleaner and easier to manage.

This post was edited by Malexir on Feb 23 2013 12:04am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 23 2013 12:04am
oh. i thought you had a problem with the switch/case itself.

uc = input.next();

then make sure your switch is inside the loop so that it goes back to the switch at the next iteration
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll