PDA

View Full Version : Anyone Speak Java?


SportinOne
02-17-2011, 12:53 PM
So in an effort to be quick, i'm writing a program that checks input for an integer. there is a loop so that nothing can happen until an integer is entered. The problem is that the loop repeats itself twice when a non integer is entered without prompting for a new entry between the first and second error message. It seems like the non integer entry is stuck in there. I've already made a statement that takes the non integer input and passes it to a variable so that it can process out, because without it its an infinite loop. So i'm wondering why it takes two rounds to get the input out of there. I'm using in.nextLine() to pass the input to a variable. Is there a better way?

Any help is much appreciated.

Hercules Rockefeller
02-17-2011, 12:57 PM
I speak Jive

bronco militia
02-17-2011, 01:03 PM
http://farm5.static.flickr.com/4037/4471910605_9ab344f82c.jpg

jhns
02-17-2011, 01:08 PM
I am having a hard time figuring out what you are asking but it sounds like a problem with the way your loop is structured.

SportinOne
02-17-2011, 01:20 PM
You can use in.nextInt for integers. It sounds like a problem with the way your loop is structured though.

the problem is when i enter a non integer such as an "a." The "a" seems to be sticking in that input and it fails to prompt the user for an input until the second time around.

So it goes:

Prompt for integer
Allow Input
Integer not entered, so print error message
Prompt for integer
(skips input allowance, i think because it's still stored in input)
Integer not entered, so print error message
Prompt for integer
Allow input (finally)

here's my code:

do
{

//prompt user for their decision (buy, sell, or end turn)
intEntered = true;
System.out.println();
System.out.print("Would you like to: " + "\n1. Buy" + "\n2. Sell" + "\n3. End Your Trading");
System.out.println();
if(!in.hasNextInt())
{
dummy = in.nextLine();
System.out.println("Invalid option. Please Select 1, 2, or 3.");
intEntered = false;

}

//check to see if input was integer
} while(intEntered == false);

Without the statement: "dummy = in.nextLine();" i have an infinite loop, so this processes the input somewhere but it takes two turns for some weird reason. If i have two dummy statements, then it takes an extra "a" entry to get to the next prompt.

rovolution
02-17-2011, 01:37 PM
is this command line based?

I see an "in" object in your code but im not sure what type it is.

how exactly are you reading input in from the user?

SportinOne
02-17-2011, 01:38 PM
is this command line based?

I see an "in" object in your code but im not sure what type it is.

how exactly are you reading input in from the user?

this is a code fragment.

there is a scanner to read input.

jhns
02-17-2011, 01:41 PM
I think you want to use in.nextInt for the input, then you need to clear it with in.next() after the error message when it isn't an integer. You may not need to change to in.nextInt.

SportinOne
02-17-2011, 01:43 PM
I think you want to use in.nextInt for the input, then you need to clear it with in.next() after the error message when it isn't an integer.

well, see the problem is that if i use in.nextInt and the user doesn't enter an int then the program crashes... so i have to have this looping prompt ahead of it to continue prompting until the integer is entered. but i will try in.next() quick.

Edit: Jhns, i could kiss you. Or maybe just read from the text more next time.. lol in.next() saved the day.

Thanks, pal!

jhns
02-17-2011, 02:06 PM
well, see the problem is that if i use in.nextInt and the user doesn't enter an int then the program crashes... so i have to have this looping prompt ahead of it to continue prompting until the integer is entered. but i will try in.next() quick.

Edit: Jhns, i could kiss you. Or maybe just read from the text more next time.. lol in.next() saved the day.

Thanks, pal!

Yeah, if you used nextInt you would have to check for the error. Your current way is better.

McDman
02-17-2011, 05:22 PM
"Waki mallya kuna chu chu? " oh wait, that's what Jabba speaks.

BroncosSR
02-17-2011, 06:26 PM
.

BroncosSR
02-17-2011, 06:38 PM
.

UltimateHoboW/Shotgun
02-17-2011, 07:15 PM
<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/7fkZdz4Vz10" frameborder="0" allowfullscreen></iframe>

?

broncocalijohn
02-17-2011, 08:13 PM
I know Jack talk Thai. Jack talk Thai very well.

OABB
02-17-2011, 08:52 PM
Damn.... Just came here to make an airplane jive joke.


You guys are good.

worm
02-17-2011, 09:41 PM
Edit: Jhns, i could kiss you. Or maybe just read from the text more next time.. lol in.next() saved the day.

Thanks, pal!

Once again it is proven that there is no question or problem that the Mane doesn't have an answer for.

We need a Watson vs Mane throwdown challenge.

OOJack
02-18-2011, 09:21 AM
do
{

//prompt user for their decision (buy, sell, or end turn)
intEntered = true;
System.out.println();
System.out.print("Would you like to: " + "\n1. Buy" + "\n2. Sell" + "\n3. End Your Trading");
System.out.println();
if(!in.hasNextInt())
{
dummy = in.nextLine();
System.out.println("Invalid option. Please Select 1, 2, or 3.");
intEntered = false;

}

//check to see if input was integer
} while(intEntered == false);


For Tebow's sake, please remove do/whiles from memory! Recursion baby!

public static void menu(){
System.out.print("Enter an int: " + "\n1. blah" + "\n2. blahh" + "\n3. blahhh\n");
}

public static void main() throws Exception {
menu();

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();

boolean isInt = Pattern.matches("^\\d*$", input);
if (!isInt){
System.out.print("ERROR: Not a valid int, fool!\n");
main();
}
}

CEH
02-18-2011, 09:39 AM
public static void main(String[] args)
{
private static Scanner scanner = new Scanner(system.in);
System.out.print("Would you like to: " + "\n1. Buy" + "\n2. Sell" + "\n3. End Your Trading");
System.out.println();

while (scanner.hasNextInt())
System.out.println('Thank you for " + scanner.nextInt().ToString(); );

}

OOJack
02-18-2011, 09:46 AM
public static void main(String[] args)
{
private static Scanner scanner = new Scanner(system.in);
System.out.print("Would you like to: " + "\n1. Buy" + "\n2. Sell" + "\n3. End Your Trading");
System.out.println();

while (scanner.hasNextInt())
System.out.println('Thank you for " + scanner.nextInt().ToString(); );

}



wrong, if it's a string the program just ends without user feedback

Irish Stout
02-18-2011, 10:18 AM
I thought this was going to be a discussion on coffee.

Fedaykin
02-18-2011, 12:06 PM
So the Mane is now a homework help network?