d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Java Question
Prev12
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 09:39pm
you can't use uc in the for condition if you declare it inside. declare it outside.

watch your control flow. if you only check if it's done in the top of the loop, the bottom of the loop is going to execute. you can always check for the done condition inside the loop like my first suggestion, but you can't do that inside a switch statement since break has a different meaning there.
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 09:45pm
It terminates right away.

double userCash;
String ucc = input.next();
System.out.println("Please type your first name to being");
String user = input.next();
System.out.println("Hello " + user + " and welcome to the game!");

for (userCash = 10.00; userCash > 0.00 && !"done".equalsIgnoreCase(ucc);) {

this is what I have currently have.. I am not quite sure what you are saying. I declared two seperate strings, 1 outside and 1 inside. The one outside stops the code and requires an input.. Is there any way to just simply say, if user.input = "done" end loop?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 10:00pm
Quote (Malexir @ Feb 20 2013 10:45pm)
Is there any way to just simply say, if user.input = "done" end loop?


did you not read my first suggestion?
Quote
if ("done".equalsIgnoreCase(uc))
break;


make sure that's in an IF statement, not SWITCH

This post was edited by carteblanche on Feb 20 2013 10:00pm
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 10:13pm
I have tried the if ("done".equalsIgnoreCase(uc))
break;
I have put it in numerous places.. It just is not working.

I just tried making another case named "done" within I subtract the userCash - userCash, which ends the code, but not before it println's the default case.. I am a noob at this coding stuff so I really don't understand the .equalsIgnoreCase which is probably why I dont know where to put it within my code..
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 20 2013 10:52pm
i'd put it right after you read it.

String uc = input.next().toLowerCase();
if ("done".equalsIgnoreCase(uc))
break;

switch (...)

you need to learn how to debug.
Member
Posts: 534
Joined: Feb 8 2011
Gold: 53.30
Feb 20 2013 11:07pm
Thank you, that did in fact work.
And I agree, next week we are planning to dabble into debugging.. I only read a few pages into it, and tested it out..
I appreciate you repeatedly helping me :D
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll