The Orange Mane -  a Denver Broncos Fan Community  

Go Back   The Orange Mane - a Denver Broncos Fan Community > Orange Mane Discussion > Orange Mane Central Discussion
Register FAQ Members List Calendar Chat Room Mark Forums Read



Reply
 
Thread Tools Display Modes
Old 02-17-2011, 12:53 PM   #1
SportinOne
Ring of Famer
 
SportinOne's Avatar
 

Join Date: Mar 2005
Posts: 3,794
Default Anyone Speak Java?

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.

Last edited by SportinOne; 02-17-2011 at 01:23 PM..
SportinOne is offline   Reply With Quote
Sponsored Links
Old 02-17-2011, 12:57 PM   #2
Hercules Rockefeller
Ring of Famer
 
Hercules Rockefeller's Avatar
 

Join Date: May 2001
Posts: 16,246

Adopt-a-Bronco:
DJ Williams
Default

I speak Jive
Hercules Rockefeller is offline   Reply With Quote
Old 02-17-2011, 01:03 PM   #3
bronco militia
OMG...this is horrible!
 
bronco militia's Avatar
 
THE GREATEST

Join Date: Apr 2001
Location: colorado springs, co
Posts: 24,657
Default

bronco militia is offline   Reply With Quote
Old 02-17-2011, 01:08 PM   #4
jhns
Ring of Famer
 
but you still can't C me!

Join Date: Jul 2006
Location: Omaha
Posts: 12,362

Adopt-a-Bronco:
None
Default

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.

Last edited by jhns; 02-17-2011 at 01:16 PM..
jhns is offline   Reply With Quote
Old 02-17-2011, 01:20 PM   #5
SportinOne
Ring of Famer
 
SportinOne's Avatar
 

Join Date: Mar 2005
Posts: 3,794
Default

Quote:
Originally Posted by jhns View Post
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:

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.
SportinOne is offline   Reply With Quote
Old 02-17-2011, 01:37 PM   #6
rovolution
Ring of Famer
 
rovolution's Avatar
 

Join Date: Jul 2006
Location: Colorado Springs, CO
Posts: 2,945

Adopt-a-Bronco:
None
Default

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?
rovolution is offline   Reply With Quote
Old 02-17-2011, 01:38 PM   #7
SportinOne
Ring of Famer
 
SportinOne's Avatar
 

Join Date: Mar 2005
Posts: 3,794
Default

Quote:
Originally Posted by rovolution View Post
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.
SportinOne is offline   Reply With Quote
Old 02-17-2011, 01:41 PM   #8
jhns
Ring of Famer
 
but you still can't C me!

Join Date: Jul 2006
Location: Omaha
Posts: 12,362

Adopt-a-Bronco:
None
Default

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.

Last edited by jhns; 02-17-2011 at 01:44 PM..
jhns is offline   Reply With Quote
Old 02-17-2011, 01:43 PM   #9
SportinOne
Ring of Famer
 
SportinOne's Avatar
 

Join Date: Mar 2005
Posts: 3,794
Default

Quote:
Originally Posted by jhns View Post
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!
SportinOne is offline   Reply With Quote
Old 02-17-2011, 02:06 PM   #10
jhns
Ring of Famer
 
but you still can't C me!

Join Date: Jul 2006
Location: Omaha
Posts: 12,362

Adopt-a-Bronco:
None
Default

Quote:
Originally Posted by SportinOne View Post
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.
jhns is offline   Reply With Quote
Old 02-17-2011, 05:22 PM   #11
McDman
Ring of Famer
 
McDman's Avatar
 
Huzzah!

Join Date: Jul 2006
Posts: 4,636

Adopt-a-Bronco:
None
Default

"Waki mallya kuna chu chu? " oh wait, that's what Jabba speaks.
McDman is offline   Reply With Quote
Old 02-17-2011, 06:26 PM   #12
BroncosSR
Perennial Pro-bowler
 

