5 Replies - 348 Views - Last Post: 28 November 2012 - 08:45 PM Rate Topic: -----

#1 raehp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 12

Arrays won't fill from method.

Posted 28 November 2012 - 08:08 PM

Simply stated, this project calls to read input from a file and store this information in 5 arrays using a method to fill the arrays. My problem is, when I sout one of the arrays after the method in main, it does not display. I don't think the arrays are filling correctly from the method. If I print the array, I get this "[Ljava.lang.String;@1d63e39".
This is my first semester learning java, so my knowledge is limited. NetBeans doesn't flag any code as being bad, and the program compiles, the arrays just don't seem to fill. Any input is appreciated.

import java.io.*;
import java.util.Scanner;

public class State_Information {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException{
        
        Scanner keyboard = new Scanner(System.in);
        
        //Array Declarations
        String[] stateName = new String [50];
        String[] stateNickname = new String [50];
        String[] stateCapital = new String [50];
        String[] stateFlower = new String [50];
        String[] statePop = new String [50];

        //Method Calls
        fillArray(stateName, stateNickname, stateCapital, stateFlower,
                statePop);
       
    }    
        
    /**
     *
     * @param stateName
     * @param stateNickname
     * @param stateCapital
     * @param stateFlower
     * @param statePop
     */
    public static void fillArray(String[] stateName, 
            String[]stateNickname, String[] stateCapital, String[] stateFlower, 
            String[] statePop) throws IOException{   
            
        
        //Declare File Object
        File myFile = new File("state_data.txt");
        if (!myFile.exists())
        {
            System.out.println("Unable to open state_data.txt");
            System.exit(0);
        }
        
        //Declare Scanner Object
        Scanner inputFile = new Scanner(myFile);
        
        //Read From Input
        for (int i=0; i<50; i++)
        {
        stateName[i] = inputFile.nextLine();
        stateNickname[i] = inputFile.nextLine();
        stateCapital[i] = inputFile.nextLine();
        stateFlower[i] = inputFile.nextLine();
        statePop[i] = inputFile.nextLine();
        }
        
        //Close File
        inputFile.close();
    }   



Is This A Good Question/Topic? 0
  • +

Replies To: Arrays won't fill from method.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Arrays won't fill from method.

Posted 28 November 2012 - 08:11 PM

Don't really understand what you mean by "fill array"
String array, as all Object arry, are initialized with all their elemnts to null
Was This Post Helpful? 0
  • +
  • -

#3 raehp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 12

Re: Arrays won't fill from method.

Posted 28 November 2012 - 08:19 PM

I guess what I'm trying to say is the fillArray method is meant to read info from a text file, then when the method is called in main, put the information into 5 arrays. The text file has the state name, capital, flower, nickname, and population for all 50 states. The method should read this information and then when called, fill the string arrays declared in main with this information from the file. With the code I posted, if I simply printed one of the arrays, stateName for example, all it displays is "[Ljava.lang.String;@192b996" instead of the 50 state names from the file. In the method, I believe the code for reading the file is correct. I don't know if it is the method call that is incorrect or what it might be. In the course we just learned objects and classes, which we won't use for this project, if that sheds some light on my overall knowledge of java.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Arrays won't fill from method.

Posted 28 November 2012 - 08:25 PM

You have to read that as soon as possible

http://www.dreaminco...arallel-arrays/

and actually I think it is an emergency :)/// don't worry your PC won't explode but you miss a real basic concept in Java and OO programming :^:
Was This Post Helpful? 1
  • +
  • -

#5 raehp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 12

Re: Arrays won't fill from method.

Posted 28 November 2012 - 08:40 PM

I understand that classes and objects make are the route to go, but for this project we have to use parallel arrays set up similar to my code. I've never attempted anything with more than one array at a time prior, but this project calls for this concept. I'm just stuck on why the 5 arrays do not fill with the information read from the file in the fillArray method.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Arrays won't fill from method.

Posted 28 November 2012 - 08:45 PM

Just chck your code. Should work
Simply add a few System.out.println() to check where it fails
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1