Need help inputting data from file into array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shadowofanubis66
    New Member
    • Feb 2008
    • 1

    #1

    Need help inputting data from file into array

    Basically, the project I'm working on this week is to make a school lunch menu with a GUI, when the person types in a day, the menu for that day shows up. We have two files, ones a day off text file which just has the days off in it, and the other is a lunch menu file, which each line being an entry. Each day inside that lunch menu file ends with the word Milk. I've been trying to get the days off from the file and store it into an array of int, but every time i System.out.prin t the array.... it gives me gibberish. Can you guys help me with this (and any other issues you see in the program). I'd greatly appreciate it because I've been looking at this for awhile and am really stuck.

    Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class SchoolLunch implements StringLogInterface{
    	private int[] indexArray = new int[31];
    	private static Scanner input;
    	private static PrintWriter output;
    
    	private void getDaysOff(){
    		int line = 0;
    		try{
    			FileReader reader1 = new FileReader("days_off.dat");
    			input = new Scanner(reader1);
    			
    			while(input.hasNextInt()){
    				line = input.nextInt();
    				System.out.println(indexArray);
    			}	
    			indexArray[line]++;
    			
    		}catch(FileNotFoundException e){
    				System.out.print("The file could not be found.");
    				e.printStackTrace();
    			}
    		
    		}
    	
    	private void buildMenu(){
            int menuentry = 1;
            String names = "a";
            try{
                FileReader reader2 = new FileReader("lunches.dat");
                input = new Scanner(reader2);
                ArrayStringLog log1 = new ArrayStringLog("Day" + " " + menuentry);
               
                
                while(input.hasNext() && !names.contains("Milk")){
                    names = input.nextLine();
                    log1.insert(names);
                    log1.isFull();
                    log1.size();
                    menuentry++;
                    log1.getName();
                    log1.toString();
                    
                }
                System.out.print(log1);
            }catch(FileNotFoundException e){
                    System.out.print("The file could not be found.");
                    e.printStackTrace();
                }
        }    
    		
    	
    	private boolean isDayOff(int n){
    		int line2 = 0;
    
    		try{
    			FileReader reader1 = new FileReader("days_off.txt");
    			input = new Scanner(reader1);
    			
    			while(input.hasNext()){
    				line2 = input.nextInt();
    			}	
    		}catch(FileNotFoundException e){
    				System.out.print("The file could not be found.");
    				e.printStackTrace();
    			}
    		if(line2 == n)
    			return true;
    			else
    				return false;
    	}
    	
    	private void getMenu(int k){
    		indexArray[k]++;
    	}
    	
    	public static void main(String[] args) {
    		SchoolLunch sl = new SchoolLunch();
    		sl.getDaysOff();
    		
    		
    
    	}
    
    }
    From just trying to run the getDaysOff method, this is what i get in the console...

    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
    [I@fd13b5
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    That's what you get when you try to directly write an array object. Specifically, that is what the toString method of arrays return. Demo:
    [CODE=Java]import java.util.Array s;

    public class ArrayExample {
    public static void main(String[] args) {
    int[] v = {10,20,30,40,50 ,60};

    System.out.prin tln("#1 " + v);

    System.out.prin tln("#2 " + Arrays.toString (v));

    System.out.prin t("#3");
    for(int i=0; i<v.length; ++i) {
    System.out.prin t(" " + v[i]);
    }
    System.out.prin tln();
    }
    }[/CODE]

    Comment

    Working...