0

I'm completely stuck on how to write my output to an output file. I've provided some info below to hopefully make myself more clear. Thanks to anyone that can help me.

Output when 1234 is the input (I need this to be written to the txt file):

Your original number was: 
1234
The reversed digits are: 
4321
The even digits are: 
2 4
The odd digits are:
1 3 

What is currently being written to the txt file:

Your original number was: 
1234

Here is my code right now:

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

    public class order {

    //Input validation. Allows any integer greater than 0. Any number starting 
    with 0 is invalid.

    private static void validate(Scanner input) { 

        String userEntry;       

        System.out.println("Please enter a positive integer greater than 0.");
        userEntry = input.nextLine();

        while (!userEntry.matches("^[1-9][0-9]*$")) {
            System.out.println("Invalid number. Try again");
            userEntry = input.nextLine();
        }

        try {
            FileWriter writer = new FileWriter("outDataFile.txt", true);
            PrintWriter outputfile = new PrintWriter(writer);

            outputfile.println("Your original number was: ");
            outputfile.println(userEntry);
            System.out.println("Your original number was: ");
            System.out.println(userEntry);

            System.out.println("The reversed digits are: ");
            reverse (input, userEntry);
            System.out.println();

            System.out.println("The even digits are: ");
            even (input, userEntry);
            System.out.println();

            System.out.println("The odd digits are: ");
            odd (input, userEntry);
            System.out.println();

            } catch (IOException e){            
        }            
    }

    //Reverses the order of digits.

    public static void reverse(Scanner keyboard, String intString) {

        int strsize = intString.length();
        int[] digits = new int[strsize];

        for (int i = 0; i < strsize; ++i)
        {
            String temp = Character.toString(intString.charAt(i));
            digits[i] = Integer.parseInt(temp);
        }

        for (int i = strsize - 1; i >= 0; --i)
        {
            System.out.print(digits[i]);
        }
    }

    //Extracts even numbers.
    public static void even (Scanner keyboard2, String intString2) {

        int strsize2 = intString2.length();
        int[] digits2 = new int[strsize2];

        for (int i = 0 ; i < strsize2 ; ++i) {

                String temp = Character.toString(intString2.charAt(i));
                digits2[i] = Integer.parseInt(temp);

                if(digits2[i] % 2 == 0) {
                System.out.print(digits2[i] + " ");
            }   
        }                                           
    }

    //Extracts odd numbers.

    public static void odd (Scanner keyboard3, String intString3) {

        int strsize3 = intString3.length();
        int[] digits3 = new int[strsize3];

        for (int i = 0 ; i < strsize3 ; ++i) {

                String temp = Character.toString(intString3.charAt(i));
                digits3[i] = Integer.parseInt(temp);

                if(digits3[i] % 2 != 0) {

                System.out.print(digits3[i] + " ");                             
            }   
        }                                           
    }

    //Allows continuous input.

    public static void main(String[] args) {                

        Scanner keyboard = new Scanner(System.in);

        boolean validate = true;            
        while (validate) {
        validate(keyboard);

        boolean invalid = true;                                                                     
        while (invalid) {
            System.out.println( "Would you like to try another calculation? [y/n]" );               

            Scanner input = new Scanner(System.in);
            char answer;
            String option = input.next();   

            if ("y".equalsIgnoreCase(option)) {                         
                invalid = false;
            }   
            else if ("n".equalsIgnoreCase(option)) {                    
                invalid = false;
                validate = false;
                    System.out.println( "Goodbye." );               
                    input.close();
            }
            else {                                                                                  
                System.err.println("The only valid answers are y/n.");
                System.out.println();
            }
            }
        }
    }
}   
Navi4458
  • 5
  • 4
  • You never use your `PrintWriter` to print anything other than the original number. Hence, this is all you see in the output file. – Tim Biegeleisen Apr 06 '17 at 05:48
  • 1
    Welcome to Stack Overflow! I would imagine that 95% of this code is not relevant to your question. Please create a [**Minimal**, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – Joe C Apr 06 '17 at 05:50
  • Is the output from `System.out` what you expect? If so, why not just pass your `PrintWriter` into your other methods and write in the same way as the first line? – petehallw Apr 06 '17 at 05:50
  • What else are you trying to write out? All the other things you print out? Or just some of them? – Michael Sharp Apr 06 '17 at 05:55
  • @petehallw Sorry, could you walk me through it a bit. I'm not sure how to do that. I'm actually still learning about passing objects through methods. – Navi4458 Apr 06 '17 at 05:59
  • @MichaelSharp Sorry I should have been more clear. I just edited the notes if you want to look at it again. I need the program to write out everything that i print out. Currently it's not printing the arrays. – Navi4458 Apr 06 '17 at 06:03
  • @Navi4458 Did my answer not help? – petehallw Apr 07 '17 at 07:27

2 Answers2

0

The problem is in your try block.

Incorrect:

outputfile.println("Your original number was: ");
        outputfile.println(userEntry);
        System.out.println("Your original number was: ");
        System.out.println(userEntry);

    System.out.println("The reversed digits are: ");
    reverse (input, userEntry);
    System.out.println();

    System.out.println("The even digits are: ");
    even (input, userEntry);
    System.out.println();

    System.out.println("The odd digits are: ");
    odd (input, userEntry);
    System.out.println();

Problem: You almost have to mirror the System.out.println's with outputfile.println's

Correct:

outputfile.println("Your original number was: ");
outputfile.println(userEntry);
System.out.println("Your original number was: ");
System.out.println(userEntry);

System.out.println("The reversed digits are: ");
reverse (input, userEntry);
System.out.println();
outputfile.println("The reversed digits are: ");
reverse (input, userEntry);
outputfile.println();


System.out.println("The even digits are: ");
even (input, userEntry);
System.out.println();
outputfile.println("The even digits are: ");
even (input, userEntry);
outputfile.println();

System.out.println("The odd digits are: ");
odd (input, userEntry);
System.out.println();
outputfile.println("The odd digits are: ");
odd (input, userEntry);
outputfile.println();
  • Oh no. I understand that. My problem right now is that my arrays aren't being written to the text file. I've edited examples of the output and what's being written. Hopefully I've made my problem a bit more clear. – Navi4458 Apr 06 '17 at 06:05
  • @Navi4458 maybe you have to add a method that names the odd numbers odds, or a method that names the even numbers evens, and write outputfile.println("The odd digits are: " + odds); and outputfile.println("The even digits are: " + evens); –  Apr 06 '17 at 06:08
  • @Navi4458 also a method for the reverse digits, sorry ;-) –  Apr 06 '17 at 06:09
  • i looked for similar posts, and found this. I understand that it's a little different, but here it is if it helps. http://stackoverflow.com/questions/13707223/how-to-write-an-array-to-a-file-java –  Apr 06 '17 at 06:16
