-1

I am currently working on an assignment and my task is to read an input of a couple of sentences on multiple lines and print out the words in two columns in an output file. I have completed the assignment for the most part however the program is writing the second word of some lines in another column. Does anybody know what is going on?

Here is the text of the input file:

This summer I will be going to training. I will be going to training for PLTW CSP. It will be two weeks long. It should be in san jose. I love pizza! I also like to eat burgers. I want to die. The square root of 81 is 9. I want to play checkers. I also would like to play chess. Heaven would be something I aspire to go to. The square root of 64 is 8. I cannot wait to eat my pizza. Samsung just released a new phone I have an LG G7. Atom is my editor of choice. I need to get a new tv. *Note: The first line ends at "in san jose."

Here is the code:

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    File inputFile = new File("input.txt");
    Scanner in = new Scanner(inputFile);
    PrintWriter out = new PrintWriter("output.txt");

    //in.useDelimiter("[^A-Za-z]+");
    String input = "";
    while (in.hasNextLine()) {
      int count = 0;
      input = in.nextLine();
      input = input.replace(".", "");
      input = input.replace("!", "");

      Scanner lineScanner = new Scanner(input);
      String words = "";
      while (lineScanner.hasNext()) {
        count++;
        words = lineScanner.next();
        out.printf("%10.10s", words);
        if (count % 2 == 0) {
          out.println();
        }
      }
    }
      out.close();
  }
}

This is the output:

     This    summer
         I      will
        be     going
        to  training
         I      will
        be     going
        to  training
       for      PLTW
       CSP        It
      will        be
       two     weeks
      long        It
    should        be
        in       san
      jose         I      love
     pizza         I
      also      like
        to       eat
   burgers         I
      want        to
       die       The
    square      root
        of        81
        is         9
         I      want
        to      play
  checkers         I
      also     would
      like        to
      play     chess
    Heaven     would
        be something
         I    aspire
        to        go
        to       The    square
      root        of
        64        is
         8         I    cannot
      wait        to
       eat        my
     pizza   Samsung      just
  released         a
       new     phone
         I      have
        an        LG
        G7      Atom        is
        my    editor
        of    choice
         I      need
        to       get
         a       new
        tv
LLK-11
  • 55
  • 5

1 Answers1

1

The problem is when while loop goes to the next iteration, count always resets to 0, and two words will always be added on the same line (because of if (count % 2 == 0) condition). But at that time you could already have one word on the same row from previous loop iteration (in that case, from the previous sentence).

There are a lot of ways to fix it. For example, you can move int count = 0; out of while loop:

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    File inputFile = new File("input.txt");
    Scanner in = new Scanner(inputFile);
    PrintWriter out = new PrintWriter("output.txt");

    //in.useDelimiter("[^A-Za-z]+");
    String input = "";
    int count = 0;
    while (in.hasNextLine()) { 
      input = in.nextLine();
      input = input.replace(".", "");
      input = input.replace("!", "");

      Scanner lineScanner = new Scanner(input);
      String words = "";
      while (lineScanner.hasNext()) {
        count++;
        words = lineScanner.next();
        out.printf("%10.10s", words);
        if (count % 2 == 0) {
          out.println();
        }
      }
    }
      out.close();
  }
}

Then the output will be:

      This    summer
         I      will
        be     going
        to  training
         I      will
        be     going
        to  training
       for      PLTW
       CSP        It
      will        be
       two     weeks
      long        It
    should        be
        in       san
      jose         I
      love     pizza
         I      also
      like        to
       eat   burgers
         I      want
        to       die
       The    square
      root        of
        81        is
         9         I
      want        to
      play  checkers
         I      also
     would      like
        to      play
     chess    Heaven
     would        be
 something         I
    aspire        to
        go        to
       The    square
      root        of
        64        is
         8         I
    cannot      wait
        to       eat
        my     pizza
   Samsung      just
  released         a
       new     phone
         I      have
        an        LG
        G7      Atom
        is        my
    editor        of
    choice         I
      need        to
       get         a
       new        tv
amseager
  • 5,795
  • 4
  • 24
  • 47
  • Thank you! I had a feeling it was because of this however I tried resetting count while it was still in the while loop and results were not any better. Thanks again! – LLK-11 Mar 13 '19 at 21:38