0

So our professor has assigned us with a program which reads a text file he has provided us with; it sorts it, and creates a new file with the sorted stuff. He wants us to call three methods from the main i.e. read, sort, and write

I have done some of the work, but i'm confused what arguments to provide for io.sort. And how do i convert that text thing into an array to provide an argument Here's my code:

public void read() {
    try {
        Scanner myLocal = new Scanner(new File("dictionary.txt"));
        while (myLocal.hasNextLine()) {
            System.out.println(myLocal.nextLine());
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}

public void sort(String[] arr) {
    int n = arr.length;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            /* ERROR: When j == 0, j - 1 == -1, which is out of bounds */
            if (arr[j - 1].compareTo(arr[j]) > 0) { 
                swap(j, arr);
            }
        }
    }
}

public void swap(int j, String[] arr) {
    String temp = arr[j - 1];
    arr[j - 1] = arr[j];
    arr[j] = temp;
}

public void write() {
    try {
        PrintStream writer = new PrintStream(new File("sorted.txt"));
        for (int i = 0; i < 100; i++) {
            writer.println(i);
        }
        writer.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}
3
  • 2
    first step - instead of printing out the contents of the input file, store it in an array Commented Apr 10, 2018 at 5:04
  • i've posted the new read method below, as you told me. Commented Apr 10, 2018 at 5:25
  • somehow I have a feeling that your professor wants you to solve this assignement instead of the stackoverflow community Commented Apr 10, 2018 at 11:31

3 Answers 3

1

Here is how i've changed my read() method:

public void read()
{
    String[] myArray = new String[1000];
    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        String a = myLocal.nextLine();
        while (myLocal.hasNextLine()){
            for (int i=0; i<myArray.length; i++){
                myArray[i] = a;
            }                   

        }
    }
    catch(IOException e){
        System.out.println(e);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Now, the problem with this is that the scope of myArray is limited to the read method. Either add a return type of String[] or move the declaration of myArray to a field
Also you want String a = myLocal.nextLine(); to be in the while loop so that its value changes each time
You should change you initial question instead of creating a 'non' answer
1
class IO{
String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

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

public void sort()
{   
    int n = myArray.length;
    for (int i=0; i<n-1; i++){
        for(int j=0; j<n-i-1; j++){
            if(myArray[j+1].compareTo(myArray[j])<0){
                String temp = myArray[j];
                myArray[j] = myArray[j+1];
                myArray[j+1] = temp;
                //toLower
            }
        }

    }



}

public void swap(int j, String[] arr)
{

    String temp = arr[j-1];
    arr[j-1] = arr[j];
    arr[j] = temp;
}

public void write()
{

    try{
        PrintStream writer = new PrintStream(new File("sorted.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}

}

Comments

1

CORRECT CODE (SOLVED)

class IO{

String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

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

public void sort()
{   
    int n = myArray.length;

    for (int i=0; i<n; i++){
        for(int j=1; j<n-i; j++){ 
        if (myArray[j-1].compareTo(myArray[j])>0){



                swap(j, myArray);



            }

        }


    } 



}
public void swap(int j, String[] myArray)
{
    String temp = myArray[j-1];
    myArray[j-1]=myArray[j];
    myArray[j]=temp;


}

public void write()
{
    try{
        PrintStream writer = new PrintStream(new File("myIgnoreNew.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}

}

1 Comment

Try to get used to using try-with-resources for these kinds of read and write. Look it up, it's very straightforward to use and a good habit to have.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.