2017-04-06 91 views
0

我完全停留在如何將輸出寫入輸出文件。我在下面提供了一些信息,希望能讓我更清楚。感謝任何能夠幫助我的人。如何將數組寫入文件?

當1234是輸入(我需要這被寫入到txt文件)輸出:

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

什麼目前正在寫入TXT文件:

Your original number was: 
1234 

這裏是我的代碼現在:

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(); 
      } 
      } 
     } 
    } 
} 
+0

您從不使用您的'PrintWriter'來打印除原始數字之外的任何東西。因此,這就是您在輸出文件中看到的所有內容。 –

+1

歡迎來到Stack Overflow!我會想象這95%的代碼與你的問題無關。請創建一個[**最小**,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –

+0

來自'System.out'的輸出是你期望的嗎?如果是這樣,爲什麼不把你的'PrintWriter'傳遞給其他方法,並以與第一行相同的方式寫入? – petehallw

回答

0

我已經測試了下面的c ode用於將反向編號寫入文件。您可以將相同的技術應用於其他方法。注意我正在使用PrintWriter.print而不是PrintWriter.println來獲取同一行上的所有數組元素。

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(); 
     } 
    } 
} 
+0

謝謝。這有很多幫助。 – Navi4458

+0

@ Navi4458太棒了!沖洗'PrintWriter'是非常重要的一步。如果您對此感到滿意,您可以將答案標記爲已接受嗎? – petehallw

0

問題出在您的try區塊。

錯誤:

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(); 

問題:你幾乎有'與outputfile.println小號的

正確反映System.out.println

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(); 
+0

哦,不,我明白那個。我現在的問題是我的數組沒有被寫入文本文件。我編輯了輸出示例和正在編寫的內容。希望我已經讓我的問題更清楚一點。 – Navi4458

+0

@ Navi4458也許你必須添加一個方法來命名奇數賠率,或者一個方法來命名偶數,並寫出outputfile.println(「奇數是:」+賠率);和outputfile.println(「偶數位是:」+ evens); – CodingNinja

+0

@ Navi4458謝謝你的方式澄清 – CodingNinja