• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Patterns  RSS feed

 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code and this array:

The output is:


But what I want is :


Note: the array is supposedly random letters and not just the alphabet. I want it so it continues from the letters after the new line, but am unsure how to do so.

Thank you
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about line 10.  What would you do to the index to get the next letter?
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Think about line 10.  What would you do to the index to get the next letter?

add 1 to J?
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Think about line 10.  What would you do to the index to get the next letter?

I've tried :
this, but it started at b instead.

I've tried:
this, but only given me the letter f. I'm kind of out of ideas at the moment.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a different index for arrays?
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:How about using a different index for arrays?


How can I achieve this? Sorry I'm currently at school right now but I'm unsure about what you mean by this.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about creating a whole new index just for advancing the letters?
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:How about creating a whole new index just for advancing the letters?

Making another loop? Inside the current loop?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a loop, just another index.
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Not a loop, just another index.

Sorry I'm probably a burden but I'm trying so here's how I'm currently interpretting the situation you're telling me to create a whole new variable and do a counter? Because I'm not understanding on creating a new index. Thank you for helping by the way and not just giving the answer.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes when one approach isn't working, it helps to take a different tack.  You don't have to solve this all at once.  You can easily solve your problem of wanting to output the letters consecutively. Don't worry about the formatting for now, just get the letters to be printed in the order you want them. Try printing them all on one line first. Then try to print each letter on a different line.

Once you have that done, think about where you want to start a new line. Again, don't worry about forming a triangle just yet.  Just think of outputting the correct number of letters per line.

So the progression would look something like this:

First cut (all in one row):
A B C E D F G ... X Y Z

Second cut (one letter per line):
A
B
C
D
E
...
X
Y
Z

