2012-02-29 26 views
1

変数 "petientData.txt"に、変数petName、petType、およびnumVisitsを出力するクラス用の簡単なJavaプログラムを作成しています。私はpetTypeとnumVisitsが正しく印刷されていますが、petNameは印刷されていません。 petTypeが2+の単語をキャプチャしなければならない唯一の文字列であるため、私は最初の迷惑文と何か関係があります。Java出力ストリームが正しく出力されない

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 '(量> = 1)'冗長であり、 '量は== 0 'ならばループがゼロ回反復されますので、削除する必要がある場合に、これを行うことができます。 – Bohemian

+0

'keyboard'広告の' outputStream'変数はどうやって作成していますか? –

+0

なぜあなたは 'junk = keyboard.nextLine()'を呼びますか? –

答えて

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が "Grey Parrot"のような2ワードの場合はどうなりますか? – Chromey

+0

@Chromey:私の答えを編集しました。はじめて複数の言葉を見ないことに対する謝罪。今では複数の単語がうまく動作します。 – neo108

+0

ありがとう! – Chromey

関連する問題