0

So I'm working on an assignment for a class in Java, and am having a problem getting scan.nextLine to fill the array of objects. Of course scan.nextLine is giving a String and the object array can't take that. The assignment specifies that the array must be of objects and that each part of the array is filled with an entire line from the file being read. Thanks for any help! It does have to be Temperature objects as it's used in a class later.

        Scanner scan = new Scanner(new File("Temperatures.txt"));
        int n = scan.nextInt() + 1;
        Temperature[] Temperature = new  Temperature[n];
        String dd = scan.nextLine();
        for (int i = 0; i < n; i++) {
            Temperature[i] = scan.nextLine();
        }
        System.out.println(Temperature[n - 1]);
        System.out.println(Temperature[0]);
        return Temperature;
    }

I have tried casting to the object and that did not work.

  • 1
    You don't have an Object array (`Object[]`). You have an array of `Temperature` objects. I don't think you meant that way (unless you have a `Temperature` class). I think you need to make it a `String[]` – user7 Nov 18 '19 at 04:39
  • what is exactly your temperatur object? i think it should have a constructor which will take string in input and assign value. after then you can do Termprature[i] = new Temperature(scan.nectLine()) – Jigar Gajjar Nov 18 '19 at 04:42
  • [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/597657) – Eng.Fouad Nov 18 '19 at 04:47

0 Answers0