Third cut (correct # letters per line):
A
B C
D E F
G H I J
K L M N O
...

Fourth cut (form a pyramid):
...

Do you think you can at least try to do the first and second cuts?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One other tip: You only need one for-loop to output the letters. The only other for-loop you'll need won't come in until the fourth cut of the step-wise approach I just outlined for you and that second for-loop will only output the correct number of spaces before you start printing out a row of letters in the pyramid.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Johnson wrote:
Knute Snortum wrote:Not a loop, just another index.

Sorry I'm probably a burden but I'm trying so here's how I'm currently interpretting the situation you're telling me to create a whole new variable and do a counter? Because I'm not understanding on creating a new index. Thank you for helping by the way and not just giving the answer.

Yeah, "create a variable that holds an index into arrays" is what I meant.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could start helping yourself by cleaning up some of the cognitive dissonance you create by naming your array of letters, "arrays" -- that's like calling the set of numbers from 0 to 10 "sets".  It may seem like a trivial thing but look up the Stroop Effect and you'll see just how much that kind of thing can mess with the way your brain works.
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Sometimes when one approach isn't working, it helps to take a different tack.  You don't have to solve this all at once.  You can easily solve your problem of wanting to output the letters consecutively. Don't worry about the formatting for now, just get the letters to be printed in the order you want them. Try printing them all on one line first. Then try to print each letter on a different line.

Once you have that done, think about where you want to start a new line. Again, don't worry about forming a triangle just yet.  Just think of outputting the correct number of letters per line.

So the progression would look something like this:

First cut (all in one row):
A B C E D F G ... X Y Z

Second cut (one letter per line):
A
B
C
D
E
...
X
Y
Z

Third cut (correct # letters per line):
A
B C
D E F
G H I J
K L M N O
...

Fourth cut (form a pyramid):
...

Do you think you can at least try to do the first and second cuts?

Hello sorry at the time you posted your comment I was currently at school.
1st step:
for(int i = 0; i <= 6; i++)
        {
            System.out.print(arrays[i]);
        }
2nd step:
for(int i = 0; i <= 6; i++)
        {
            System.out.println(arrays[i]);
        }
3rd step:
unsure how you made the pyramid without a for loop
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Johnson wrote:3rd step:
unsure how you made the pyramid without a for loop

You misunderstood me. I didn't say you can create the pyramid without a for-loop. You have to take what you did in the first and second cut and apply what you learned about keeping letters on the same line and starting a new line of letters. There's a slight difference in the statements you use to do one vs. the other right?  So when outputting letters for the pyramid, you have a combination of printing some letters on the same line and other letters starting on a different line, right? So, you still need a for loop.  This is where you go back to Knute's previous suggestions.  See how you can use his tips and what you've done so far in the  first and second cut. The combination of those things will let you print the pattern that looks like this:

A
B C
D E F
G H I J
K L M N O
...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you stop thinking about Java coding and think about describing the process in plain English, it will help.

1st line, you print one letter
2nd line, you print two letters
3rd line, you print three letters
4th line, you print four letters

Do you see a pattern there? How may letters do you print on the 5th line? 6th line? 7th line? ... 35th line?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And again, you can help yourself by getting rid of the cognitive dissonance (i.e., the mismatch in the name and what the array actually represents) that the name arrays creates.  Think about it, do you call the collection of dogs in a kennel "the kennels"?  Or the cars in a lot "the lots"?  Or the birds in a flock "the flocks"?  You don't, right? Because it's downright silly to do that. Then why would you call the letters in an array "arrays"?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't think that choosing proper names in your programs is a trivial thing. Some of the best programmers in the world have said that the two hardest things in programming are: 1. Cache invalidation, 2. Choosing good names, and 3. Off-by-one errors.  By far, #2 in that list is the hardest of them all.
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
L Johnson wrote:3rd step:
unsure how you made the pyramid without a for loop

You misunderstood me. I didn't say you can create the pyramid without a for-loop. You have to take what you did in the first and second cut and apply what you learned about keeping letters on the same line and starting a new line of letters. There's a slight difference in the statements you use to do one vs. the other right?  So when outputting letters for the pyramid, you have a combination of printing some letters on the same line and other letters starting on a different line, right? So, you still need a for loop.  This is where you go back to Knute's previous suggestions.  See how you can use his tips and what you've done so far in the  first and second cut. The combination of those things will let you print the pattern that looks like this:

A
B C
D E F
G H I J
K L M N O
...

I currently have:
     
I can feel like Im going in the right direction but can you tell me what I am doing wrong I did store the values.
 
L Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Don't think that choosing proper names in your programs is a trivial thing. Some of the best programmers in the world have said that the two hardest things in programming are: 1. Cache invalidation, 2. Choosing good names, and 3. Off-by-one errors.  By far, #2 in that list is the hardest of them all.


Im sorry that me choosing the names were annoying you lol this was just a small sample of what I have to do for my self learn. Ill change the name of the array
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Johnson wrote:Im sorry that me choosing the names were annoying you lol this was just a small sample of what I have to do for my self learn. Ill change the name of the array

It wasn't annoying me; I was just pointing out a common problem that programmers have in that they tend to focus too much on implementation details rather than intent when it comes to choosing names. You probably picked the name "arrays" for the simple reason that you're dealing with an array and that's what you're focused on. It's like calling an engine a "mass of metal, hoses, wires, nuts, and bolts" instead of just "the engine."  Always think intent, not implementation when choosing names to use in your programs.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Johnson wrote:. . . choosing the names were annoying you . . .
You are not annoying us. Come back in six months and try to read your code, and then you will find out why good names are important.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Johnson wrote:I currently have:I can feel like Im going in the right direction but can you tell me what I am doing wrong I did store the values.

On line 3, why do you write i < 6  Why use a specific number 6? That severely limits the usefulness of your code. If there are more than 6 elements in the array, then are you not going to print those other ones? What if there are fewer than 6 elements in the array?

On line 8 and 9, what's the purpose of doing this? This code doesn't even do what you want it to do. What does hold.length()] represent and why are you using that value as an index to the array of letters? What do you want to do inside the loop and why do you think those two lines of code achieve that goal? Tell us what you're thinking so we can get some insight on why/how you've come up with incorrect code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!