• Post Reply Bookmark Topic Watch Topic
  • New Topic

input validation for negative numbers  RSS feed

 
Johnnny Smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all! I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. Now for the life of me i have not been able to figure this out and could really use some expert help. I have included my current code, the program works perfectly, but i have no idea what to do about the negative numbers. Thanks!!!

package gradplanner;

import java.util.Scanner;

public class GradPlanner {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numofclasses = 0;
int totalCUs = 0;
System.out.print("Enter number of courses left in the degree program: ");
numofclasses = input.nextInt();

int numCU[] = new int[numofclasses];
for (int i = 0; i < numofclasses; i++) {
System.out.print("Input the number of CUs for course (" + (i + 1) + "): ");
numCU[i] = input.nextInt();
totalCUs += numCU[i];

}

System.out.println("Total total number of CUs for all courses: " + totalCUs);


double plannedCUs = 0;
do {
System.out.print("How many CUs you are planning to take each term: ");
plannedCUs = input.nextInt();
if (plannedCUs < 12 || plannedCUs > totalCUs) {
System.out.println("Take each term with a minimum of 12 CUs being selected and upto total");
}
} while (plannedCUs < 12 || plannedCUs > totalCUs);

double numTermsToCompletion = Math.ceil(totalCUs / plannedCUs);
System.out.println("Number of terms to completion: " + numTermsToCompletion);
System.out.println("Tuition cost based on number of terms to completion: $" + (numTermsToCompletion * 2890));
System.out.println("Number of months to completion: " + (numTermsToCompletion * 6));

}

}
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome!

You might find this article useful.

Also, when posting code, please UseCodeTags (<- click this).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first few steps are always the same.

1) turn off your computer.
2) get pencils, paper, and erasers.
3) start THINKING about the problem. Do NOT think about Java.
4) write down the steps as you would give them to a third party
5) revise those steps, making them simpler and more explicit
6) repeat step 5 until you could hand the directions to a young child and expect them to be able to follow them without help from anyone

ONLY when you have done all the above should you consider writing a line of code.

So...how would YOU, using only your brain, accomplish the task?
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pay close attention to the posts from Campbell and Fred -- priceless advice contained there.

As for your question: You are quite close to figuring it out. You are already checking if the CUs is less than 12. And, if my math is correct, negative numbers are less than 12 (hard to be certain with the new math they teach you kids these days.) If you need a specific check for negative numbers, add a check for CUs < 0 before your code to check for < 12.

Regards,
Robert
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW...this is how your code looks if you use code tags. Next time, paste in your code, highlight it all and click the 'code' button you'll see above:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!