Results 1 to 5 of 5
- 02-20-2014, 09:25 PM #1
Member
- Join Date
- Feb 2014
- Posts
- 2
- Rep Power
- 0
Help with Program-input validation for negative numbers
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!!!
Java Code: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)); } }
Last edited by Zerocool6667; 02-20-2014 at 10:55 PM.
- 02-20-2014, 09:41 PM #2
Re: Help with Program-input validation for negative numbers
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.If you don't understand my response, don't ignore it, ask a question.
- 02-20-2014, 10:56 PM #3
Member
- Join Date
- Feb 2014
- Posts
- 2
- Rep Power
- 0
Re: Help with Program-input validation for negative numbers
Hope that's better sorry i am new here.
- 02-20-2014, 11:02 PM #4
Re: Help with Program-input validation for negative numbers
A little better, but now the code has lost all its indentations. Nested statements should be indented.
If you don't understand my response, don't ignore it, ask a question.
- 02-20-2014, 11:16 PM #5
Member
- Join Date
- Jan 2014
- Posts
- 97
- Rep Power
- 0
Similar Threads
-
Adding and Subtracting positive or negative two numbers of any length with strings
By beginner234 in forum New To JavaReplies: 9Last Post: 02-07-2014, 10:34 AM -
Math.sqRt and negative numbers
By SkoodlyWah in forum New To JavaReplies: 4Last Post: 07-19-2013, 11:07 AM -
forming negative numbers
By TopNFalvors in forum New To JavaReplies: 4Last Post: 04-11-2011, 02:38 AM -
Mod of Negative Numbers
By Venny in forum New To JavaReplies: 7Last Post: 01-28-2011, 06:32 AM -
Help with negative numbers
By cela0811 in forum New To JavaReplies: 2Last Post: 01-04-2011, 06:04 PM
Bookmarks