• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone tell me what's wrong with this simple program?  RSS feed

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).

Here is my code:



And this is what the output looks like:

Actual output
-------------------------------------------
Enter an integer: 5
- 10
-
- Enter an integer: -4
- 8
- -6
-
- Enter an integer: 11
- -1
-
- Enter an integer: q
- -1
- -6

When it's supposed to look like this:

Expected output
-------------------------------------------
Enter an integer: 5
Enter an integer: 10
Enter an integer: -4
Enter an integer: 8
Enter an integer: -6
Enter an integer: 11
Enter an integer: -1
Enter an integer: q
11
-6


Could someone tell me what's wrong?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Welcome to the ranch.

Every time you call in.nextInt() it reads a number from the console.
You are calling in.nextInt() multiple times within your loop.



Presumably you only want to read ONE number from the user, and then do all the calculations with that same number.

 
Andrew Kenyon
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to nextInt() not behaving like you think it does, you forgot to handle the case when the current integer isn't a maximum or minimum. In that event, you still want a message on the console prompting the user to enter another integer. Note, I named my scanner object "keyboard" (force of habit), and declared temp right after min and max.

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Kenyon wrote:In addition to nextInt() not behaving like you think it does, you forgot to handle the case when the current integer isn't a maximum or minimum. In that event, you still want a message on the console prompting the user to enter another integer. Note, I named my scanner object "keyboard" (force of habit), and declared temp right after min and max.


In your example only max would be set if input only contained a single integer.
 
Andrew Kenyon
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you certain? When I tested it using the same numbers as in the OP, I got the expected result the OP was looking for.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Kenyon wrote:Are you certain? When I tested it using the same numbers as in the OP, I got the expected result the OP was looking for.

Try (in this order): 1, 2, 3, 4, 5
 
Andrew Kenyon
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Good catch. I'm afraid my fix for that looks a bit kludgy; I'd be interested in seeing a more elegant solution if anyone has one.



 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Drop the 'else'.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping me out guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!