有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

oop使用文件IO(Java)从方法接收输入

我需要允许用户告诉程序文件在哪里,并以特定的方式输出数据。我似乎无法将数据传递到单独的类文件。我做错了什么

import java.util.Scanner;
import java.io.*;
public class Student_Test {

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in); // sets up scanner
    System.out.print("Enter file name: "); //user provides file name and location
    String userFile = in.nextLine(); // accepts input from user
    File file = new File(userFile); //uses the file method to import the data
    Scanner inputFile = new Scanner(file); // uses scanner to read the data
    System.out.print(inputFile.Out());



}

}

另外,我可以提供一些关于如何开始一个单独的学生班来做这项工作的建议。我将要读取的文件有多行文本。我必须把这些文本中的一些转换成整数。然后我必须以某种方式输出它。我应该使用void方法还是return方法


共 (1) 个答案

  1. # 1 楼答案

    只是一些提示 首先,你必须思考一个学生班级需要什么属性。例如: 学生有全名,社会保险号(也许?) 然后你可以创建这样一个单独的类

    public class Student {
        private String fullName,socialSecurityNumber;
        public Student(String fullname,String secNumb){
            fullName=fullname;
            socialSecurityNumber=secNumb;
        }
    }
    

    但主要是你必须考虑学生们要做什么