0

I am trying to write a program that i need to output a 2 dimensional array for a list of first and last names entered by the user. When I try running the program there is no errors, but it gives me this for output:

[[Ljava.lang.String;@b8f82d, [Ljava.lang.String;@1ad77a7,     
[Ljava.lang.String;@18aaa1e, [Ljava.lang.String;@a6aeed,   
[Ljava.lang.String;@126804e, [Ljava.lang.String;@b1b4c3, 
[Ljava.lang.String;@d2906a, [Ljava.lang.String;@72ffb, 
[Ljava.lang.String;@1df38fd, [Ljava.lang.String;@16a786, 
[Ljava.lang.String;@1507fb2, [Ljava.lang.String;@1efb836, 
[Ljava.lang.String;@126e85f, [Ljava.lang.String;@161f10f, 
[Ljava.lang.String;@1193779, [Ljava.lang.String;@8916a2, 
[Ljava.lang.String;@2ce908, [Ljava.lang.String;@77158a, 
[Ljava.lang.String;@27391d, [Ljava.lang.String;@116ab4e, 
[Ljava.lang.String;@148aa23, [Ljava.lang.String;@199f91c, 
[Ljava.lang.String;@1b1aa65, [Ljava.lang.String;@129f3b5, 
[Ljava.lang.String;@13f3045, [Ljava.lang.String;@17a29a1,  
[Ljava.lang.String;@1434234, [Ljava.lang.String;@af8358, 
[Ljava.lang.String;@d80be3, [Ljava.lang.String;@1f4689e,  
[Ljava.lang.String;@1006d75, [Ljava.lang.String;@1125127, 
[Ljava.lang.String;@18dfef8, [Ljava.lang.String;@15e83f9,  
[Ljava.lang.String;@2a5330] 

BUILD SUCCESSFUL (total time: 0 seconds)

This is my code:

package assignment_6_1;
import java.util.Scanner;
import java.util.Arrays;
public class Assignment_6_1 {

    public static void main(String[] args) {
        //Create a Scanner
        Scanner input = new Scanner(System.in);
        //Create int & string []
        String[][]firstAndLastNames = new String[36][2];
            int[]heightOfPerson = new int[36];

        //Gather list of names and heights
        for(int i=0; i<heightOfPerson.length; i++)
        {
            //Get first name
            System.out.println("Enter Your First Name " + "Passenger #" + i+1 + ": ");
            firstAndLastNames[1][i] = input.next();

            //Get last name
            System.out.println("Enter Your Last Name " + "Passenger #" + i+1 + ": ");
            firstAndLastNames[2][i] = input.next();

            //Get height in inches
            System.out.println("Enter your height (Iches) " + "Passenger #" + i+1 + ": ");
            heightOfPerson[i] = input.nextInt();

            if(firstAndLastNames[36][2] != null){
            System.out.println(Arrays.deepToString(firstAndLastNames));
            break;}
        }
    }
}

I then have to output the height of the person but I have not gotten that far yet.

  • It's printing the content of every cell correctly, but the content is an array in itself, so you get the object's `toString` printed I suppose. See this SO answer for an example to print a 2d array: http://stackoverflow.com/questions/19648240/java-best-way-to-print-2d-array – Darwind Feb 04 '15 at 14:35
  • You aren't printing the data properly; after you get the data, the array must be iterated through to get the data. Your current "System.out.println(Arrays.toString(firstAndLastNames)); will simply print the addresses. You need to iterate through the array for each element using a for-loop. – Evan Bechtol Feb 04 '15 at 14:37
  • What is the output that you expect? – TheLostMind Feb 04 '15 at 14:40
  • Just an example of how it works with `Arrays.deepToString(Object[])`: http://pastebin.com/R5ns3ACU – Darwind Feb 04 '15 at 14:44

5 Answers5

2

Don't use Arrays.toString(array). Use Arrays.deepToString(array). It will print multi-dimensional arrays.

    String[][] array = new String[][] {
            { "Apple", "Pear", "Fruit" },
            { "Pi", "Rho", "Omega" }, 
            { "Jack", "Jill", "Joe" } 
    };

    System.out.println(Arrays.deepToString(array));

    //Prints: [[Apple, Pear, Fruit], [Pi, Rho, Omega], [Jack, Jill, Joe]]
M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
  • Thought of posting this *initially* but the problem is we don't know in what format the OP wants the answer :) – TheLostMind Feb 04 '15 at 14:43
  • Looking at what he has now I reckon any legiable output will do. :D That said, looking at OP should be using a person class to aggregate the names and height. I reckon this is some ones CS coursework though. – M.P. Korstanje Feb 04 '15 at 14:45
  • Not right now though. First he has to see that his output is empty and figure out why. – M.P. Korstanje Feb 04 '15 at 14:57
  • @mpkorstanje i have the output fixed, but now i am getting an array out of bounds exception on line 28 which is where i did if(firstAndLastNames[35][0] != null){ System.out.println(Arrays.deepToString(firstAndLastNames));} any suggestions i dont know how to fix it – Garrett Carder Feb 06 '15 at 14:10
  • Arrays start at index 0. If you have an array of length 2, the valid indexes are 0 and 1. – M.P. Korstanje Feb 07 '15 at 10:53
1

You are stringifying the outer array, but the inner arrays are not handled. What you need to do is to build the output by iterating over the outer array.

This should help:

for (String[] firstAndLastName : firstAndLastNames) {
    System.out.println(firstAndLastName[0] + ", " + firstAndLastName[1]);
}

Alternatively you can store the user details in a User object. Then, if you define a toString method on that object thhe original Arrays.toString call will produce more legible output.

class User {
    private String firstName, lastName;

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    ...

    public String toString() {
        return firstName + ", " + lastName;
    }
}

Also, as nielsen points out your for loop has the wrong loop test on it. You have an empty array at the moment so you need to address that as well.

Matthew Franglen
  • 4,441
  • 22
  • 32
1

Maybe you should not do

 for(int i=0; i>35; i++)

but rather

for(int i=0; i<35; i++)
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You are trying to store the last name in a place that does not exist in your array!

You are declaring your array like this:

String[][]firstAndLastNames = new String[35][1];

This means you have 35 rows, and 1 column. Arrays start at index zero. Not one.

When you are trying to store your last names:

firstAndLastNames[i][1] = input.next();

You are not calling the correct column index. Change the array delcaration line line to this:

String[][]firstAndLastNames = new String[35][2]; //now we have 2 columns

Once you store the data correctly, you need to iterate through the array to properly display your data.

try the following code:

public static void main(String[] args) {
        String[][] array = new String [3][2]; //Notice how the column declaration is now '2'. This means available indexes are 0 and 1.
        Scanner scn = new Scanner (System.in);
        for (int i = 0; i < array.length; i++) {
            System.out.println ("Enter data " + i + ")");
            array[i][0] = scn.nextLine();
        }
        array[0][1] = "1"; //Understand why this works now?
        array[1][1] = "2";
        array[2][1] = "3";  

        for (int i = 0; i < array.length; i++) {
             for (int k = 0; k < 2; k++) {
                 System.out.println((array[i][k]));
             }
        }

    }
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
0

Answer by matthew is perfect but if we do small change in concat operation then it will become more fluent for eg.

 public static void main(String...args) {

    String[][] firstAndLastName =  {
            {"Bob","Martin"},
            {"Martin","Fowler"}
    };

    for(String[] name : firstAndLastName) {
        System.out.println(toString(name));
    }
}

 private static String toString(String[] a) {
    int iMax = a.length - 1;

    StringBuilder b = new StringBuilder();

    for (int i = 0; ; i++) {
        b.append(a[i]);
        if (i == iMax)
            return b.toString();
        b.append(", ");
    }
}
Ashkrit Sharma
  • 627
  • 5
  • 7
  • There is no need to create an iMax variable, to be honest. Why not just use if ( i == a.length-1 )?? – Evan Bechtol Feb 04 '15 at 14:58
  • Function is copy paste from Arrays.toString. Value is not going to change per iteration of loop , so it is better to resolve it only once. – Ashkrit Sharma Feb 04 '15 at 15:19
  • The resolution is constant time though, so assigning it in memory doesn't provide any real benefit. it's like saying x=y, when you could have just used y. – Evan Bechtol Feb 04 '15 at 15:33
  • array length is header field so if you array is bigger 64 byte then it will not fit in same cache line as data. In such case length will share cache line with some other variable and if that variable keeps on changing then there will will be cache line invalidation and due to that you will notice delay because cpu has to move data again from main memory to cache line. In context of this question this is not applicable but I prefer to use meaning full names for expression to make code simple to understand – Ashkrit Sharma Feb 05 '15 at 00:25
  • Definitely a good consideration for developing larger applications though. – Evan Bechtol Feb 05 '15 at 00:26