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 + " ");
        }
    }
}
Community
  • 1
  • 1
  • `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) ` – Scary Wombat May 21 '20 at 02: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. – Lucas Hoage May 21 '20 at 02:18
  • See https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 for more about gibberish – Scary Wombat May 21 '20 at 02:19

1 Answers1

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
    }
}
leetcode269
  • 391
  • 4
  • 15
  • 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. – Lucas Hoage May 21 '20 at 02:17
  • 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. – leetcode269 May 21 '20 at 02:22
  • 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? – Lucas Hoage May 21 '20 at 02:23
  • I'm sorry. I don't know which question is your theory question. Could you repeat it here? – leetcode269 May 21 '20 at 02:25
  • 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? – Lucas Hoage May 21 '20 at 02:35
  • Yes, an array is an object in java. So you can think of a 2D array as an array that's holding a bunch of "array objects". So in order to extract the individual object stored in the 2D array, you need to first get the individual "array object". That's what the outer for loop is doing. Then you need another for loop to actually loop through each "array object" in order to get the individual object. Same logic applies to any 2D array, no matter the type. – leetcode269 May 21 '20 at 02:40
  • Yeah, I definitely understand the for loop's purpose. It's running until it exhausts the entirety of the elements. Once the new 2d array object has the contents of the 3d array, the nested for loop begins running until it "dumps" it's contents into the string variable s. Thanks a ton for sticking with me. – Lucas Hoage May 21 '20 at 02:53