Jack Sena
Jack Sena

Reputation: 1

Errors in Java code

I'm trying to get the highest and lowest number entered by user. I got this. I just new in programming. There are 3 errors.

import java.io.*;
public class HighestToLowest
{
    public static void main(String []args)throws IOException{
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));        

        double[] input = new double[8];
        int index;
        int highIndex = 0;
        int lowIndex = 0;
        double sum = 0;

        System.out.println("Enter The Scores Of Judges: ");
        for (index = 0; index<8; index++){
             System.out.print("Enter The Score" + (index + 1) + ": ");
             input[index] = Double.parseDouble(dataIn.readLine()); 
         }

         for (index = 1; index < 8; index++)
             if(input[highIndex] < input[index])
                highIndex = index;


         for (index = 1; index < 8; index++)
             if (input[lowIndex] > input[index])
                 lowIndex = index;

         for (index = 0; index < 8; index++)
             sum = sum + input[index];
         try
            {
                input[index] = Double.parseDouble(dataIn.readLine());
            }
            catch(IOException e)
            {
                System.out.println("error");
            }

            if(sum>index)
            {
                sum=highIndex;
            }

            if(sum>=index)
            {
                index=lowIndex;
            }                        
        }

        System.out.print("Highest is: + highIndex");
        System.out.print("Lowest is: + lowIndex");

         System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 



    }
}

Upvotes: 0

Views: 194

Answers (1)

Alpine
Alpine

Reputation: 3858

Although you say there are only 3 errors, there seen to be a bit more

public static void main(String []args)throws IOException{ //<- ?
{//<- why two curly brackets?

In for loop

for (index = 0; index < 8; index++){//<- curl bracket missing?
         sum = sum + input[index];
         try {
              input[index] = Double.parseDouble(dataIn.readLine());
          } catch(IOException e) {
              e.printStackTrace();
         }

         if(sum>index) {
              sum=highIndex;
          }
          if(sum>=index){
              index=lowIndex;
           }                        
     } // <- or extra curl bracket?

This line will print Highest is: + highIndex

  System.out.print("Highest is: + highIndex");

Any thing in " " is printed as it is.

So change it to

 System.out.print("Highest is:" + highIndex);

Same applies for

  System.out.print("Lowest is: + lowIndex");

Are you from C programming, this line is correct

System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 

In Java it can also be written as

 System.out.println("The Contestant Receives a total of " +  (sum - highIndex - lowIndex));

Upvotes: 1

Related Questions