Results 1 to 5 of 5
  1. #1
    Zerocool6667 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default 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 09:55 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  3. #3
    Zerocool6667 is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: Help with Program-input validation for negative numbers

    Hope that's better sorry i am new here.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  5. #5
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Help with Program-input validation for negative numbers

    You could do something like this ...

    Java Code:
    while (x < 0){
    // Output error message
    }

Similar Threads

  1. Replies: 9
    Last Post: 02-07-2014, 09:34 AM
  2. Math.sqRt and negative numbers
    By SkoodlyWah in forum New To Java
    Replies: 4
    Last Post: 07-19-2013, 10:07 AM
  3. forming negative numbers
    By TopNFalvors in forum New To Java
    Replies: 4
    Last Post: 04-11-2011, 01:38 AM
  4. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 05:32 AM
  5. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •