DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24

    error variable not initialized

    import java.io.*;
    public class PP1 {
    public static int getGoodInt(BufferedReader kbd) {
    int n;
    boolean ok=false;
    while ( !(ok==true)) {
    try {
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));
    String instring = br.readLine();
    n = Integer.parseInt(instring);
    ok=true;

    }
    catch (IOException e)
    {
    System.out.println("Input/output error");
    }
    catch (NumberFormatException e)
    {
    System.out.print(n +" is not an int -- try again:");
    //error variable n might not have been initialized
    }
    }
    return n; // same thing as the other
    }
    }
    help
    thanks

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    You're variable n, is given a value within a loop, and also within a try/catch statement. There is a possibility that it never gets initialized, so when you try to return it, it will give you an error.

    PS. Rather than doing !(ok == true), you can do ok == false, or ok != true, or the "best" way, !ok.

    Hope this helps.

  3. #3
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24
    I tried taking it out of the loop but that just gives me a whole new list of errors. what sould I do to get it initialized. I know this might be a dumb question but I am new to java. just started reading a good java book introduction to problem solving and programming
    thanks

    here is the code that I am writing the getGoodInt method for hope if this helps

    // Reads in an int n, then n ints, prints them, their max, min, ave out
    // Checks for NumberFormatException and corrects...
    import java.io.*;
    public class PP1drvr {
    public static void main(String[] args) throws IOException {
    BufferedReader kbd = new BufferedReader(
    new InputStreamReader(System.in));
    boolean ok;
    String numStr;
    // get number of entries
    System.out.print("Give the number of int to read: ");
    int num = PP1.getGoodInt(kbd);
    // get the entries
    int[] nums = new int[num];
    for (int i=0; i<num; i++) {
    System.out.print("Enter the "+i+"th int: ");
    nums[i] = PP1.getGoodInt(kbd); }
    // print the answers
    int sum = nums[0];
    int min = nums[0];
    int max = nums[0];
    for (int i=1; i<num; i++) {
    sum += nums[i]; min = Math.min(min,nums[i]);
    max = Math.max(max,nums[i]); }
    float ave = ((float)sum)/num;
    System.out.println("The numbers are");
    for (int i=0; i<num; i++)
    System.out.print(" "+nums[i]);
    System.out.println();
    System.out.println("The largest number is "+max);
    System.out.println("The smallest number is "+min);
    System.out.println("The average is "+ave);
    }
    }

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Just set it to something right in the beginning. This way it's guarenteed to have a value:
    Code:
    int n = 0;
    Last edited by destin; 02-13-2006 at 08:54 PM.

  5. #5
    Join Date
    Oct 2005
    Location
    Chicago
    Posts
    24
    thanks it works. and thanks for the help with !ok

Similar Threads

  1. Applet not initialized
    By trixma in forum Java
    Replies: 2
    Last Post: 02-22-2005, 04:33 PM
  2. Replies: 1
    Last Post: 12-09-2002, 03:27 PM
  3. Replies: 1
    Last Post: 09-16-2002, 09:53 AM
  4. Strings Not Being Initialized
    By Kevin Baker in forum .NET
    Replies: 3
    Last Post: 10-01-2001, 11:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links