Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Unanswered - Uncompressing file program Not working

So in my file i have two lists, one if for the unique words which will be used to make the whole sentence and the other is the compressed sentence which has been compressed to the positions of the words in the unique words list. The file looks like this:

Is, this, a, simple, sentence, 
1, 2, 3, 4, 5, 3, 4, 5, 4, 

I want my program to use the unique words and re create the original sentence which was: Is this a simple sentence a simple sentence is simple. However, my code does not work the way i want it to work and so it is not uncompressing the sentence. Here is my code:

public static void main(String[] args) {
String compressed = "";
String compressed1 = "";
int Index1;
String orig = "";
int num1=0;
int num2=0;
BufferedReader br;
        try{
            br = new BufferedReader (new FileReader("CompletedWork.txt"));
            try {
                    String Uni = br.readLine();
                    System.out.println(Uni);  //outputs the content of the first line in the file
                    String Uni1 = Uni.replaceAll(",", "");  //gets rid of the commas in the unique words
                    String Uni2 = Uni1.toLowerCase();  //changes the case to lowercase
                    String[] ArrUni = Uni2.split(" ");  //turns the string into a string array
                    System.out.println(ArrUni[0]);  // checks that the array splits into words


                    compressed = br.readLine();  //read next line
                    System.out.println(compressed);  //outputs the compressed 
                    compressed1 = compressed.replaceAll(",", ""); //removes the punctuation from the compressed sentence.
                    System.out.println(compressed1);
                    String[] ArrComp = compressed1.split(" ");
                    System.out.println(ArrComp[2]);

                    for(num1=0; num1 < ArrComp.length; num1++) { //starts the loop
                        for(num2=0; num2 < ArrComp.length; num2++) {
                            if (ArrComp[num1].equals(num2)) {
                            orig = orig.concat(ArrUni[num2]);
                        }else{
                        }

                       }}
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            System.out.println(e);  //outputs an error message if the file is not present
            e.printStackTrace();



    }
        System.out.println(orig);
} 

}

I have all of the imports just to clarify. The string Orig should be outputted but it is not and it should contain the original sentence. Here is the output to the console:

run:
Is, this, a, simple, sentence, 
is
1, 2, 3, 4, 5, 3, 4, 5, 4, 
1 2 3 4 5 3 4 5 4 
3

BUILD SUCCESSFUL (total time: 0 seconds)

As you can see, there is a space after the 3 which is where Orig is outputted but nothing is outputted. I hope someone can help me solve this. If you need anymore information to solve, just ask. FYI i'm pretty new to java.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem is your if statement. You are comparing a string (ArrComp[num1]) with a number (num2) which will always return false.

It should be:

if (Integer.parseInt(ArrComp[num1]) == num2) {
    orig = orig.concat(ArrUni[num2 - 1] + " ");
}

Also note the - 1 when getting the word to avoid an ArrayIndexOutOfBoundsException, and the space between the words.

And if you want:

Is this a simple sentence a simple sentence is simple

Then your compressed sentence has to be:

1, 2, 3, 4, 5, 3, 4, 5, 1, 4,

You can also make your other code efficient or at least easier to read by using a regular expression to split the strings on a comma and get rid of extra space in one line, like this:

yourString.split("\\s*,\\s*");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!