java stream 打印_Java outputStream无法正确打印

我正在为一个简单的Java程序用于输出变量petName,petType和numVisits到名为“PatientData.txt”的txt文件。我有petType和numVisits打印正确,但不是petName。我几乎是积极的,它与我的第一个垃圾声明有关,因为petType是唯一需要捕获2+词的字符串。这是我的代码:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值