• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a type-safe for-loop to return min, max, and avg values  RSS feed

 
Zachary House
Greenhorn
Posts: 20
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to use a for loop to have a user enter 10 integers. The program also has to be type-safe (a.k.a. if a user enters a non-integer, the program won't crash).
If it is permitted, I also need to know how to get the minimum value of all the numbers that the user enters. If possible, I would like to use Math.min(). I don't want to touch arrays, if possible, because I haven't even begun to learn how to use them yet.


When I run this code, I get this output. *Note* I typed 10 every time for my input.
run:
run:
Please enter 10 more number(s).
10
Please enter 9 more number(s).
10
Please enter 8 more number(s).
10
Please enter 7 more number(s).
10
Please enter 6 more number(s).
10
Please enter 5 more number(s).
10
Please enter 4 more number(s).
10
Please enter 3 more number(s).
10
Please enter 2 more number(s).
10
Please enter 1 more number(s).
10
Minimum: 0
Maximum: 10
Average: 10.0
BUILD SUCCESSFUL (total time: 13 seconds)

However, when I type a non-integer, I get this output:
Please enter 10 more number(s).
g
Error! Invalid number. Try again.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
C:\Users\TookMyNameOut\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)


One a side note, while typing out this topic, I was able to solve 4 or 5 other problems with my code, all of which were going to be part of this topic. I am proud of myself 
 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possible solution would be to use

then use the Integer class and valueOf method
https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#valueOf-int-
wrapped in a try/catch block
https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, wrap the

line in a try/catch block and skip the Integer part altogether.
Zachary House wrote:One a side note, while typing out this topic, I was able to solve 4 or 5 other problems with my code, all of which were going to be part of this topic. I am proud of myself 

you should be, and I'm glad you were able to get most of it figured out.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a loop for the test about whether the input represents an int or not, rather than the if.
Alternative: find my post about Scanners and Streams; I had better not tell you which forum to look in, but it was within the last week or so. I shall let you go through the methods list to find a method which would limit your Stream to ten elements.

Beware with Scanners: System.in is implicitly always open, so you must find a way of terminating input without closing the Scanner.
 
Zachary House
Greenhorn
Posts: 20
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys so much! I finally got the code to work with your suggestions. Also, I was able to fix my issue with the minimum value.

Am I allowed to say that the Code Ranch is so much better than Stack Exchange? Here, the responses are constructive, friendly, and swift.
Whereas at Stack Exchange, I have seen several commentators deprecate newer coders that asked a question.
This forum and its experts are a major credit to the coding world, and both help to create one of my favorite learning environments.
 
Pete Letkeman
Ranch Foreman
Posts: 917
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary House wrote:Thank you guys so much! I finally got the code to work with your suggestions. Also, I was able to fix my issue with the minimum value.

I'm glad that you got a working solution.

Zachary House wrote:This forum and its experts are a major credit to the coding world, and both help to create one of my favorite learning environments.

I'm glad that you are happy with this site. And I agree that there are truly some experts on this site like Campbell Ritchie, which is something that one can inspire to be like some day.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary House wrote:Thank you guys so much! I finally got the code to work with your suggestions. Also, I was able to fix my issue with the minimum value. (...)

Beware, you have the same issue with the maximum value. Just type in 10 negative values.
How did you solve the minimum issue?
 
Zachary House
Greenhorn
Posts: 20
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, after having implemented a catch block, and examining my code more thoroughly, I found the issue with my original code. I commented out the catch block and rearranged parts of my code.
I was able to get the correct outputs with the following changes:
*Note* This is code I chose to use for this class, whereas I am using the catch block to fix errors in a different, more involved class.
  • I made the code prompt the user for a single int value before the loop, created a type-safe while-loop (will show you where I went wrong in the code that follows), and set average, minimum, and maximum equal to newInt.
  • Then the code would proceed onto the loop, which I changed to repeat only 9 times instead of 10.
  • Each time it repeat itself, it would run through the same type-safe while-loop I had earlier, and then use Math.Max(maximum, newInt) and Math.Min(minimum, newInt), and then add the new value in newInt to average. Afterwards, I would divide average by 10.


  •  
    Pete Letkeman
    Ranch Foreman
    Posts: 917
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you say that you have thoroughly tested it with large positive and large negative numbers and it works then I'll take your word on that.

    However are you now saying that there is an error and if so what is the error that you are experiencing?
     
    Zachary House
    Greenhorn
    Posts: 20
    C++ Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh, my apologies. There is no longer an error. I meant that I was moving the catch block that I created into a separate project for implementation, because I feel it would be best used there. I have tested the program with numbers that aren't too extreme (-1500<x<1500). It has performed as expected/desired.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How many times does the loop in line 16 run? Why aren't you running one loop 10×?
    You have done well with the loop guaranteed to find anint, () but most people would use next() in line 8 rather than nextLine(). You only need to get rid of one token at a time.
    It is customery to start the search for the smallest with the largest value, for the largest with the smallest value and for totals with 0. Most people would start withThat will get you out of the same loop running twice, and having to test for it being a proper int in two places. Look how similar lines 6‑11 are tor lines 19‑24 are

    You are right to get rid of the try‑catch; that loop will more‑or‑less guarantee ints as long as there is input still coming. Just about the only way you can not get ten ints is by stopping entering anything.
     
    Zachary House
    Greenhorn
    Posts: 20
    C++ Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The loop at line 16 will run 9 times since it will start with i equal to 0 and end at theend of the loop during which i will equal 0. Just before this loop however, I have the user enter a number that is both type-safe and value-safe. That completes the program's need for 10 integers.

    I didn't run just a single loop 10 times because, at the time, I did not know of Integer.MAX_VALUE and Integer.MIN_VALUE 
    I was forced to prompt the user for and int, the store the value in each of the instance variables min, max, and average for further use and comparison with other user-entered values, if that makes any sense.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!