我想從電視文件中讀取數據,存儲在數組列表中,然後將數據打印到屏幕上。爲什麼來自文件文件的文本沒有被打印出來?
package Testing;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Options {
private static ArrayList<Object> tv1Holder = new ArrayList<Object>();
private static Scanner inputData = new Scanner(System.in);
public static void main (String[] st){
System.out.println("Hi och welcome to TV");
System.out.println("What do you want to do?");
System.out.println("1) Show guide for tv1");
System.out.println("2) Show guide for tv2");
System.out.println("3) Show and handle recordings");
System.out.println("End");
int intakeValue = inputData.nextInt();
switch (intakeValue) {
case 1:showTv1();
break;
case 2: showTv2();
break;
case 3: showAndHandleRecordings();
break;
case 4: end();
default:
break;
}}
private static void showTv1(){
Scanner scanIn = null;
try{
scanIn = new Scanner(new FileReader("tv1.txt"));
while (scanIn.hasNextLine()){
String[] s = scanIn.nextLine().split(" ");
for(String newSValues: s){
System.out.println(tv1Holder.add(newSvalues));
}
}
}
catch (Exception e) {
System.out.println("Error:" + e.getMessage());
}
}
tv1.txt文件包含我想存儲在數組列表中的電視節目列表。截至目前,代碼僅打印出真正的'x'次數。
HTTP://文檔。 oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html#add%28java.lang.Object%29 – x4rf41