user1877918
user1877918

Reputation: 43

Reading ints from a txt file and storing to an array

I am trying to read integers from a text file and store them into an array. The text file reads:

4
-9
-5
4
8
25
10
0
-1
4
3
-2
-1
10
8
5
8

Yet when I run my code I get [I@41616dd6 in the console window...

public static void main(String[] args) throws IOException
    {
        FileReader file = new FileReader("Integers.txt");
        int[] integers = new int [100];
        int i=0;
        try {
            Scanner input = new Scanner(file);
            while(input.hasNext())
            {
                integers[i] = input.nextInt();
                i++;
            }
            input.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(integers);
    }

Upvotes: 4

Views: 26194

Answers (6)

The_Fresher
The_Fresher

Reputation: 77

This should help you to read Integer from a file and store it in array

import java.util.Scanner;

import java.io.File;

import java.util.ArrayList;

public class filetoarray {

public static ArrayList<Integer> read(File f)
{
    ArrayList<Integer> array=new ArrayList<Integer>();
    try
    {
    
    Scanner sc=new Scanner(f);
    while(sc.hasNextLine())
    {
        array.add(sc.nextLine());
    }
    
    }
    catch(Exception e)
    {
        System.out.printf("ERROR : %s", e);
    }
    return array;
}
public static void main(String[] args) {
    File file1=new File("file1.txt");//your file path here
    ArrayList<Integer> array1;
    array1=read(file1);
    System.out.println(array1);
        
}

}

Upvotes: 0

Randall Hunt
Randall Hunt

Reputation: 12572

You're printing out the virtual memory address of the array instead of the actual array items:

You can print out the actual array items, one by one, like this:

// This construct is called a for-each loop
for(int item: integers) {
   System.out.println(item);
}

@akuhn points out correctly that Java has a built in helper for this:

System.out.println(Arrays.toString(integers));

Note that you'll need to add:

import java.util.Arrays

in your imports for this to work.

Upvotes: 5

Rahul
Rahul

Reputation: 16335

Whenever you pass any object to System.out.println(), it prints the toString() for that object. If its not overridden, it prints the memory address of that object.

System.out.println(integers);

is trying to print toString() representation of integer array which is nothing but the JVM address of this array.

To print the actual numbers in the array, you either need to iterate through the array or convert the array to java.util.ArrayList.(which has the toString() method implemented.)

Upvotes: 0

akuhn
akuhn

Reputation: 27793

Unfortunately, Java’s designers missed to add a proper string representations for arrays.

Instead use

System.out.println(Arrays.toString(integers));

You need to import java.util.Arrays; to make this work.

Upvotes: 3

Sikorski
Sikorski

Reputation: 2691

If using an int array is not a restriction, then i would suggest use List. You can use it like this :

 List<Integer> integers = new ArrayList<Integer>();
 Scanner input = new Scanner(file);
 while(input.hasNext()){
   integers.add(scanner.nextInt());
 }
 System.out.println(integers);

Output : [1,2,-1,23]

Upvotes: 0

Murali N
Murali N

Reputation: 3498

instead of this

System.out.println(integers);

try this

System.out.println(integers[0] + " : " + integers[1]);

you need to print actual values in integers[] array not array itself

Upvotes: 2

Related Questions