2015-10-18 93 views
1
public static void main(String[] args) throws FileNotFoundException { 

     @SuppressWarnings("unused") 
     Scanner in = new Scanner(System.in); 
     System.out.println("enter filename"); 
     Filename=in.next(); 
     PrintWriter outputFile =new PrintWriter(Filename); 
     outputFile.println(); 
     outputFile.close(); 
     getInput(); 
     display(); 

    } 

    public static void display() throws FileNotFoundException{ 
     for (int i = 0; i < genders.length; i++) { 
      System.out.println(ages[i]+";"+genders[i]+";"+emails[i]+";"+salaries[i]); 
    }} 


    public static void getInput(){ 

     System.out.print("How many users do you wish to enter: "); 
     int num = in.nextInt(); 
     ages= new int[num]; 
     genders = new String[num]; 
     emails = new String[num]; 
     salaries = new double[num]; 

     for (int i = 0; i < num; i++) { 
      System.out.print("Please enter your age for person "+(i+1)+": "); 
      ages[i] = in.nextInt(); 
      while (ages[i]<20 ||ages[i]>30){ 
       System.out.println("invalid age please re enter again"); 
       ages[i] = in.nextInt();} 
      in.nextLine(); 

嘿傢伙我試圖將用戶輸入的內容寫入文件。我的問題是它創建的文件,但沒有寫入它。我嘗試了各種方法,但沒有任何幫助?創建文件沒有寫入java

+0

坦率地說,您的代碼有點混亂,但我仍然試圖查看您的代碼。別擔心,你沒有放棄。 – mashedpotats

+3

重複的[http://stackoverflow.com/questions/18070629/how-to-read-from-users-input-in-java-and-write-it-to-a-file](http://stackoverflow .com/questions/18070629/how-to-read-from-users-input-in-java-and-write-it-to-a-file) – nihilon

+0

代碼的哪個部分應該將用戶輸入寫入文件?您只能向文件寫入一行空白行,其他所有輸出都將寫入控制檯。 – Andreas

回答

0

你的問題在這裏得到解答: https://stackoverflow.com/questions/18070629/how-to-read-from-users-input-in-jav‌​a-and-write-it-to-a-file

但概括地說:你是根本創建一個以用戶輸入命名的文件。您需要將該信息實際寫入文件:

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class MainClass { 
    private static String fileName; 
    private static String[] genders, emails; 
    private static double salaries[]; 
    private static int userCount; 
    private static int[] ages; 
    private static Scanner in = new Scanner(System.in); 

    public static void main(String[] args) throws FileNotFoundException { 

     System.out.println("enter filename"); 
     fileName = in.nextLine(); 
     File myFile = new File(fileName); 
     getInput(); 
     display(); 
     FileWriter fWriter = null; 
     BufferedWriter writer = null; 
     try { 
      fWriter = new FileWriter(myFile); 
      writer = new BufferedWriter(fWriter); 
      writer.write(display().toString()); 
      writer.newLine(); 
      writer.close(); 
     } catch (Exception e) { 
      System.out.println("Error!"); 
     } 
    } 

    public static ArrayList<String> display() throws FileNotFoundException { 
     ArrayList<String> data = new ArrayList<String>(); 

     for (int i = 0; i < genders.length; i++) { 

      data.add(ages[i] + ";" + genders[i] + ";" + emails[i] + 
        ";" + salaries[i]); 
     } 

     for (int i = 0; i < genders.length; i++) { 

      System.out.println(ages[i] + ";" + genders[i] + ";" + emails[i] + 
           ";" + salaries[i]); 
     } 
     return data; 
    } 

    private static void getInput() { 
     System.out.print("How many users do you wish to enter: "); 
     int userCount = in.nextInt(); 
     ages = new int[userCount]; 
     genders = new String[userCount]; 
     emails = new String[userCount]; 
     salaries = new double[userCount]; 

     for (int i = 0; i < userCount; i++) { 
      System.out.print("Please enter your age for person " + (i + 1) + 
          ": "); 
      ages[i] = in.nextInt(); 
      while (ages[i] < 20 || ages[i] > 30) { 
       System.out.println("invalid age please re enter again"); 
       ages[i] = in.nextInt(); 
      } 
      in.nextLine(); 
     } 
    } 
} 
+0

我試過這個它不工作。它仍然不會打印任何內容到文本文件 – user3607224

+0

@ user3607224,編輯後包含一個完整的工作示例。在這一點上你應該做的就是添加適當的部分,以獲得性別,電子郵件和工資到'getInput()' – nihilon

2
Filename=in.next(); 
    PrintWriter outputFile =new PrintWriter(Filename); 
    outputFile.println(); 

我認爲你正在創建與用戶輸入的名稱的文件,並打印一個空行吧