Having trouble compiling. What am I doing wrong?
Having trouble compiling. What am I doing wrong?
/* method accepts parameters (array, max and min) and returns count
* of how many integers fall between max and min.
*/
import java.util.*;
import java.io.*;
public class RangeCount2{
public static final int Length = 10;
public static void main (String [] args){
Scanner console = new Scanner(System.in);
Scanner input = getInfo(console);
processFile(input);
Scanner num = new Scanner(System.in);
System.out.print(“Maximum Number: “);
int max = console.nextInt();
System.out.print(“Minimum Number: “);
int min = console.nextInt();
}
public static Scanner getInfo(Scanner console){
Scanner input = null;
while (input == null){
System.out.print(“input file name? “);
String name = console.nextLine();
try {
input = new Scanner(new File(name));
} catch (FileNotFoundException e) {
System.out.println(“File not found. Please try again.”);
}
}
return input;
}
public static void processFile(Scanner input){
int[] Array = new int [Length];
while (input.hasNextLine()){
String text = input.nextLine();
int[] Array1 = transferFrom(text);
}
}
public static int[] transferFrom(String text){
Scanner data = new Scanner (text);
int[] result = new int [Length];
int i = 0;
while (data.hasNextInt()){
result[i] = data.nextInt();
i++;
}
countInRange(result, max, min);;
}
public static void countInRange (int[] result, int max, int min){
int count = 0;
for (int x : result) {
if (x <= max && x >= min){
count++;
}
}
System.out.println(“There is/ are ” + count + ” elements whose values fall”);
System.out.println(“between ” + min + ” and ” + max + “.”);
}
}
Compile error:
RangeCount2.java:60: cannot find symbol
symbol : variable max
location: class RangeCount2
countInRange(result, max, min);;
^
RangeCount2.java:60: cannot find symbol
symbol : variable min
location: class RangeCount2
countInRange(result, max, min);;
^
2 errors
Tool completed with exit code 1
Best answer:
Twice you’re referencing a variable called “max” without having defined such a variable.
You did define a variable called “max” in your main() method, but it’s just a local variable; it doesn’t exist outside the context of main().
Since all your methods are static (and you therefore can’t use instance variables) you probably want to ask the user for min and max before you process the file (you’ve got it the other way around in your main() method) then pass those values to the other functions as parameters.
Tags: compiling., doing, having, trouble, wrong
Under Forum

1 Comment for Having trouble compiling. What am I doing wrong?
1. armyman1122 | January 31st, 2008 at 10:44 am
your problem is in this line of code
countInRange(result, max, min);
if you look where the method is located, it does not have any values assigned to either max or min at that scope. Result does have a value since within the scope of this method call, it gets the array of data.nextint
you may also want to watch your return type as it will probably expect an int return type
Leave a Comment for Having trouble compiling. What am I doing wrong?
Trackback this post | Subscribe to the comments via RSS Feed