Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Compile error - empty string?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Compile error - empty string?

    I'm writing basically my first program for school. I've written small ones, following instructions, but this is the most vague. I'm having issues. I can't figure out what the error means. I'm not done with the code, but I think the ArrayList is throwing me off. I'm trying to gather user input and sum the total. Here's the code:

    package graduationplanner;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.lang.Double;
     
    public class GraduationPlanner {
     
        public static void main(String[] args) {
           Scanner in = new Scanner(System.in);
           System.out.println("Welcome to the Western Governor's University Graduation Planner! \nWe will calculate the number of terms you have left until graduation, \nalong with the amount of tuition you have left to pay. \nWe will also provide the number of months until graduation! \nLet's get started by asking a few questions.");
           System.out.println("How many CU's are required for your degree?"); //Prompts user for the total number of CU's for degree and stores in variable
           double totalCU = in.nextDouble(); //storing user input
           if (totalCU <= 0){ //verification on positive input by user
               System.out.println("That's not right! Enter a number higher than zero!");
               System.out.println("How many CU's are required for your degree?");
               totalCU = in.nextDouble(); //stores corrected number
           }
           //System.out.println(totalCU); //Outputting number entered for check
     
     
           double sum = 0;
     
            ArrayList<Double> leftCU = new ArrayList<>();
            {
                System.out.println("Please enter the number of CU's for each class that is left to complete, Q to quit:");
                double number = 0;
                number = Integer.parseInt(in.nextLine());
                if (number <= 0);
                {
                    System.out.println("The number of credits must be greater than zero!");
                    System.out.println("Please enter the number of CU's for each class that is left to complete, Q to quit.");
                                }
                while (in.hasNextDouble()) {
                    leftCU.add(in.nextDouble());
                }
                for (int i = 0; i < leftCU.size(); i++) {
                    sum =+ leftCU.get(i);
                }
                System.out.println("Total credits remaining: " + sum);
            }
     
           System.out.println("And what's the number of CU's left for just this term?"); //Prompts user for CUs left for term
           double cuLeftTerm = in.nextDouble(); //storing user input
           if (cuLeftTerm < 0) {
               System.out.println("Please enter a positive number please.");
               System.out.println("And what's the number of CU's left for just this term?");
               cuLeftTerm = in.nextDouble(); //stores corrected number
           }
     
           final double tuitionCost = 2890; //stores value for tuition cost per term
           final double termMonths = 6; //stores value for number of months in a term
           final double minCUs = 12; //stores value for number of CUs required in a term
     
           double termLeft = sum/minCUs; //Calculates number of terms left
           double totalTuition = termLeft * tuitionCost; //Calculates the amount of tuition left to pay
           double monthsLeft = termLeft * termMonths; //Calculates the number of months left
     
           System.out.println("+----------------------------------------+");
           System.out.println("|Number of Terms left is: " + termLeft + "|");
           System.out.println("|Total amount of Tuition left is: " + totalTuition + "|");
           System.out.println("|Number of Months left is: " + monthsLeft + "|");
           System.out.println("+----------------------------------------+");
           System.out.println("Thank you for using the WGU Graduation Planner");
           }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Compile error - empty string?

    Can you copy and paste the error message?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,845
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Compile error - empty string?

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Compile error - empty string?

    Anything you declare constant using 'final' should be written in uppercase.

    public final int LIVES = 3;

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    769
    Thanks
    0
    Thanked 145 Times in 128 Posts

    Default Re: Compile error - empty string?

    Quote Originally Posted by Ada Lovelace View Post
    Anything you declare constant using 'final' should be written in uppercase.

    public final int LIVES = 3;

    Wishes Ada xx
    I would argue with that.
    It depends whether the variable is also static or not. If the variable is both static and final then its a constant and should be all uppercase with underscores in between words.
    But if the variable is not static then there is no reason to make it uppercase, it might simply be an immutable attribute.

  6. The Following User Says Thank You to Cornix For This Useful Post:

    copeg (July 2nd, 2014)

  7. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Compile error - empty string?

    Quote Originally Posted by dicdic View Post
    Can you copy and paste the error message?
    output:

    Welcome to the Western Governor's University Graduation Planner!
    We will calculate the number of terms you have left until graduation,
    along with the amount of tuition you have left to pay.
    We will also provide the number of months until graduation!
    Let's get started by asking a few questions.
    How many CU's are required for your degree?
    50
    Please enter the number of CU's for each class that is left to complete, Q to quit:
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at graduationplanner.GraduationPlanner.main(Graduatio nPlanner.java:27)
    Java Result: 1

  8. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Compile error - empty string?

    double number = 0;
    number = Integer.parseInt(in.nextLine());
    Your parsing an integer - but look what you declared

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Replies: 3
    Last Post: November 30th, 2013, 04:52 PM
  2. compile error
    By silverpen10 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: January 12th, 2013, 10:15 AM
  3. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  4. Error while compile
    By when im gone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 7th, 2012, 09:53 AM
  5. Compile Error, tried everything please help
    By cdub0 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 03:31 AM