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 5 of 5

Thread: Initializing

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Initializing

    Here is my code, with the following error that I get, I dunno why its trying to make me initialize a String:

    import java.util.Scanner;

    public class Lab7_Ex2
    {

    public static void main(String [] args)
    {

    Scanner keyboard = new Scanner(System.in);

    int numberCount = 0;
    String numberString;
    int number;
    int total = 0;
    double average = 0.0;

    System.out.print("Enter the first number, or 9999 to exit: ");
    number = Integer.parseInt(numberString);

    while (number !=9999)
    {
    total += number;
    numberCount++;

    System.out.print("Enter a number" + (numberCount + 1) + " or 9999 to exit: ");
    number = Integer.parseInt(numberString);
    }

    if (numberCount > 0)
    {
    average = (double) total / numberCount;
    System.out.print("The average of the numbers is: " + average + ".");
    }

    System.exit(0);
    }
    }
    -------------------------------------------------------------------------------------------------------------------

    ----jGRASP exec: javac -g Lab7_Ex2.java
    Lab7_Ex2.java:21: variable numberString might not have been initialized
    number = Integer.parseInt(numberString);
    ^
    1 error

    ----jGRASP wedge: exit code for process is 1.
    ----jGRASP: operation complete.


  2. #2
    Junior Member Krumpir's Avatar
    Join Date
    Jul 2012
    Location
    Cape Town, South Africa
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Initializing

    Change String numberString; to String numberString = 200;
    Check out my site -> tssolutions.net16.net

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Initializing

    Quote Originally Posted by Krumpir View Post
    Change String numberString; to String numberString = 200;
    If you had thought about it a little more, or if you had taken the trouble to actually test it before posting, you would realize that your suggestion won't compile.

    To the Original Poster: It's OK to declare a String without giving it an initial value, but Java won't let you use it in your program until you have somehow given it a value.

    Didn't you intend to use your keyboard Scanner object to read something into the String before parsing it into an int?


    Cheers!

    Z
    Last edited by Zaphod_b; July 26th, 2012 at 01:05 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Initializing

    The reason the compiler is complaining is because you never once initialized `numberString`. I suspect you intended to initialize it with something like:
    numberString = keyboard.next();
    ... but unfortunately you never did.
    Besides, given you're utilizing `Scanner`, you are able to skip the `Integer.parseInt()` call and the intermediate `numberString` field completely by calling `Scanner.nextInt()` directly, along with `Scanner.hasNextInt()` to determine whether there is any more input. You can see below for an example of how that would work:

    import java.util.Scanner;
     
    public final class Lab7_Ex2 {
     
      public static void main(String[] argv) {
        final Scanner input = new Scanner(System.in);
     
        int a = 0;
        int n = 1;
     
        System.out.print("Enter the first number, or 9999 to exit: ");
        int i;
        while (input.hasNextInt() && (i = input.nextInt()) != 9999) {
          a += i;
          System.out.print("Enter number " + ++n + ", or 9999 to exit: ");
        }
     
        if (--n > 0) {
          System.out.printf("The average of the numbers is: %#.2f\n", ((double) a) / n); 
        }
      }
    }
    Last edited by veeer; July 29th, 2012 at 03:54 PM. Reason: Adding explanation

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Initializing

    veeer, please read the following:
    Forum Rules
    http://www.javaprogrammingforums.com...n-feeding.html
    Your post above has been edited.

Similar Threads

  1. Help with initializing Lists
    By Jumbosize in forum Collections and Generics
    Replies: 1
    Last Post: May 28th, 2012, 02:47 PM
  2. [SOLVED] Not initializing
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 30th, 2011, 04:32 AM
  3. Initializing Variables
    By Tjstretch in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: October 29th, 2011, 12:37 PM
  4. Nested-If Problem/ Initializing
    By ak120691 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 08:37 PM
  5. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 04:49 PM