0

I've tested the below code which works for writing the reverse number to the file. You can apply the same techniques to the other methods. Note I'm using PrintWriter.print as opposed to PrintWriter.println to get all the array elements on the same line.

public class Order {

    PrintWriter outputfile;
    FileWriter writer;

    public void validate(Scanner input) { 

        String userEntry;       

        System.out.println("Please enter a positive integer greater than 0.");
        userEntry = input.nextLine();

        while (!userEntry.matches("^[1-9][0-9]*$")) {
            System.out.println("Invalid number. Try again");
            userEntry = input.nextLine();
        }

        try {
            writer = new FileWriter("outDataFile.txt", true);
            outputfile = new PrintWriter(writer);

            outputfile.println("Your original number was: ");
            outputfile.println(userEntry);
            outputfile.flush();
            System.out.println("Your original number was: ");
            System.out.println(userEntry);

            System.out.println("The reversed digits are: ");
            outputfile.println("The reversed digits are: ");
            reverse (input, userEntry, writer);
            System.out.println();

        } catch (IOException e){            
        }            
    }

    //Reverses the order of digits.
    public void reverse(Scanner keyboard, String intString, FileWriter writer) {

        outputfile = new PrintWriter(writer);

        int strsize = intString.length();
        int[] digits = new int[strsize];

        for (int i = 0; i < strsize; ++i)
        {
            String temp = Character.toString(intString.charAt(i));
            digits[i] = Integer.parseInt(temp);
        }

        for (int i = strsize - 1; i >= 0; --i)
        {         
            System.out.print(digits[i]);
            outputfile.print(digits[i]);
            outputfile.flush();
        }
    }   
}
petehallw
  • 1,014
  • 6
  • 21
  • 49