0
   public void importStudent(String fileName) throws FileNotFoundException{
        File input = new File("students.txt");
        Scanner readFile = new Scanner(input);
        ArrayList<Object> tokensList = new ArrayList<Object>();

        while (readFile.hasNextLine()){

            tokensList.add(readFile.nextLine().split(","));

            String FirstName = (String) tokensList.get(0);
            String LastName = (String) tokensList.get(1);
            String phoneNum = (String) tokensList.get(2);
            String address = (String) tokensList.get(3);
            double gpa = (double) tokensList.get(4);
            String major = (String) tokensList.get(5);
            double creditsTaking = (double) tokensList.get(6);
            //all of the stuff in one line of the text file
            Student s = new Student( FirstName, LastName, phoneNum, address, gpa,
                    major, creditsTaking);
            peopleBag.add(s);
        }
        readFile.close();
    }

So i have a text file in which each line has all of the information for one object of Student class that I am trying to create. What I want to do is read one line of the text file, add of the info to an array list, then use that list to fulfill all of the fields of my Student constructor. This method has no red lines, but I get the following error when I run this method:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String at step1.PeopleBag.importStudent(PeopleBag.java:35) at step1.Demo.main(Demo.java:10)

5
  • 2
    Could you show us which line is 35th one? Commented Nov 20, 2015 at 22:51
  • You're not actually showing the code that breaks. What is at PeopleBag.java :35 Commented Nov 20, 2015 at 22:52
  • Yours is a classic XY Problem where you're asking for a solution to a specific code problem, when your overall approach is wrong, and the true solution is to use a different approach completely. The problem is not how to get the ArrayList to accept a different type, it is rather that you shouldn't be passing an array into your ArrayList as you're doing. Just use the array returned from the split method and get rid of the tokensList ArrayList altogether. Commented Nov 20, 2015 at 22:54
  • Actually the List vs Array looks like it is the right issue. Commented Nov 20, 2015 at 22:55
  • Wow I've been working on this for a long time, and didn't realize that I've been trying to populate my ArrayList with an array. How do I save the doubles though with a regular list? the gpa and creditsTaking fields are doubles Commented Nov 20, 2015 at 22:58

2 Answers 2

3

String.split returns an array, not a List. You could fix it like,

List<String> tokensList = new ArrayList<>();
tokensList.add(Arrays.asList(readFile.nextLine().split(",")));

or change tokensList to an array, and access it like an array.

String[] tokens = readFile.nextLine().split(",");
String FirstName = tokens[0];
// ...
Sign up to request clarification or add additional context in comments.

5 Comments

if I use tokens as an array, will I be able to cast the GPA and creditsTaking to double?
@Isaiah yes, you will. Instead of list.get(x) you just do tokens[x].
double gpa = (double) tokens[4]; gives me a red line that says i cannot cast from String to double. Am I using the wrong syntax?
@Isaiah Just use Double.valueOf( tokens[4] ). Unfortunately not everything can just be cast to another thing.
@mibac thank you very much. I'm getting an index out of bounds exception on tokens[1], but I think that might be unrelated to this problem. I'll try to figure that one out
0

or do that because split return an array, you should specify the index of array

 tokensList.add(Arrays.asList(readFile.nextLine().split(",")[0]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.