Hey guys, I'm having a bit of trouble with my program. It seems that out.print(translationText) only prints out the last line in theStringBuilder translationText. It's really werid, because System.out.print(translationText) prints out everyline from the StringBuilder translationText.

I added append to be true for the FileOutputStream object, but now it just adds the rest onto the file, without keeping the format of each line. I understand the problem with the PrintStream overwriting the file each time, but I'm not sure how to fix it.

Input:
ABC
DEF

Current Output (when written to a text file only):
3,33,333,

Current Output w/Append (when written to a text file only):
2,22,222,3,33,333

Expected Output (when written to a text file only):
2,22,222,
3,33,333,

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

public class P2 {
  
  //****** MAIN*********//
/* Tells the user what the program does, just to be user-friendly. Then it takes the first and second user entered argument for the input file location and output file location and turns it into a string, then passes it into the readFileInput method. */

  public static void main(String[] args) throws Exception {
    System.out.println("This is a text to keypad converter.  It will translate your text into cellphone keypad numbers.  Below is the translation.");
    
    String fileInputArg = args[0];
    String fileOutputArg = args[1];
    readFileInput(fileInputArg, fileOutputArg);
  }
  
  //****** METHODS*********//
  /*-> readFileInput Method:
This method takes the first argument from the user for the location of the file containing the text needing to be translated.  It takes the string input from main and opens the file using java.io.File class, then scans the document with the Scanner class.  The while loop will loop until the end of the document, and inside the while loop turns the content of the file into a String that is then put into the translateFileInput method below.n*/

  public static void readFileInput(String fileNameInput, String fileOutputArg) throws Exception {
    File fileContent = new File(fileNameInput);
    Scanner input = new Scanner(fileContent);
    
    while (input.hasNext()){
      String fileText = input.next();
      translateFileInput(fileText, fileOutputArg);
    }
  }
  
/* -> translateFileInput
Translates the inputed string file from the readFileInput method.  This method takes the string in as a varible and puts it into a char array using the toCharArray method.  It will then loop through each letter stored in the array, and compare it to the case statements.  Using StringBuilder, I was able to mutate a string, and used the append command.  Each time a case matches and is run, the corresponding number is appended onto the main string.  It then calls writeFileOuput  and gives it the fileOutputArg for the filename and the translationText which is the output content. */

public static void translateFileInput(String text, String fileOutputArg) throws Exception {
    StringBuilder translationText = new StringBuilder();
    char[] lettersToNumbers = text.toUpperCase().toCharArray();
    for (int i=0; i<lettersToNumbers.length; i++){
      switch (lettersToNumbers[i]) {
        case ' ':
          translationText.append("0,");
          break;
        case 'A':
          translationText.append("2,");
          break;
        case 'B':
          translationText.append("22,");
          break;
        case 'C':
          translationText.append("222,");
          break;
        case 'D':
          translationText.append("3,");
          break;
        case 'E':
          translationText.append("33,");
          break;
        case 'F':
          translationText.append("333,");
          break;
        case 'G':
          translationText.append("4,");
          break;
        case 'H':
          translationText.append("44,");
          break;
        case 'I':
          translationText.append("444,");
          break;
        case 'J':
          translationText.append("5,");
          break;
        case 'K':
          translationText.append("55,");
          break;
        case 'L':
          translationText.append("555,");
          break;
        case 'M':
          translationText.append("6,");
          break;
        case 'N':
          translationText.append("66,");
          break;
        case 'O':
          translationText.append("666,");
          break;
        case 'P':
          translationText.append("7,");
          break;
        case 'Q':
          translationText.append("77,");
          break;
        case 'R':
          translationText.append("777,");
          break;
        case 'S':
          translationText.append("7777,");
          break;
        case 'T':
          translationText.append("8,");
          break;
        case 'U':
          translationText.append("88,");
          break;
        case 'V':
          translationText.append("888,");
          break;
        case 'W':
          translationText.append("9,");
          break;     
        case 'X':
          translationText.append("99,");
          break;     
        case 'Y':
          translationText.append("999,");
          break;     
        case 'Z':
          translationText.append("9999,");       
          break;      
      } 
    }
    writeFileOutput(fileOutputArg, translationText);
  } 
  
  /* -> writeFileOutput:
Takes the fileOutputArg (the filename) and the translationText (which is the computed output from translateFileInput) and prints it to the system.out and to the text file specified from fileOutputArg. */

  public static void writeFileOutput(String fileOutputArg, StringBuilder translationText) throws Exception {
    PrintStream out = new PrintStream(new FileOutputStream(fileOutputArg,true)); //Append true 
    System.out.println(translationText);
    out.print(translationText);
    out.close();
  }
}

Recommended Answers

All 3 Replies

Each time is created a new instance of PrintStream out -line 129
You need create this instance only once (without this method, and put it as an additional method parameter).

When you use System.out, you are using println, which prints the string and then a new line. But when you use out to print to the file, you are only using out.print (line 131). Change this to out.println to get each line of text on a new line.

When you use System.out, you are using println, which prints the string and then a new line. But when you use out to print to the file, you are only using out.print (line 131). Change this to out.println to get each line of text on a new line.

Thanks! I just figured that out a second before I found your post. Even though this doesn't really "solve" the problem (still making several objects), it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.