我刚刚制作了这个简单的学生程序,它读取数据并写入文件。
我需要的是,如果我想输入100个学生数据,如何在列表中输入,那应该来自用户端
例如,
输入您要输入的学生:2
姓名:Satish devnani 卷号:1
姓名:Sonu 卷号:2
如果用户输入100则应为100。
直到此:
tabindex
答案 0 :(得分:0)
尝试以下方法:
import java.util.*;
import java.io.*;
class filesatish2 {
List studentList = new ArrayList();
String name;
int number;
int count = 0;
public static void main(String args[]) throws IOException {
// int i;
filesatish2 stname = new filesatish2();
stname.count = stname.HOWMANY();
stname.READ();
stname.FILEWRITE();
}
int HOWMANY() {
int count;
Scanner sc = new Scanner(System.in);
printit("How many data you want to enter ?");
count = sc.nextInt();
// printit(""+count);
return count;
}
void READ() {
Scanner sc = new Scanner(System.in);
Scanner text = new Scanner(System.in);
for (int i = 0; i < count; i++) {
printit("Name :");
name = text.nextLine();
printit("Number :");
number = sc.nextInt();
studentList.add(name + "\t" + number + "\n");
}
}
public static void printit(String a) {
System.out.print(a);
}
public void FILEWRITE() throws IOException {
File student = new File("Student.txt");
FileWriter printer = new FileWriter(student);
student.createNewFile();
for (int i = 0; i < count; i++) {
printer.write((String) studentList.get(i));
}
printer.flush();
printer.close();
}
}