0

A similarly written code will work but only if there is a single array. When I try this using multidimensional arrays, it tells me that it cannot be converted from an array to a string.

Why is this happening?

What direction can I take to learn how to solve his problem?

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[][] sentence = new String[3][2];
        Scanner scaninput = new Scanner(System.in);

        for (int row = 0; row < sentence.length; row++) {
            for (int col = 0; col < sentence[row].length; col++) {
                System.out.println("Enter some words: ");
                sentence[row][col] = scaninput.nextLine();
            }
        }

        for (String sentences : sentence) {
            System.out.println(sentences + " ");
        }
    }
}
3
  • for (String sentences : sentence) will produce a compile error as you are trying to convert a String array to a String. Like the input, you could have an inner for loop using for (String [] sentences : sentence) Commented May 21, 2020 at 2:12
  • I did try that on recommendation from the IDE, when I do that, I get gibberish like this: [[Ljava.lang.String;@5caf905d [[Ljava.lang.String;@5caf905d [[Ljava.lang.String;@5caf905d [[Ljava.lang.String;@5caf905d [[Ljava.lang.String;@5caf905d [[Ljava.lang.String;@5caf905d That's why I was wondering where it went wrong. Commented May 21, 2020 at 2:18
  • See stackoverflow.com/questions/29140402/… for more about gibberish Commented May 21, 2020 at 2:19

1 Answer 1

1

The variable sentence is a reference to an array of arrays. You need to do something like the following to access individual string.

for(String[] sentences : sentence){
    for(String s : sentences){
      \\Do something
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Edit: Nevermind, it works. I had to change the variable in the print to s. I think I'm grasping this concept. Thank you for breaking that down for me. I think I understand what you're saying a little. I'm basically stuffing the information into one package that can then be converted down into a simple string.
Can you show what you are putting in your 2d array? If I run the code as it is, I don't see a problem that you mentioned.
I just edited my last post. It worked. If you could touch up on my theory question though? Is that the correct understanding of it?
I'm sorry. I don't know which question is your theory question. Could you repeat it here?
So when I take a multidimensional array's elements and I want to extract that information, I have to downgrade it to a single array and then downgrade that to a string? Does the same technique work for primitives such as int and double? and can those be downgraded from say int[][] and converted in the end to a String?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.