1

I deleted my last post because I did'nt even ask what I wanted toask so sorry about that. Anyways, This is regarding my last Question. The assingment wants me to send data to an outputfile that looks like this:

EXample Input File:

ABC54301 TFTFTFTT TFTFTFFTTFT

Example Output File:

ABC54301 TFTFTFTTSTFTFTFFTTFT 77.5% C
                         X    X   X X X

So in My outputData Method I need to Change the Print and Println Statements so they send it it to the output file. Here is my outputData Method and Main Method

public static void outputData(String[][] studentTests, StringBuilder[][] studentResults, double[] testScores)
  {
    for(int rowIndex = 0; rowIndex < studentTests.length; rowIndex++)
    {
      for(int colIndex = 0; colIndex < studentTests[rowIndex].length; colIndex++)
      {
        if(colIndex == 0)
        {
          System.out.print(studentTests[rowIndex][colIndex] + " ");
        }
        else
        {
          System.out.println(studentTests[rowIndex][colIndex]);
        }
      }
      for(int colIndex = 0; colIndex < studentTests[rowIndex].length; colIndex++)
      {
        if(colIndex == 0)
        {
          System.out.print(studentResults[rowIndex][colIndex] + " ");
        }
        else
        {
          System.out.println(studentResults[rowIndex][colIndex]);
        }
      }
    }
  }

Here is the Main Method

public static void main(String[] args)throws IOException
  {
    inFile = new Scanner(new FileReader("GradesIn.dat"));
    outFile = new PrintWriter("GradesOut.dat");

    String testAnswers = inFile.next();

    int numberOfStudents = inFile.nextInt();
    double[] testScores = new double[numberOfStudents];

    String[][] studentTests = new String[numberOfStudents][2];
    StringBuilder[][] studentResults = new StringBuilder[numberOfStudents][2];    

    initializeResults(studentResults);
    getData(studentTests);
    gradeTests(studentTests, studentResults, testAnswers, testScores);    
    outputData(studentTests, studentResults, testScores);

    outFile.close();
    inFile.close();
  }

So if someone could help me NOT give me the code to make it where it will send the data to the output file and put the Grade Percentage and LetterGrade out to the side of the T's and F's in the output file. Thanks I am open to anything.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • Ive read through the Basic I/O Tutorial didnt really help – user3605613 May 07 '14 at 05:23
  • 1
    You sure you read the basic I/O tutorial? You need to make use of `outFile` to write to `GradesOut.dat` file. I would recommend you to check this out: [How to use PrintWriter and File classes in Java?](http://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java) – wns349 May 07 '14 at 05:26
  • outputting to a `.dat` file workd just like with any other file :) the file ending is just a convention and has nothing special to say. – Theolodis May 07 '14 at 05:28

2 Answers2

0

PrintWriter#print(String s) method will print your string and PrintWriter#println(String s) method will print the string and terminates the line as quoted from java docs

PrintWriter#println(String s): Prints a String and then terminates the line. This method behaves as though it invokes print(String) and then println().

With that said, if you want to print a data in single row you would use print(String) method appended with a SPACE and after writing last column you should use println(String) in order to write your String and change the row.

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

You need to change three (general) things,

First, the method outputData needs to take in your PrintWriter outFile -

outputData(outFile, studentTests, studentResults, testScores); // <-- pass it in.

Second, update the method definition -

public static void outputData(PrintWriter pw, String[][] studentTests, 
    StringBuilder[][] studentResults, double[] testScores) // <-- add to signature

Third, change the print() calls

// System.out.print(studentTests[rowIndex][colIndex] + " ");
pw.print(studentTests[rowIndex][colIndex] + " ");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • what about the println statements or were you meaning change both of them to pw.print? – user3605613 May 07 '14 at 05:35
  • Every call to System.out.print (or System.out.println) should be changed to a corresponding `pw.print` or `pw.println` call (assuming you want the output to change from `System.out` to the file). – Elliott Frisch May 07 '14 at 05:37
  • ok I did all that now im going to sound stupid but the part "outputData(outFile, studentTests, studentResults, testScores); // <-- pass it in." Where exactly is this needed at at first I though that was the method so all I did was add the outFile in the method heading. but then I took it out and added that whole line right before the For Loop – user3605613 May 07 '14 at 05:42
  • That goes directly after `gradeTests(studentTests, studentResults, testAnswers, testScores);`. New to Java I take it? – Elliott Frisch May 07 '14 at 05:45
  • Yes I am. This was our first assingment also this is off topic but in my folder i have the java file and the input and output.dat files. So we use Putty to compile and what not. but i think i have to do some kid of command like java GradesOut.dat or something. My professor is most likely asleep – user3605613 May 07 '14 at 06:03