0

The Numbers are not on the same line, there are 1000 lines each with a number.

This is the code I have but I receive an error when I run it, it points to the int part of the array list and says unexpected type.

import java.util.ArrayList;
import java.io.*;
import java.util.Scanner; 
public class Homework4
{
   public static void main(String[] args) throws IOException
   {
      //Variables 
      int num;
      int temp; 

      Scanner kb = new Scanner(new File("number.txt"));
      ArrayList<int> list = new ArrayList<int>();
      while (kb.hasNextLine())
      {
         list.add(kb.nextLine());


      }      

      //Close File
      inputFile.close();




   }
}
Myles
  • 11
  • 2
  • 1
    I don’t know mich about Java, but I think that kb.nextLine() gives you a string. So basically you are adding string to int arraylist. Try casting readLine to int. – golobitch Apr 15 '20 at 16:22

3 Answers3

1

You have to use the wrapper class Integer.

Change ArrayList<int> to ArrayList<Integer>

Similarly,

long-> Long
double-> Double
char-> Character
float-> Float

you can read more about primitive type collections here and here

Also, when you read the data from file, using kb.nextLine() it returns String type. You can convert it to Integer type using Integer.parseInt() in the following way.

list.add( Integer.parstInt( kb.nextLine() ) );

The next part is, closing the resource. you have not declared a variable by name inputFile. it must be kb instead.

kb.close();

The complete code is as follows

public static void main(String[] args) throws IOException {
    Scanner kb = new Scanner(new File("number.txt"));
    ArrayList<Integer> list = new ArrayList<>();
    while (kb.hasNextLine()) {
        list.add(Integer.parseInt(kb.nextLine()));
    }
    kb.close();
}

Hope this helps.

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
  • This answer is clear and really helps @Myles to understand where the problem is. I can see you just edited, maybe you can also add the Java 8 version of this so it is not inly useful but also up to date. – vicco Jun 18 '20 at 03:14
1

Since you claim to be reading from a text file, you can use class java.nio.file.Files to read it and then use java's stream API to map each line of the text file to an Integer and collect all those Integers to a java.util.List, as shown in the following code:

Path path = Paths.get("path-to-your-file");
try {
    List<Integer> integers = Files.lines(path) // throws java.io.IOException
                                  .map(Integer::parseInt)
                                  .collect(Collectors.toList());
    System.out.println(integers);
}
catch (IOException x) {
    x.printStackTrace();
}
Abra
  • 19,142
  • 7
  • 29
  • 41
0

You have several errors in that code. Firstly, use Integer object wrapper for int primitive type. Next, you need to parse the read String to integer type with the Integer.parseInt() method. And close the Scanner instance instead of undefined file variable. Try following code:

import java.util.ArrayList;
import java.io.*;
import java.util.Scanner; 
public class Homework4{

public static void main(String[] args) throws IOException
{
    //Variables
    int num;
    int temp;

    Scanner kb = new Scanner(new File("number.txt"));
    ArrayList<Integer> list = new ArrayList<>();
    while (kb.hasNextLine())
    {
        list.add(Integer.parseInt(kb.nextLine()));


    }

    //Close File
    kb.close();




}

}

RKCZ
  • 695
  • 2
  • 6
  • 11