Joseph Melton
Joseph Melton

Reputation: 1

Java File Reading Incorrectly

So I am reading from a file hurricane information, the format of the information is belown

1980 Aug 945 100 Allen

1983 Aug 962 100 Alicia

1984 Sep 949 100 Diana

1985 Jul 1002 65 Bob

Here is the code for my arrays to hold the data and the while loop to read the file

int arrayLength = 59;
String [] year = new String[arrayLength];
String [] month = new String[arrayLength];
String [] pressure = new String[arrayLength];
String [] month = new String[arrayLength];

File fileName = new File("hurcdata2.txt");
    Scanner inFile = new Scanner(fileName);

int index = 0;
    while (inFile.hasNext())
    {
        year[index] = inFile.next();
        month[index] = inFile.next();
        pressure[index] = inFile.next(); 
        windSpeed[index] = inFile.next();
        name[index] = inFile.next();
        index ++;
    }

When I run this program and print out what is being read from the file I get this

Name [Ljava.lang.String;@42f7ba93

Year [Ljava.lang.String;@74cb7e2c

Month [Ljava.lang.String;@5bc8b69b

Pressure [Ljava.lang.String;@564ca930

I have no idea what is causing the program to read the file like this, the file is named correctly and in the same directory as the program.

Upvotes: 0

Views: 72

Answers (3)

Unihedron
Unihedron

Reputation: 11041

You're possibly using System.out.println(year), where this method actually invokes year.toString(). Since arrays are simple objects which does not override toString(), you get the default behaviour when printing it:

/* Object.toString() source */
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

You should use Arrays.deepToString(year) method:

System.out.println(Arrays.deepToString(year));
System.out.println(Arrays.deepToString(month));
System.out.println(Arrays.deepToString(pressure));
System.out.println(Arrays.deepToString(month));

A similar but more expensive alternative is to create a new ArrayList and print it:

System.out.println(new ArrayList<>(Arrays.asList(year)));
System.out.println(new ArrayList<>(Arrays.asList(month)));
System.out.println(new ArrayList<>(Arrays.asList(pressure)));
System.out.println(new ArrayList<>(Arrays.asList(month)));

Except, your program has two month variables, so that wouldn't even compile.

Upvotes: 0

Vighanesh Gursale
Vighanesh Gursale

Reputation: 921

Assuming that the data is stored correctly and for printing the data use following code:

int i = 0;

while(i < index)
{
       System.out.println("Name:"+name[i]);
       System.out.println("Year:"+year[i]);
       System.out.println("Month:"+month[i]);
       System.out.println("Pressure:"+pressure[i]);
       i++;
}

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35051

Printing arrays / calling .toString on them doesn't do what you'd like it to do in Java. Rather than doing

System.out.println(name);

you need to do

for (String nm : name) {
  System.out.print(nm);
}

Upvotes: 1

Related Questions