2012-02-29 79 views
1

我正在爲類應該輸出變量petName,petType和numVisits到名爲「PatientData.txt」的txt文件的簡單Java程序。我有petType和numVisits打印正確,但不是petName。我幾乎是積極的,它與我的第一個垃圾聲明有關,因爲petType是唯一需要捕獲2+詞的字符串。這是我的代碼:Java outputStream不能正確打印

import java.util.Scanner; 
import java.io.*; 
public class AcmeClinic 
{ 
public static void main(String[] args) 
{ 
    Scanner keyboard = new Scanner(System.in); 
    PrintWriter outputStream = null; 

    try 
    { 
    outputStream = new PrintWriter(new FileOutputStream("PatientData.txt")); 
    } 

    catch(FileNotFoundException e) 
    { 
    System.out.println("Unable to create the output file."); 
    System.exit(0); 
    } 

    System.out.println("Enter the number of pets to store information for:"); 
    int amount = keyboard.nextInt(); 
    String [] petNames = new String [amount]; 
    String [] petTypes = new String [amount]; 
    int [] numVisits = new int [amount]; 
    int index; 
    String junk; 
    outputStream.println("Patient Data:"); 
    outputStream.println("Pet Name Pet Type Number of Visits"); 
    if (amount >= 1) 
    { 
    for (index = 0; index < amount; index++) 
    { 
    System.out.println("Type the pet name, then press Enter:"); 
    petNames[index] = keyboard.nextLine(); 
    junk = keyboard.nextLine(); 
    System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:"); 
    petTypes[index] = keyboard.nextLine(); 
    System.out.println("Type the number of visits last year, then press Enter:"); 
    numVisits[index] = keyboard.nextInt(); 
    outputStream.printf("%8s %-8s %-8d%n",petNames[index], petTypes[index],numVisits[index]); 
    } 
    } 

    outputStream.close(); 
} 
} 

示例輸入:

Enter the number of pets to store information for: 
4 
Type the pet name, then press Enter: 
Champ 
Type the animal type (dog, cat, bird, rodent), then press Enter: 
dog 
Type the number of visits last year, then press Enter: 
8 
Type the pet name, then press Enter: 
Bob 
Type the animal type (dog, cat, bird, rodent), then press Enter: 
cat 
Type the number of visits last year, then press Enter: 
3 
Type the pet name, then press Enter: 
Mickey 
Type the animal type (dog, cat, bird, rodent), then press Enter: 
rodent 
Type the number of visits last year, then press Enter: 
1 
Type the pet name, then press Enter: 
Polly 
Type the animal type (dog, cat, bird, rodent), then press Enter: 
bird 
Type the number of visits last year, then press Enter: 
6 

實施例輸出:(PatientData.txt)

Patient Data: 
Pet Name Pet Type Number of Visits 
     dog  8  
     cat  3  
     rodent 1  
     bird  6  
+1

fyi,'if(amount> = 1)'是多餘的,應該刪除,因爲如果'amount == 0'循環會迭代零次。 – Bohemian 2012-02-29 00:33:42

+0

你是如何創建鍵盤廣告outputStream變量的? – 2012-02-29 00:34:28

+0

你爲什麼要調用'junk = keyboard.nextLine()'? – 2012-02-29 00:38:04

回答

0

nextInt()是造成立即nextLine()使用它,以便避免。這將2個或多個字的工作,以及...

System.out.println("Enter the number of pets to store information for:"); 
int amount = Integer.parseInt(keyboard.nextLine()); 
String [] petNames = new String [amount]; 
String [] petTypes = new String [amount]; 
int [] numVisits = new int [amount]; 
outputStream.println("Patient Data:"); 
outputStream.println("Pet Name Pet Type Number of Visits"); 

for (int index=0;index < amount; index++) { 
    System.out.println("Type the pet name, then press Enter:"); 
    petNames[index] = keyboard.nextLine(); 
    System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:"); 
    petTypes[index] = keyboard.nextLine(); 
    System.out.println("Type the number of visits last year, then press Enter:"); 
    numVisits[index] = Integer.parseInt(keyboard.nextLine()); 
    outputStream.printf("%8s %-8s %-8d%n", petNames[index], petTypes[index], numVisits[index]); 
} 

正如前面提到的,你不必在這裏使用數組。你可以這樣做,而不是...

System.out.println("Enter the number of pets to store information for:"); 
int amount = Integer.parseInt(keyboard.nextLine()); 
outputStream.println("Patient Data:"); 
outputStream.println("Pet Name Pet Type Number of Visits"); 

String petName = new String(); 
String petType = new String(); 
int numVisit = 0; 

for (int index = 0; index < amount; index++) { 
    System.out.println("Type the pet name, then press Enter:"); 
    petName = keyboard.nextLine(); 
    System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:"); 
    petType = keyboard.nextLine(); 
    System.out.println("Type the number of visits last year, then press Enter:"); 
    numVisit = Integer.parseInt(keyboard.nextLine()); 
    outputStream.printf("%8s %-8s %-8d%n", petName, petType, numVisit); 
} 
+0

好吧,但如果petName是2個詞如「灰鸚鵡」? – Chromey 2012-02-29 02:24:46

+0

@Chromey:編輯了我的答案。對於第一次沒有查看多個單詞的道歉。多個單詞現在可以正常工作。 – neo108 2012-02-29 03:37:58

+0

非常感謝! – Chromey 2012-02-29 05:06:13