Java 打印多行文本文档
我有一个问题,我的代码没有打印出文本文档中的所有内容。我的任务是将.txt文件中的内容放入一个新的.txt文件,我想你可以说“fancier” 这是给我的文本文件 托马斯·史密斯32.25 44 金约翰逊255.60 35 无名氏332.9021 Karen Java 1788.99 65 这是我需要的“奇特”输出(只需要输出所有这些) 名字是:托马斯 姓史密斯 购买的物品总数为:3件 客户的总数是:6.75 客户的总四舍五入(cast)为:6 客户的年龄是:44岁 我想我只是盯着它看了这么久,我只是看得太多了Java 打印多行文本文档,java,Java,我有一个问题,我的代码没有打印出文本文档中的所有内容。我的任务是将.txt文件中的内容放入一个新的.txt文件,我想你可以说“fancier” 这是给我的文本文件 托马斯·史密斯32.25 44 金约翰逊255.60 35 无名氏332.9021 Karen Java 1788.99 65 这是我需要的“奇特”输出(只需要输出所有这些) 名字是:托马斯 姓史密斯 购买的物品总数为:3件 客户的总数是:6.75 客户的总四舍五入(cast)为:6 客户的年龄是:44岁 我想我只是盯着它看了这么久,
Scanner inFile = new Scanner(new FileReader("customer.txt"));
double options;
System.out.println("How would you like to input your data?\n1 Input information from customer.txt\n2 Input information from the keyboard.");
options = console.nextDouble();
if (options == 1){
//Variable to store first name
String firstName;
//Variable to store last name
String lastName;
//Variable to store how many items bought
int itemsBought;
//Variable to store the price per item
double itemPrice;
//Variable to store their age
int age;
while (inFile.hasNext()){
//Gets the first name
firstName = inFile.next();
//Gets the last name
lastName = inFile.next();
//Gets number of items bought
itemsBought = inFile.nextInt();
//Gets the price per item
itemPrice = inFile.nextDouble();
// Gets their age
age = inFile.nextInt();
PrintWriter outFile = new PrintWriter("programOutputFile.txt");
outFile.println("The customers first name is: " + firstName);
outFile.println("The customers last name is: " + lastName);
outFile.println("The customer bought " + (int)itemsBought + " items.");
outFile.println("The customers total is " + itemPrice);
outFile.println("The total cost rounded " + (int)itemPrice);
outFile.println("The customers age is: " + (int)age);
outFile.close();
}
}
else if (options == 2) {
String firstname;
String lastname;
int items = 0;
double price = 0;
int age1 = 0;
int counter; //loop control variable
counter = 0;
int limit; //store the number of items
System.out.print("Enter the number of entries you have "); //Line 1
limit = console.nextInt(); //Line 2
while (counter < limit) {
// It is asking for the user to input their first name
System.out.println("Please tell me what is your first name? ");
firstname = console.next();
// It is asking for the user to input their last name
System.out.println("What is your last name? ");
lastname = console.next();
// It is asking for the number of items they purchased
System.out.println("How many items did your purchase? ");
items = console.nextInt();
// Here it is asking for the total price they paid
System.out.println("What was the cost of each item? ");
price = console.nextDouble();
System.out.println("How old are you? ");
age1 = console.nextInt();
double total = items * price;
counter++;
if (counter != 0){
//Outputs the length of Firstname
System.out.println("The name is " + firstname.length() + " letters in your first name.");
//Outputs the first letter of LastName
System.out.println("The first letter of your last name is: " + lastname.charAt(0));
//Outputs the number of items bought
System.out.println("You bought " + items + " items.");
//Outputs Price
System.out.println("Your total price was " + total);
//Outputs the Price as a int number
System.out.println("Your total price rounded is " + (int)total);
//Outputs the age
System.out.println("They are " + age1 + " years old.");
PrintWriter outFile = new PrintWriter("programOutputFile.txt");
//Outputs the information given above into a .txt file
outFile.println("The customers first name is: " + firstname);
outFile.println("The customers last name is: " + lastname);
outFile.println("The customer bought " + (int)items + " items.");
outFile.println("The customers total is " + total);
outFile.println("The total cost rounded " + (int)total);
outFile.println("The customers age is: " + (int)age1);
outFile.close();
}
else
System.out.println("Invalid. Please try again.");
}
}
inFile.close();
}
Scanner infle=新的扫描仪(新的文件阅读器(“customer.txt”);
双重选择;
System.out.println(“您希望如何输入数据?\n1从customer.txt输入信息\n2从键盘输入信息”);
options=console.nextDouble();
如果(选项==1){
//用于存储名字的变量
字符串名;
//用于存储姓氏的变量
字符串lastName;
//变量来存储购买的物品数量
int itemsbound;
//变量来存储每个项目的价格
双倍价格;
//变量来存储他们的年龄
智力年龄;
while(infle.hasNext()){
//获取第一个名称
firstName=infle.next();
//获取姓氏
lastName=infle.next();
//获取购买的项目数
itemsbunk=infle.nextInt();
//获取每个项目的价格
itemPrice=infle.nextDouble();
//得到他们的年龄
年龄=infle.nextInt();
PrintWriter outFile=新的PrintWriter(“programOutputFile.txt”);
outFile.println(“客户的名字是:“+firstName”);
outFile.println(“客户姓氏为:“+lastName”);
println(“客户购买了”+(int)itemsbund+“items.”);
outFile.println(“客户总额为”+项目价格);
outFile.println(“四舍五入的总成本”+(int)项目价格);
outFile.println(“客户年龄为:”+(int)年龄);
outFile.close();
}
}
else if(选项==2){
字符串名;
字符串lastname;
整数项=0;
双倍价格=0;
国际年龄1=0;
int计数器;//循环控制变量
计数器=0;
int limit;//存储项目数
System.out.print(“输入您拥有的条目数”);//第1行
limit=console.nextInt();//第2行
while(计数器<限制){
//它要求用户输入他们的名字
System.out.println(“请告诉我你的名字是什么?”);
firstname=console.next();
//它要求用户输入他们的姓氏
System.out.println(“你姓什么?”);
lastname=console.next();
//它是询问他们购买的物品数量
System.out.println(“您购买了多少物品?”);
items=console.nextInt();
//这里是询问他们支付的总价格
System.out.println(“每个项目的成本是多少?”);
price=console.nextDouble();
System.out.println(“你多大了?”);
age1=console.nextInt();
双倍总额=项目*价格;
计数器++;
如果(计数器!=0){
//输出Firstname的长度
System.out.println(“名称为“+firstname.length()+”您的名字中的字母”);
//输出LastName的第一个字母
System.out.println(“您姓氏的第一个字母是:“+lastname.charAt(0));
//输出购买的物品数量
System.out.println(“您购买了”+项目+“项目”);
//输出价格
System.out.println(“您的总价为”+总价);
//以整数形式输出价格
System.out.println(“您的四舍五入总价为”+(int)total);
//输出年龄
System.out.println(“他们是”+age1+“岁”);
PrintWriter outFile=新的PrintWriter(“programOutputFile.txt”);
//将上述信息输出到.txt文件中
outFile.println(“客户的名字是:“+firstname”);
outFile.println(“客户姓氏为:“+lastname”);
println(“客户购买了”+(int)项+“项”);
outFile.println(“客户总数为”+总数);
outFile.println(“四舍五入的总成本”+(整数)总计);
outFile.println(“客户年龄为:”+(int)age1);
outFile.close();
}
其他的
System.out.println(“无效。请重试”);
}
}
infle.close();
}
从现在起,它将打印出Karen Java的行,而不是Karen、John、Kim和Thomas的行。我已经完成了选项2,但我又遇到了同样的问题