Java中的输入扫描器
我要做的是读取3个文件,并将它们存储在一个名为Student的数组中,然后使用readIRstudent、readIR101和readIR102打印出读取的文件。我的问题是,当我执行代码时,只有第一个readIRstudent在我的测试类中实际打印出来,而且我也无法让数组工作。这是我的密码Java中的输入扫描器,java,arrays,input,Java,Arrays,Input,我要做的是读取3个文件,并将它们存储在一个名为Student的数组中,然后使用readIRstudent、readIR101和readIR102打印出读取的文件。我的问题是,当我执行代码时,只有第一个readIRstudent在我的测试类中实际打印出来,而且我也无法让数组工作。这是我的密码 import javax.swing.*; import java.io.*; import java.util.ArrayList; import java.util.Scanner; /** * Cr
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Lewis on 28/03/2015.
*/
public class Task2 {
public Scanner input;
int reg;
double ir101,ir102l;
double aggregate;
String student_name;
public void openIRstud()throws Exception{
JFileChooser IRstudchooser = new JFileChooser();
if(IRstudchooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile = IRstudchooser.getSelectedFile();
input = new Scanner(inFile);
System.out.println("IRstudents file read successfully.");
}
}
public void openIR101()throws Exception{
JFileChooser IR101chooser = new JFileChooser();
if(IR101chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile = IR101chooser.getSelectedFile();
input = new Scanner(inFile);
System.out.println("IR101 file read successfully.");
}
}
public void openIR102()throws Exception{
JFileChooser IR102chooser = new JFileChooser();
if(IR102chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
java.io.File inFile1 = IR102chooser.getSelectedFile();
input = new Scanner(inFile1);
System.out.println("IR102 file read successfully.");
}
}
public void readIRstud (){
while(input.hasNext()){
String irstudid = input.next();
String irname = input.next();
System.out.printf(" %s %s\n", irstudid, irname);
}
System.out.println("Complete.");
}
public void readIR101 (){
while(input.hasNext()){
String ir101studid = input.next();
String ir101mark = input.next();
System.out.printf(" %s %s\n", ir101studid, ir101mark);
}
System.out.println("Complete.");
}
public void readIR102 (){
while(input.hasNext()){
String ir102studid = input.next();
String ir102mark = input.next();
System.out.printf(" %s %s\n", ir102studid, ir102mark);
}
System.out.println("Complete.");
}
public void Student(){
ArrayList<Student> students = new ArrayList<>();
while (input.hasNext()) {
String line = input.nextLine();
Student e = new Student();
e.reg = Integer.parseInt(line.split(" ")[0]);
e.ir101 = Double.parseDouble(line.split(" ")[1]);
students.add(e);
}
}
}
还有ArrayList students部分=新ArrayList;表示此语言级别不支持菱形类型如何修复?若要修复此问题,请将此行替换为ArrayList students=new ArrayList;try ArrayList students=新建ArrayList;将学生放在第二个菱形中您发布的代码不会打印出任何内容,尽管它确实包含在调用时可以打印的方法。要想知道为什么有些东西没有被打印出来,你必须看看应该用来打印的代码。你的主要方法在哪里?还是构造器?这个类的流程不清楚。。。