【问题标题】:What if I want to make this class as a list in java?如果我想在java中将这个类作为一个列表怎么办?
【发布时间】:2016-11-05 04:13:55
【问题描述】:

我刚刚制作了这个简单的学生程序,它读取数据并写入文件。

我需要的是,如果我想输入 100 个学生的数据,如何将其放入列表中,并且应该来自用户端

例如,

输入你想输入的学生:2

姓名:Satish devnani 卷号:1

姓名:索努 卷数:2

如果用户输入 100,那么它应该是 100。

到此为止:

import java.util.*;
import java.io.*;


class filesatish2{

    String name;
    int number;
    //int i=0;
    public static void main(String args[]) throws IOException {
        //int i;
        filesatish2 stname= new filesatish2();      
        Students.READ();    
        Students.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);

        printit("Name   :");
        name=text.nextLine();

        printit("Number     :");
        number=sc.nextInt();
    }

    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();

        printer.write(""+name);
        printer.write("\t"+number);

        printer.flush();
        printer.close();
     }

}

【问题讨论】:

    标签: java arrays class object


    【解决方案1】:

    尝试以下方法:

    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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2017-01-04
      相关资源
      最近更新 更多