0

I am running into problems when it comes to the printing. when I run the program I get this:

TEST PROBLEM: 10 
0
0
0
0
0
0
0
0
10

I am only using the readNumbers() method at the moment (I have commented out the others). I keep running in to a problem where it makes comparisons and swaps. However, printing it out the numbers once there sorted comes out as the following on top.

File 1 contains: 10,1,2,3,4,5,6,7,8,9,10
file 2 contains: 10,10,9,8,7,6,5,4,3,2,1
file 3 contains: 10,3,9,10,8,2,7,5,1,4

My code:

import java.io.*;


   public class BubblesortA
    {
     public static void insertionSort (int [] data)
   {
  int comp=0;
  int swaps=0;

  int in, out, temp;

  for (out=1; out < data.length; out++)
  {
     temp = data[out];
     comp++;

     for (in=out; in > 0 && data[in-1] >= temp; in--)
     {
        data[in] = data[in-1];
        swaps++;
     }
     data[in] = temp;
  }

  System.out.println("Insertions sort comps: "+comp);
  System.out.println("Insertion sort swap: "+swaps);
  System.out.println();

}


   public static void selectSort (int [] data )
   {
  int in, out, min;
  int counter;
  int comparisons=0;
  int swaps=0;

  for (out=0; out < data.length-1; out++)
  {
     min = out;
     comparisons++;

     for (in = out+1; in < data.length; in++)
     {
        if (data[in] < data[min])
        {
           min = in; // new minimum

           swaps++;
        }
     }

     int tmp = data[out];     // swap items
     data[out] = data[min];   //
     data[min] = tmp;          //
    }

    System.out.println("Select sort Comp: "+comparisons);
    System.out.println("Select sort Swaps: "+swaps);
    System.out.println();





  }

  public static void generateRandom(int gty)
  {
     int counter;
     int [] fillArray = new int [gty];
     int random;


   for (counter=0; counter < fillArray.length; counter++)
  {
     random = (int) (Math.random() * 100+1);
     fillArray [counter]  = random;


  }

  bubbleSort(fillArray);
  selectSort(fillArray);
  insertionSort(fillArray);
}
   public static void readNumbers (String fileName)
 {
  int counter;
  int number;
  int  [] data = new int[fileName.length()];


  try
  {
     FileReader fr = new FileReader(fileName);
     BufferedReader br = new BufferedReader(fr);
     fileName = br.readLine();

     for(counter=0; counter < fileName.length(); counter++)
     {
        number = Integer.parseInt(fileName);
        data [counter] = number;

        System.out.println(" TEST PROBLEM: "+number+ " ");

        fileName = br.readLine();
     }
     br.close();
     fr.close();



     bubbleSort(data);



  }
  catch (IOException e)
  {
     System.out.println(e);
  }


 }
  public static void bubbleSort (int [] numbers)
 {
   int outter;
   int inner;
   int temp;
   int counter;
   boolean sorted = false;

   int comparisons=0;
   int swaps =0;


   while(!sorted)
   {
   for(outter=0; outter < numbers.length; outter++)
   {
     comparisons++;
     sorted = true;

     for (inner = 0; inner < numbers.length - 1 - outter; inner++)
     {
        if (numbers[inner] > numbers[inner + 1])
        {
           temp = numbers[inner];
           numbers[inner] = numbers[inner + 1];
           numbers[inner + 1] = temp;

           swaps++;
           sorted = false;
          }

       }
    }
  /**
     int hold = numbers.length -1;

     while (hold != 0 && sorted)
     {
        comparisons++;
        sorted = false;
        for (inner = 0; inner < hold; inner++)
        {
           if (numbers[inner] > numbers[inner + 1])
           {
              temp = numbers[inner];
              numbers[inner] = numbers[inner + 1];
              numbers[inner + 1] = temp;

              swaps++;
              sorted = true;
           }

        }


     }
   */


  }

  for(counter=0; counter < numbers.length; counter++)
  {
     System.out.println(numbers[counter]);
  }

  System.out.println();
  System.out.println("BubbleSort Comparsions: "+comparisons);
  System.out.println("BubbleSort Swaps: "+swaps);
  System.out.println();

 }

public static void main(String[] args) {

  int [] = {10,9,8,7,6,5,4,3,2,1,};
 
  String file1 = "File1.txt";
  String file2 = "File2.txt";
  String file3 = "File3.txt";


  //bubbleSort(data);

   readNumbers("File1.txt");
  //readNumbers("File2.txt");
  //readNumbers("File3.txt");
  //generateRandom(100);

   }//main
 }//class 
1
  • If you expected fileName.length() to be the number of lines of data in the file whose name is contained in the string variable fileName, you're going to be disappointed. Also, your loop depends on fileName.length(), but fileName is being altered inside the loop, so you never know when the loop will end. Use fileName for one thing and one thing only; use a different variable to read the lines of data. Jan 29, 2021 at 15:05

1 Answer 1

1

You have used same variable "fileName" as file-name and as the length of the content

int[] data = new int[fileName.length()]; //This is incorrect, it should be numbers count in the file

following method will work for u

public static void readNumbers(String fileName) {
    int counter;
    int number;
    int[] data = new int[11]; //EDIT 1

    try {
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
        fileName = br.readLine();

        for (counter = 0; counter < 11; counter++) { //EDIT 2
            number = Integer.parseInt(fileName);
            data[counter] = number;

            System.out.println(" TEST PROBLEM: " + number + " ");

            fileName = br.readLine();
        }
        br.close();
        fr.close();

        bubbleSort(data);

    } catch (IOException e) {
        System.out.println(e);
    }

}

File content 10 9 8 7 6 5 4 3 2 1 10

Suggestion: Instead of hardcoding 11, you should replace for loop with while loop and loop till the readLine() method not returns null

you can use below code to get number of lines in a file

private static int getLength(String fileName) {
    int count = 0;
    try {
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);

        while ((br.readLine()) != null) {
            count++;
        }

        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return count;
}
4
  • Omg, thank you. I am curious then. Say I needed to know how many numbers were in the file to pass it across to the array. How would I go about this, if this makes sense?
    – user450157
    Jan 29, 2021 at 15:24
  • Could you show me an example of the while loop being used for this particular case?
    – user450157
    Jan 29, 2021 at 15:24
  • added the code to get the length @user450157
    – Loki
    Jan 29, 2021 at 17:07
  • 1
    Thank you kindly Loki, you have no idea how much you have helped and for my future in learning as well. Big thank you!
    – user450157
    Jan 30, 2021 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.