输入文件DATA1.txt将包含7行,每行包含一个正整数,用于描述每种类型的糖果数量。输出文件OUT1.txt将包含与输入的数字相同的星数。 即: 输入DATA1.txt将显示:
12
8
10
5
20
3
7
输出OUT1.txt将显示:
12: ************
8: ********
10: **********
5: *****
20: ********************
3: ***
7: *******
我想已经有一个名为DATA1的文本文件已经创建了7个数字。我希望程序读取它(使用fileRead类)并创建一个名为OUT1的新文本文件,其中包含这些数字和与它们对应的*(使用fileWrite类)。我想一起摆脱用户输入。我该如何修改这个程序?
这是我写的代码:
import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CandyLand{
public static void main(String str[]){
int num1,num2,num3,num4,num5,num6,num7;
String input1 = JOptionPane.showInputDialog("Please, enter the number of candies in the first pile");
num1 = Integer.parseInt(input1);
String input2 = JOptionPane.showInputDialog("Please, enter the number of candies in the second pile");
num2 = Integer.parseInt(input2);
String input3 = JOptionPane.showInputDialog("Please, enter the number of candies in the third pile");
num3 = Integer.parseInt(input3);
String input4 = JOptionPane.showInputDialog("Please, enter the number of candies in the fourth pile");
num4 = Integer.parseInt(input4);
String input5 = JOptionPane.showInputDialog("Please, enter the number of candies in the fifth pile");
num5 = Integer.parseInt(input5);
String input6 = JOptionPane.showInputDialog("Please, enter the number of candies in the sixth pile");
num6 = Integer.parseInt(input6);
String input7 = JOptionPane.showInputDialog("Please, enter the number of candies in the seventh pile");
num7 = Integer.parseInt(input7);
String fileName = JOptionPane.showInputDialog("Please, enter the name of the file");
try{
DataOutput f = new DataOutputStream(new FileOutputStream(fileName + ".txt"));
f.writeBytes(String.valueOf(num1));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num2));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num3));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num4));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num5));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num6));
f.writeBytes("\r\n");
f.writeBytes(String.valueOf(num7));
}
catch(Exception e){
String msg = e.toString();
System.out.println(msg);
}
Scanner file = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
//You should be reading from the first file you created, not OUT1.txt
file = new Scanner(new File(fileName + ".txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
while(file.hasNext()){
if (file.hasNextInt())
list.add(file.nextInt());
else file.next();
}
try {
//Instead using System.out.println, use DataOutput like you were using before
DataOutput f2 = new DataOutputStream(new FileOutputStream("OUT1.txt"));
for (Integer j: list){
f2.writeBytes(j + ": ");
for(int i=1; i<=j; i++){
f2.writeBytes("* ");
}
f2.writeBytes("\r\n");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
这些是我提到的方式:
写文件:
import javax.swing.*;
import java.io.*;
class fileWrite{
public static void main(String str[]){
String fileName = JOptionPane.showInputDialog("Please, enter the name of the output file");
try{
DataOutput f = new DataOutputStream(new FileOutputStream(fileName + ".txt"));
f.writeBytes("Teacher: Hello");
f.writeBytes("Student: Hi");
f.writeBytes("Teacher: Why are you late?");
f.writeBytes("Student: I slept in");
}
catch(Exception e){
String msg = e.toString();
System.out.println(msg);
}
}
}
阅读文件:
import javax.swing.*;
import java.io.*;
class fileRead{
public static void main(String str[]){
String fileName = JOptionPane.showInputDialog("Enter the name of the file I need to read from");
try{
DataInput f = new DataInputStream(new FileInputStream (fileName + ".txt"));
String txt = f.readLine();
while(txt != null){
System.out.println(txt);
txt = f.readLine();
}
}
catch (Exception e){
String msg = e.toString();
System.out.println(msg);
}
}
}
答案 0 :(得分:0)
您可能希望删除一些多余的文本/代码以澄清您的问题。花了一些时间找到重要的一点
我想一起摆脱用户输入。我该如何修改这个程序?
我假设这次事先创建了DATA1.txt,因为你不需要用户输入。您需要做的就是删除程序的前半部分,用户输入,然后更改行
file = new Scanner(new File(fileName + ".txt"));
到
file = new Scanner(new File("DATA1.txt"));
<强>代码强>
所以我刚开始删除了所有用户输入并更改了该行。
import javax.swing.*;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CandyLand{
public static void main(String str[]){
Scanner file = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
file = new Scanner(new File("DATA1.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
while(file.hasNext()){
if (file.hasNextInt())
list.add(file.nextInt());
else file.next();
}
try {
DataOutput f2 = new DataOutputStream(new FileOutputStream("OUT1.txt"));
for (Integer j: list){
f2.writeBytes(j + ": ");
for(int i=1; i<=j; i++){
f2.writeBytes("* ");
}
f2.writeBytes("\r\n");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}