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;
}
#How to sort the words in my file alphabetically?
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @abstract dragon! Please use
/closeor theClose Postbutton 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.
How to sort the words in my file alphabetically?
Specifically, I struggling taking these words in the file, and placing them in an array
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?
I understand that. Every time I try to put the words in an array, I can only grab the first letter of*** the word
Yes, the output is exactly how the words appear in the file
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?
I believe I am
I’m not allowed to use .collections either
you have two arrays declared. Do you need the two or?
String[] wordArr and int[] indecesArr
yes, that's apart of the approach
sorry I missed that. ok
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
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
I’m following and testing it out while you type. So far, so good
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
i'm getting a null
I made changes: static String[]
And deleted data1 = myReader1.nextline()
They were causing errors
//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
oh my bad. the hell am I doing 
do ```
String[] data1 = new String[]
data1 = myReader1.nextLine().split(" ");
just make the method return void for now and remove the return
we can always change it later
sorry, to be bothering you. i'm just making corrections and still running into errors
print the 0th element
progress
ok as expected that number there will make us check each time if we have a number or a string
i assumed that
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
Thanks for the help. I’ll work on this tomorrow because I’m heading to work and won’t clock out until midnight
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤 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.
@mellow idol are you on?
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
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.
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
I misspoke. I was able to print the words and the indices separate but was having trouble returning them
But what are those indices you're speaking of? Can we see the file?
The code is in here: message.txt
... The data file
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
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
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.
💤 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.
what do you mean by "read the number of words"?
The first line, the 9 alone. That's the number of lines
so basically use a .length method?
.... 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
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
No, I meant the 9 at the start of the file