Join Date: Jan 2006
Location: Philadelphia, PA
Posts: 780
Default

.

Last edited by BroncosSR; 02-17-2011 at 06:40 PM..
BroncosSR is online now   Reply With Quote
Old 02-17-2011, 06:38 PM   #13
BroncosSR
Perennial Pro-bowler
 

Join Date: Jan 2006
Location: Philadelphia, PA
Posts: 780
Default

.

Last edited by BroncosSR; 02-17-2011 at 06:40 PM..
BroncosSR is online now   Reply With Quote
Old 02-17-2011, 07:15 PM   #14
UltimateHoboW/Shotgun
Don't piss off Manning.
 
UltimateHoboW/Shotgun's Avatar
 
Time to believe!

Join Date: Dec 2004
Location: Gensis Planet
Posts: 6,426

Adopt-a-Bronco:
C.J. Anderson
Default



?
UltimateHoboW/Shotgun is offline   Reply With Quote
Old 02-17-2011, 08:13 PM   #15
broncocalijohn
Famer of Rings
 
broncocalijohn's Avatar
 
I said Do It!

Join Date: Oct 2004
Location: Lake Forest, Orange County, Calif.
Posts: 21,971

Adopt-a-Bronco:
Simon Fletcher
Default

I know Jack talk Thai. Jack talk Thai very well.
broncocalijohn is offline   Reply With Quote
Old 02-17-2011, 08:52 PM   #16
OABB
sarcasm font
 
OABB's Avatar
 
always on.

Join Date: Mar 2007
Location: Los angeles
Posts: 7,341

Adopt-a-Bronco:
Knowshon Moreno
Default

Damn.... Just came here to make an airplane jive joke.


You guys are good.
OABB is offline   Reply With Quote
Old 02-17-2011, 09:41 PM   #17
worm
Great moments are born...
 
worm's Avatar
 
...from great opportunity

Join Date: Dec 2002
Posts: 3,902

Adopt-a-Bronco:
#20
Default

Quote:
Originally Posted by SportinOne View Post
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.
worm is offline   Reply With Quote
Old 02-18-2011, 09:21 AM   #18
OOJack
Go Broncos!
 
OOJack's Avatar
 
Go Broncos!

Join Date: Aug 2005
Location: Denver
Posts: 529

Adopt-a-Bronco:
Plummer
Default

Quote:
Originally Posted by SportinOne View Post
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);
For Tebow's sake, please remove do/whiles from memory! Recursion baby!
Code:
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();
	}
}
OOJack is offline   Reply With Quote
Old 02-18-2011, 09:39 AM   #19
CEH
Ring of Famer
 

Join Date: Mar 2004
Posts: 3,954
Default

Code:
 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();   );   
           
    }

Last edited by CEH; 02-18-2011 at 09:41 AM..
CEH is offline   Reply With Quote
Old 02-18-2011, 09:46 AM   #20
OOJack
Go Broncos!
 
OOJack's Avatar
 
Go Broncos!

Join Date: Aug 2005
Location: Denver
Posts: 529

Adopt-a-Bronco:
Plummer
Default

Quote:
Originally Posted by CEH View Post
Code:
 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
OOJack is offline   Reply With Quote
Old 02-18-2011, 10:18 AM   #21
Irish Stout
Ring of Famer
 
Irish Stout's Avatar
 
Run for it Marty!

Join Date: Nov 2005
Location: Colorado
Posts: 3,891

Adopt-a-Bronco:
Wesley Woodyard
Default

I thought this was going to be a discussion on coffee.
Irish Stout is offline   Reply With Quote
Old 02-18-2011, 12:06 PM   #22
Fedaykin
Ring of Famer
 

Join Date: Nov 2006
Posts: 4,940

Adopt-a-Bronco:
None
Default

So the Mane is now a homework help network?
Fedaykin is offline   Reply With Quote
Reply

Thread Tools
Display Modes



Forum Jump


All times are GMT -7. The time now is 02:12 AM.


Denver Broncos