#How to sort the words in my file alphabetically?

1 messages · Page 1 of 1 (latest)

abstract dragon
#
static String readSortAlpha() {
        String data1 = null;

        String[] wordArr = null;
        int[] indicesArr = null;

        try {
             File myObj1 = new File("fox.txt_words.txt");
             Scanner myReader1 = new Scanner(myObj1);

            while (myReader1.hasNextLine()) {
                 data1 = myReader1.nextLine();
                 System.out.println(data1);

                for (int j = 0; j < data1.length(); j++) {

                }
             }
         } catch (IOException e) {
             System.out.println("An error occurred.");
             e.printStackTrace();
         }
         return data1;
    }
boreal wrenBOT
#

This post has been reserved for your question.

Hey @abstract dragon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

abstract dragon
#

How to sort the words in my file alphabetically?

abstract dragon
#

Specifically, I struggling taking these words in the file, and placing them in an array

mellow idol
#

after you get your data you have to sort it. The document is telling you to use buble sort if you don't care about your grade or merge sort for full credit.

you can also do Collections.sort(dataList) but I don't think your teacher will like that

#

I see you are printing each line. Do you get any output in the console?

abstract dragon
abstract dragon
mellow idol
#

ok that means you are reading the file correctly and all that's left is to add it to an array
I would assume you are not limited to only primitive arrays?

abstract dragon
#

I’m not allowed to use .collections either

mellow idol
#

you have two arrays declared. Do you need the two or?

#

String[] wordArr and int[] indecesArr

abstract dragon
mellow idol
#

sorry I missed that. ok

abstract dragon
#

when I use the .charat() method, I only get the first letter of the words in the string and .split() only prints out the memory address

mellow idol
#

So first we need to initialize to arrays. We can't leave them equal as null.
Give it a big size like 100-1000 or more so we don't run into issues with size.

it would look something like
String[] wordArr = new String[100]
int[] indecesArr = new int[100]

#

the charAt() method returns only 1 letter cuz that's what char is and I guess the default is 0 which is the first letter.
with split() you should be getting an array of Strings.

#

so instead having data1 be just String. it should be an array as well. (String[] data1 = new String[2]) it's fine to hard code that value in but if the input file changes we may get unexpected behavior
For each line we split we should get two Strings. (except the first line) The word and the number.

#

Here is how they use it on geeksforgeeks

    public static void main(String args[])
    {
        String str
            = "GeeksforGeeks:A Computer Science Portal";
        String[] arrOfStr = str.split(":");
 
        for (String a : arrOfStr)
            System.out.println(a);
    }
}```
#

and the output

GeeksforGeeks
A Computer Science Portal```
#

so now all that's left is to insert that inside the correct arrays we had

#

to do that we need to keep an index value which helps us know till where we are in those arrays

#

so insert a int index = 0; above the while loop

abstract dragon
#

I’m following and testing it out while you type. So far, so good

mellow idol
#

so in the while loop we want to insert the values and increment the index

#

could you tell me what data[1] is returning in the console?

#

so just insert a print(sout) data[1] somewhere in the while loop

abstract dragon
#

i'm getting a null

#

I made changes: static String[]

#

And deleted data1 = myReader1.nextline()

#

They were causing errors

mellow idol
#
        //Remove this we will instantiate it inside the while loop
        //String data1 = null;

        String[] wordArr = new String[100];
        int[] indicesArr = new int[100];

        try {
             File myObj1 = new File("fox.txt_words.txt");
             Scanner myReader1 = new Scanner(myObj1);

            while (myReader1.hasNextLine()) {
                //We read the line. Then we split it on space
                 String[] data1 = new String[]
                  data1 = myReader1.nextLine().split(" ");
                 System.out.println(data1[1]);
             }
         } catch (IOException e) {
             System.out.println("An error occurred.");
             e.printStackTrace();
         }
         return data1;
    }```
#

it's causing errors cuz you weren't splitting it. So you are just returning 1 string not an array of strings.

#

And deleted data1 = myReader1.nextline()
you are getting null cuz you aren't reading the file anymore

#

just an fyi this problem will be solved without much effort using a map. I hate problems that limit the usage of certain things.

#

A problem we might run into now is that first line in the file which is just a number
so we have to check each String inside data1 if it is a number or not

#

that's the reason I'm asking you to tell me what data[1] prints

abstract dragon
#

we can use map

#

the error says to create a local variable

mellow idol
#

oh my bad. the hell am I doing kekw

#

do ```
String[] data1 = new String[]
data1 = myReader1.nextLine().split(" ");

abstract dragon
#

Now, I have an error for the return value

mellow idol
#

just make the method return void for now and remove the return

#

we can always change it later

abstract dragon
#

sorry, to be bothering you. i'm just making corrections and still running into errors

mellow idol
#

print the 0th element

abstract dragon
#

progress

mellow idol
#

ok as expected that number there will make us check each time if we have a number or a string

abstract dragon
#

i assumed that

mellow idol
#

now we just loop through data1 and check if it's a number and assign it to the correct array

#

here you can see a few ways to do that

abstract dragon
#

Thanks for the help. I’ll work on this tomorrow because I’m heading to work and won’t clock out until midnight

boreal wrenBOT
boreal wrenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

abstract dragon
#

@mellow idol are you on?

opal drum
#

Note that your instructions literally tell you to expect a number on the first line, and that number tells you how many words there are in the file

mellow idol
#

I read the whole thing a few times

#

dunno where my head was 💀

abstract dragon
#

I actually made some progress. I was able to separate the words from the indices, place them into their own arrays, and incorporate the Merge Sort. Now, when I pass in the array from separateWordsIndex() into my Merge Sort, I receive Null.


opal drum
#

Surely you understand that you can't have 1 indice and 9 words? This is not how to read the data

#

Also, you seemed to be returning only the words array, not the indices array, so I'm not sure what you were doing but that couldn't work

abstract dragon
#

I misspoke. I was able to print the words and the indices separate but was having trouble returning them

opal drum
#

But what are those indices you're speaking of? Can we see the file?

abstract dragon
opal drum
#

... The data file

abstract dragon
#

It was a text that contained this.

#

I’m not able to get the exact file bc I’m not near my laptop but the most recent code and screenshot of the text file is already in this thread

opal drum
#

Oh, that was the actual file. I see. Then yeah, you need to get these data. But the first line is the number of words

#

And you're not keeping the words and the indices in sync in the two arrays when reading them

#

Which doesn't matter much since the array of indices doesn't exist outside of the method that reads it

opal drum
# abstract dragon I misspoke. I was able to print the words and the indices separate but was havin...

Ah sorry, I didn't see you mentioned you have trouble returning them.
Well, admittedly, it's difficult to adapt to.
You can't return more than one thing. A better approach would be to create the arrays before calling the method, pass them to the method precreated, and have the method fill them. However, that means that you need to know the array sizes before calling the method, so you need to read the first line before calling the method. So you need to open the file outside of the method, read the number of words so you know the arrays' size, then call the method with the arrays to fill and the Scanner already open on the file.

boreal wrenBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

abstract dragon
opal drum
#

The first line, the 9 alone. That's the number of lines

abstract dragon
opal drum
#

.... No? That's a file you need to read. This file gives you a number. You can't use a .length thing in this circumstance

abstract dragon
#

wait, I know hasNextLine() and nextline() reads the words in the file but you said "number", I thought you meant the length of each line for some reason

opal drum
#

No, I meant the 9 at the start of the file