import java.util.*;
public class secondHighestNumber
{
public static int readData(int[] x)
{
System.out.println("Enter a sequence of up to " + x.length + " integers.");
System.out.println("Terminate the sequence with a value of -999.");
Scanner input = new Scanner(System.in);
int i;
for (i = 0; i < x.length; i++)
{
x[i] = input.nextInt();
if (x[i] == -999) break;
}
return i;
}
public static int secondHighest (int[] x)
{
int highest = x[0];
int secondhighest = x[0];
for (i = 1; i < x.length; i++)
{
if (x[i] > highest)
{
secondhighest = highest;
highest = x[i];
}
else if (x[i] > secondhighest)
{
secondhighest = x[j];
}
}
return secondhighest;
}
public static void main(String[] args)
{
int[] elements = new int[100];
int count = readData(elements);
int second;
second = secondHighest(elements);
System.out.println("The second highest value in the array: "+ second);
}
}
cannot find variable i error
Page 1 of 11 Replies - 554 Views - Last Post: 14 November 2011 - 12:18 PM
#1
cannot find variable i error
Posted 14 November 2011 - 12:16 PM
So the program's goal is to find the second highest value in an array that is supplied values through user input. My problem is that in the for loop an error in compilation keeps coming up telling me that it cannot find symbol - variable i. What am I doing wrong here?
Replies To: cannot find variable i error
#2
Re: cannot find variable i error
Posted 14 November 2011 - 12:18 PM
In your secondHighest() method you never define an integer 'i' for use in the loop.
In your other method where you do declare the integer, the scope of the variable is limited to that method only. You cannot access it from any other method.
In your other method where you do declare the integer, the scope of the variable is limited to that method only. You cannot access it from any other method.
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|