Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing Hello World from an array

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So my professor asked my class to print out Hello World normally on a horizontal line but he gave us the letters in a vertical array as shown (He actually just wrote the homework on the wall and we had to go from there so the code you see is me doing some trial and error). What I need help with is trying to fix it so it prints Hello World like normal. Below is the code I have and I just can't get it to print right. Please help

 
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your task shows the letters in columns but that's not the way it's stored. What you have is the same as
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your task shows the letters in columns but that's not the way it's stored. What you have is the same as



I see, thank you for that. Any suggestions on how to fix it and actually store it vertically? I'm asking because our professors aren't actually lecturing good so all of us are resorting to forums and other video tutorials for help. I'm sorry if this is an inconvenience but I have no other choice.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can't get it to print right.


Can you post what the current output of the program is?
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

can't get it to print right.


Can you post what the current output of the program is?



Yes of course it's "HWEOLRLLOD"
 
Carey Brown
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diko Anoian wrote:I see, thank you for that. Any suggestions on how to fix it and actually store it vertically? I'm asking because our professors aren't actually lecturing good so all of us are resorting to forums and other video tutorials for help. I'm sorry if this is an inconvenience but I have no other choice.

I'm not entirely sure that you are meant to "fix it". I'm guessing it was just written that way to force you to think about how you'd solve the problem as is.
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Diko Anoian wrote:I see, thank you for that. Any suggestions on how to fix it and actually store it vertically? I'm asking because our professors aren't actually lecturing good so all of us are resorting to forums and other video tutorials for help. I'm sorry if this is an inconvenience but I have no other choice.

I'm not entirely sure that you are meant to "fix it". I'm guessing it was just written that way to force you to think about how you'd solve the problem as is.



Thing is, I copied another normal print array task we did and decided to go from there. What you see here is that code with some changes in which I thought I had gotten closer to getting it right. We were given this:
H W
E  O
L  R
L  L
O D
And told to print this "HELLO WORLD". That's all. So that is what I need help with.
 
Greenhorn
Posts: 11
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try using an array of strings? So an array of multiple strings, representing the 2 columns? Each horizontal row is a row of 2 letters. You kind of had it in your code but you just had a single string array inside another array. Try splitting it up so you have multiple string arrays, each representing a row.

Maybe something like this:

 
Carey Brown
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q: How many rows, and how many columns does your array have?
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Q: How many rows, and how many columns does your array have?



It's supposed to have 5 rows and 2 columns. We went from 5 columns and 2 rows to this. I thought the way I wrote it made me have 5 rows and 2 columns but apparently not.
 
Carey Brown
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you were given
H W
E  O
L  R
L  L
O D
Then one way to represent it would be
{
 { "H", "W" },
 { "E", "O" },
  ...
 { "O", "D" }
}
That would give you 5 rows of 2 columns.
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you were given
H W
E  O
L  R
L  L
O D
Then one way to represent it would be
{
 { "H", "W" },
 { "E", "O" },
  ...
 { "O", "D" }
}
That would give you 5 rows of 2 columns.



GOD BLESS YOU! I'm going to try this and see if it finally solves this problem. Thank you for your time by the way.
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you were given
H W
E  O
L  R
L  L
O D
Then one way to represent it would be
{
 { "H", "W" },
 { "E", "O" },
  ...
 { "O", "D" }
}
That would give you 5 rows of 2 columns.



-----------------------------------------------------------------

So I did what you suggested and the output I got was:
HW
HW EO
HW EO LR
HW EO LR LL
HW EO LR LL OD

By now I'm sure my problem is in the for loops. To be honest, those for loops aren't really my area of expertise when it comes to arrays. I've no idea what's happening and here below I've added the new code for you. The way the for loops work in this case was not explained to us sufficiently I'm afraid.

package helloWorldArray;

public class HelloWorld {

public static void main(String[] args) {
// TODO Auto-generated method stub

String [][] myArrayX = new String[5][2];
   String [][] myArray = {
     { "H", "W" },
       { "E", "O" },      
       { "L", "R" },
       { "L", "L" },
       { "O", "D"}
     };
   
   String word = "";
   
   for (int row = 0; row < myArray.length; row++)
   {
    for (int col = 0; col < myArray[row].length; col++)
    {
    word += myArray[row][col];
    }
    word += " ";
   
   System.out.println(word);
   
   }
}
}
 
Carey Brown
Saloon Keeper
Posts: 10835
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, with paper and pencil, write down the row&col for: "H", "E", "L", "L", "O". You should see a pattern int the rows and cols.
 
Diko Enoyan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:So, with paper and pencil, write down the row&col for: "H", "E", "L", "L", "O". You should see a pattern int the rows and cols.



Forget about it, I'm just going to submit it as is. I'm wasting too much time on this. Such a shame really, the way these professors, who are accomplished programmers themselves, "teach" us is really discouraging me from Computer Science. I have no outside knowledge of Java whatsoever and these people talk to us as if we're avid programmers. The entire class shares my concern unfortunately. And to be honest, I'm loving this I truly am, but everytime I come across problems like this I just start getting frustrated and lose it. But anyways, thank you Carey for your time and patience. I went to another forum at first and they downvoted me like hell for no reason so thank you for spending time here
 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diko Enoyan wrote:. . .
HW EO LR LL OD

By now I'm sure my problem is in the for loops.

No, maybe it is simply that you are printing the pairs of letters on one line. If you printed them on separate lines you would get

To be honest, those for loops aren't really my area of expertise when it comes to arrays. . . .

Loops are by no means easy; you will have to persevere. Even experienced people have to think hard before writing that sort of loop.

I am afraid if it were I marking that, you wouldn't get a good mark. Sorry. At least let's give you a resit. Don't divide the array into multiple arrays. Start by describing the structure of the array which Carey showed you in his first post, with the confusing feature you were presented with removed. Once you have done that, let't consider the next stage.
 
Don't MAKE me come back there with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic