This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Newbie question, just starting up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newbie question, just starting up" Watch "Newbie question, just starting up" New topic

Newbie question, just starting up

Oleg Tchernov

Joined: Dec 30, 2012
Posts: 2
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

Joined: Jan 10, 2002
Posts: 63061

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Oleg Tchernov

Joined: Dec 30, 2012
Posts: 2
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, 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

Joined: Nov 09, 2012
Posts: 84

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

Joined: Oct 13, 2005
Posts: 43417
Stuie is correct. That is exactly what Bear meant.

And welcome to the Ranch
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 187

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

User input from console is always in java.lang.String format, so you can use java.util.Scanner method nextInt() to do this.

The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory |
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43417
Oleg Tchernov wrote: . . .
I'm blocked at how to code the part, 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 later still. Get the star printing working first.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11817

A, 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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Consider Paul's rocket mass heater.
subject: Newbie question, just starting up
It's not a secret anymore!