• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie question, just starting up

 
Oleg Tchernov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm new to programming and doing my first ever course in this field, which is, you guessed it, intro to java. I have an assignment which i'm a bit puzzled by and I'm fairly certain some of your expertise might help me out. The task I have to do is have a user enter a number of his choice, which in turn will create a triangle of "*".
So for example if they enter 4 then it would be:
*
**
***
****

It's essentially a practice on the while loop (I hope so) and also the int input by the user but my lack of practice really mentally blocks me from trying it out and I'm hoping someone can lead me in the right direction.
I would really appreciate an explanation and some bits here and there just so I can put it on eclipse and play around with it.

Much appreciated guys
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, step away from the keyboard.

Then grab a pencil and a piece of paper. I'm serious.

Then write, in English, what you need to do to accomplish this.

Code can come later.
 
Oleg Tchernov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah my bad I didn't proof read my initial post. Let me rephrase that more clearly.

I need a small program in which a user can input a number of his choice. That number will create a little triangle of stars like this
*
**
***
****
*****
It would be like this if the user chose "5".

I'm blocked at how to code the part system.in, the part which gives the user the possibility to choose the number.
I'm on a permanent brain fart I just don't know.
 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misunderstood Bear. When he said "Then write, in English, what you need to do to accomplish this. " he means write out what you think the program needs to do, each step, on a bit of paper. By thinking through the logical building blocks of the program without thinking about programming, it will help make the solution clearer in your head.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuie is correct. That is exactly what Bear meant.

And welcome to the Ranch
 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Oleg.
You have to be more foolish, so that you can attempt stupid[brainy] things! So, as Bear said, pen down the English literature on how you will achieve this using some loop. Then convert that into runnable code.
I'm blocked at how to code the part system.in

User input from console is always in java.lang.String format, so you can use java.util.Scanner method nextInt() to do this.
e,g.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oleg Tchernov wrote: . . .
I'm blocked at how to code the part system.in, the part which gives the user the possibility to choose the number.
. . .
In which case hard‑code the 5. If use it as i somewhere, for the time being write int i = 5;
You can change it to 6 later, and you can change it to use System.in later still. Get the star printing working first.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A key...no, wait. that's not strong enough...

a CRITICAL thing to learn is that when you program, you separate out the discrete elements of what your code has to do. Campbell's post hints at it, but I think this part is often not given the emphasis it requires.

For example...getting the input is a COMPLETELY SEPARATE problem from printing a triangle of stars. There are thousands of reasons why you might want to get input from a user, and getting it has nothing to do with what you're going to do with it. So, you separate out that part.

Further, printing a triangle of stars really has nothing to do with getting input from a user. I may want to print out the triangle based on data I read from a file, or pull from a database, or that is sent via a TCP/IP connection...again, the point is that IT DOESN'T MATTER WHERE the data comes from. Assuming I have a value from SOMEWHERE, I need to print out a triangle.

By separating out these components, you make the task possible. Michelangelo didn't take a block of marble and carve David by tackling the whole thing at once...he did it in teeny, tiny, incremental steps, constantly looking at what he did as he did it.

So let's assume you are just working on the triangle printing. Note that that can and should be broken down further. For example, if i were doing it, I'd probably have a method that printed X stars on a single line, where X is a parameter passed to it.

Then I would call that method over and over to print first 1 star, then 2, then 3...until i am done printing everything.

Each piece should be written independently from the others, and THOROUGHLY tested before you start the next.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic