wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newbie question, just starting up" Watch "Newbie question, just starting up" New topic
Author

Newbie question, just starting up

Oleg Tchernov
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 61199
    
  66

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
Greenhorn

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 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

Joined: Nov 09, 2012
Posts: 73

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.


OCAJP 7
Campbell Ritchie
Sheriff

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

And welcome to the Ranch
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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.


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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

Joined: Oct 02, 2003
Posts: 11311
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie question, just starting up