Madness7337
Madness7337

Reputation: 3

Why is an int required? Java Error: Found double, required int

I'm working on a code to read from a text file and display the maximum of the numbers. Using MaxArray[] the for statement is throwing an error because the input numbers are doubles and it is expecting an int. Any tips on what can be used to find the max of an array containing doubles?

  import java.io.*;

public class Read {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Rayman7337\\IdeaProjects\\Exam2\\src\\Data.txt"));

            String Num_1 = br.readLine();
            String Num_2 = br.readLine();
            String Num_3 = br.readLine();
            String Num_4 = br.readLine();
            String Num_5 = br.readLine();

            double Convert_1 = Double.parseDouble(Num_1);
            double Convert_2 = Double.parseDouble(Num_2);
            double Convert_3 = Double.parseDouble(Num_3);
            double Convert_4 = Double.parseDouble(Num_4);
            double Convert_5 = Double.parseDouble(Num_5);

            double MaxArray[] = {Convert_1, Convert_2, Convert_3, Convert_4, Convert_5};

            double Max = MaxArray[0];

            for(double i = 0; i<=4; i++){
                if (MaxArray[i] > Max) Max = MaxArray[i];
            }
            System.out.println("The maximum number is: " + Max);

        }catch (Exception ex){
            return;
        }

    }
}

Upvotes: 0

Views: 212

Answers (3)

Praveen Gowthaman
Praveen Gowthaman

Reputation: 87

Array indexes should not be double . Kindly check the difference with below code .

  import java.io.*;

public class Read {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Rayman7337\\IdeaProjects\\Exam2\\src\\Data.txt"));

            String Num_1 = br.readLine();
            String Num_2 = br.readLine();
            String Num_3 = br.readLine();
            String Num_4 = br.readLine();
            String Num_5 = br.readLine();

            double Convert_1 = Double.parseDouble(Num_1);
            double Convert_2 = Double.parseDouble(Num_2);
            double Convert_3 = Double.parseDouble(Num_3);
            double Convert_4 = Double.parseDouble(Num_4);
            double Convert_5 = Double.parseDouble(Num_5);

            double MaxArray[] = {Convert_1, Convert_2, Convert_3, Convert_4, Convert_5};

            double Max = MaxArray[0];

            for(int i = 0; i<=MaxArray.length; i++){
                if (MaxArray[i] > Max) Max = MaxArray[i];
            }
            System.out.println("The maximum number is: " + Max);

        }catch (Exception ex){
            return;
        }

    }
}

Upvotes: 0

vsfDawg
vsfDawg

Reputation: 1527

Array indices are integers so any type used as an array index must be safely cast to an int. To solve your immediate problem, change the type of your for-loop index accordingly.

In addition, you can begin iteration on the second element of the array because you initialize max to the value of maxArray[0]. The traditional form of the for-loop condition in this pattern is to use less-than-array-length. Java naming convention is for variables to generally begin with lowercase letters. With all that in mind...

double max = maxArray[0];
for (int i = 1 ; i < maxArray.length ; ++i) {
  if (maxArray[i] > max) {
    max = maxArray[i];
  }
}

Once you understand how this works, you could use a Stream to do it more succinctly.

double max = Arrays.stream(maxArray).max().getAsDouble();

Upvotes: 0

bytesculptor
bytesculptor

Reputation: 613

All you need to do is replace

for(double i = 0; i<=4; i++){

with

for(int i = 0; i<=4; i++){

The position in the array can only be an integer value, the value at this array position is still double as you defined.

double MaxArray[]

Further, I would replace <=4

for(int i = 0; i < MaxArray.length; i++){

So you don't have to update the limit if you add/remove a value to the array.

Upvotes: 1

Related Questions