Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Data Files

Tags:

java

I am writing a program in my AP java class and I am very confused. The program is supposed to read a data file using another program called EasyReader. The program consists of an Object class that finds the largest number, medium number, and smallest number. The Test class is supposed to read the data file (numbers.bat) that gives a set of numbers and the program is supposed to print the largest, medium, and smallest of all lines of the data file. However, it is reading only one line of the data file (the middle one) instead of all three. It is displaying

Largest = 7.3 Medium = 5 Smallest = 3.2

Please help. Thank you!

Here is the Object class:

public class Numbers 
{

    double small;
    double medium;
    double large;

    public Numbers(double A, double B, double C)
    {

      if(A>=B && A>=C)
      {
        large = A;


        if(B>=C && B<=A)
        {
          medium = B;

          small = C;
        }
        else
        {
          medium = C;
          small = B;
        }

      } 
      else if(B>=A && B>=C)
      {
          large = B;


            if(A>=C && A<=B)
            {
              medium = A;

              small = C;
            }
            else
            {
              medium = C;
              small = A;
            }


      } 

      else 
      {
          large = C;


            if(A>=B && A<=C)
            {
              medium = A;

              small = B;
            }
            else
            {
              medium = B;
              small = A;
            }

      }

    }



        public double large()
        {
            return large;

        }
        public double medium()
        {
            return medium;

        }
        public double small()
        {
            return small;

        }
    }   

Here is the Test class:

public class NumbersTest 
{

    public static void main(String[] args) 
    {

        EasyReader file = new EasyReader("numbers.dat");
        double value1 = file.readDouble();
        double value2 = file.readDouble();
        double value3 = file.readDouble();


        value1 = file.readDouble();
        value2 = file.readDouble();
        value3 = file.readDouble();




         Numbers nums = new Numbers(value1, value2, value3);

         System.out.println("Largest = " + nums.large()  + " Medium = " + nums.medium() + " Smallest = " + nums.small());
    }


}

And last the data file (numbers.bat)

4 9 2.5
3.2 5 7.3
12 8.2 9.1 

The output right now:

Largest = 7.3 Medium = 5.0 Smallest = 3.2

The expected output:

Largest = 12.0 Medium = 7.3 Smallest = 2.5
like image 998
Tristan Remus Avatar asked May 30 '26 16:05

Tristan Remus


1 Answers

Your numbers constructor only does anything if your value1 (A) is larger or equal to the other two. If it isn't it will leave small,medium and large at 0.

Edit: Expanding the answer here as it looks cleaner. So in your number constructor

public Numbers(double A, double B, double C)
{



            if(A>=B && A>=C)
            {
                large = A;


            if(B>=C && B<=A)
            {
                medium = B;

                small = C;
            }
            else
            {
                medium = C;
                small = B;
            }

        }

    }

You only have one main if statement checking if A is the largest, and the rest are nested into it. You would also need an if statement for if B or C are the largest.

public Numbers(double A, double B, double C)
{

  if(A>=B && A>=C)
  {
    large = A;


    if(B>=C && B<=A)
    {
      medium = B;

      small = C;
    }
    else
    {
      medium = C;
      small = B;
    }

  } else if if(B>=A && B>=C)
  {
    //B is the largest, add code to determine medium and small as you did before
  } else {
    //C is the largest, add code to determine medium and small as you did before
  }

}
like image 76
Linus Gudmundsson Avatar answered Jun 02 '26 06:06

Linus Gudmundsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!