3

I would like to read data from a tv file, store in an arraylist and then print out the data unto my screen.

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 file contains a list of tv shows which i want to store in an arraylist. As of now the code only prints out true 'x' amount of times.

4

1 回答 1

4

此行打印add函数的结果,它是一个布尔值 ( http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E) ):

System.out.println(tv1Holder.add(newSvalues));

要打印字符串,请将其更改为:

System.out.println(newSvalues);
tv1Holder.add(newSvalues);
于 2013-08-03T23:13:43.173 回答