简体   繁体   中英

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);
    }

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.

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.

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

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]

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.)

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);

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM