• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating double input when it is an array  RSS feed

 
Abe Gill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. This is my first post here. I'm a beginning student in Java. I made the mistake of taking this class during the Summer and the accelerated pace may have caused some things to go over my head.
This question is in regards to a homework assignment I'm doing so I don't need code, just pointing me in the right direction would be much obliged. We don't have tutors here at my school so.. internet = life saver.
Here's my problem.

I'm trying to validate the input from the user, the data type is a double. I want to make it so that the input can only be a positive number. I also want it to ask the user for the input again if the input is not valid. I know I need to use a do-while loop but my problem is, I'm trying to validate each element of an array. I don't know if I'm making sense so let me show you what I've got so far.




I tried using the "i" variable from the for loop but it keeps giving me an error. It doesn't give me an error if I put in an actual number but than it doesn't catch the error. Any ideas? Thanks again guys. I'm really new so I'm sorry if the solution is a no-brainer. It blows my mind the possibilities with programming and I can't wait until the course is over so I can concentrate on building some applications. I know I'm going off on a tangent here, but what do you guys think about learning python? Or should I just stick with Java until I finish my degree?
 
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
Nesting of do-while and for loops are reversed. The for loop should be the outer loop and the do-while should surround what is currently lines 16 & 17.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to think through what your loop is doing and what your final output should be. A few thoughts from my side.

You want to have the use enter a double value 12 times. Do you want to check every single input or do you want to check the final input. If you would like your user enter a value and then only continue when the use enters a valid value, you want to have your do-while loop inside your for loop (what the previous poster already mentioned)
Since scanner.nextDouble() method will wait until the user submits a double, I think it is a bit redundant to check if rainfall[0] stays less then zero. Besides if you declare an double array of length 12, you probably know that it will be instantiated with a default value? So it would be worth looking to the check on rainfall[0] < 0.
Another thing that comes up in my mind: Why would you check if an value in your array is less then zero? I assume that you would check the value before you put it in your array. But this also has to do with the first point about the placing of the different loops.
 
Abe Gill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome guys. I got it. This is my updated code. It validates each input and displays an error message.



It makes sense to me logically now. The "Do" must be in the "For" because the for loop while loop around, and it will "Do" it again while each input is less than 0. It needs to be in the For loop for it to repeatedly validate it. You guys are awesome!
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, nice to hear that you solved your problem.

Personally I would check the user input before putting the value in the array. That would be something like: If (userinput >= 0) then {put the value in the array and break the do-while loop}

Especially when you create an application where you store your data somewhere this will avoid that unwanted values being stored. Because what should happen when a user enters an unwanted value and terminate the program?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need for nested loops - you can use something like this:

Note that you should also handle the case where the user types in gibberish instead of a positive number.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:There is no need for nested loops - you can use something like this:

Well, that really all depends on the specs, doesn't it?  If the specs say "Keep prompting user until they enter a non-negative number", then this solution doesn't meet that requirement.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Well, that really all depends on the specs, doesn't it?  If the specs say "Keep prompting user until they enter a non-negative number", then this solution doesn't meet that requirement.

What you might could do then is declare a variable outside the while loop e.g. In the condition of the while loop you can add this as an extra condition e.g. Within the loop you can set the variable to false at the start of the loop and once the input is valid you can set it to true. In this case I think it should be possible to avoid the nested loop.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!