-
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
-
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.
-
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);
}
}
-
Just set it to something right in the beginning. This way it's guarenteed to have a value:
Last edited by destin; 02-13-2006 at 08:54 PM.
-
thanks it works. and thanks for the help with !ok
Similar Threads
-
Replies: 2
Last Post: 02-22-2005, 04:33 PM
-
By raj74 in forum ASP.NET
Replies: 1
Last Post: 12-09-2002, 03:27 PM
-
By Elmosca in forum ASP.NET
Replies: 1
Last Post: 09-16-2002, 09:53 AM
-
By Kevin Baker in forum .NET
Replies: 3
Last Post: 10-01-2001, 11:34 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks