aspose file tools*
The moose likes Beginning Java and the fly likes I am new to Java programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I am new to Java programming " Watch "I am new to Java programming " New topic
Author

I am new to Java programming

ravikanth
Greenhorn

Joined: Dec 01, 2005
Posts: 8
hi all,

I'm a new to learning to Java Programing..
pls help me ..how to get this output.


1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

First of all, please change your name. There's an official policy for names on JavaRanch - basically, you should enter your first name and last name. See the naming policy.

About your problem: Please be more specific about where you get stuck with solving your problem. We don't provide complete solutions here, because the forums are not a means to have your homework done by others, and because you wouldn't learn as much when somebody else does your homework for you.

So, please show us what code you wrote so far and where you get stuck, and we'll help you to get on with it.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Indeed. You've already been told about the naming policy several times, and if you don't give yourself a proper display name immediately, I'm going to have to close this account.

Second, it seems as though each time you get an assignment, you come and post it here, asking "how do I do this?" We don't really work that way here at the Ranch -- we are happy to offer suggestions on improving your own work, but we're not too excited about doing your work for you. Give it a try, let us know when you get stuck.

Finally, as you say, you are new to Java, so this belongs in the "Java in General (Beginner)" forum, where I will now move it.


[Jess in Action][AskingGoodQuestions]
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
I think I remember doing that once 2 years ago. If you just have to get that output then you basicly have to just write alot of "System.out.println" statements.

I just did that now this is what my output looked like:



All with 14 lines of code and half of them "system.out.println" Statements.

It is not really too hard but fun to do I had to write my name once doing the same thing.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I think I remember doing that once 2 years ago. If you just have to get that output then you basicly have to just write alot of "System.out.println" statements.


I'd bet they have to use a loop to do it as it looks like a homework assignment designed for that.
Aaron Ting
Ranch Hand

Joined: Dec 13, 2005
Posts: 78
Yeah.. looks like a homework assignment to me too.. think i was assigned to do that during my 1st year of understanding to java..

Anyway, lets give him the benefit of doubt..

Ravi: you can achieve the desired results, like what the previous postings said, "System.out.println" statement..

Besides the statement, you have also got to consider using the loops, for example, the "for" loops.. making a counter to loop the times of the println statement..

Hope the above information helps..


If you can't convince them, confuse them!
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Tip: Try to use more specific subject lines. The more meaningful your subject, the more meaningful your replies.
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
When do you become:

"old to Java programming"

as opposed to "new to Java programming" ?

- Stuart
Tony Carolla
Greenhorn

Joined: Dec 05, 2005
Posts: 13
Originally posted by Stuart Goss:
When do you become:

"old to Java programming"


Well, java came out somewhere around '94, and I just crossed into my 'late' thirties yesterday, so even though I am inexperienced, you could easily call me "old to Java"
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Actually how would you do it by loop. I been thinking about it and actually want to try to do it just for fun. Like I understand how loops work but I been thinking and ran into some problems.

like I first I thought of just having a couple for loops(1 for each line) I think that will work but my problem is with is the numbers don't go 1,2,3,4 so I am not sure how to change it in my for loop to make it go 1,2,1 or something like that.

here is what I got:



So can anyone lead me done the right path I would really like to get this working since it seems good to know like I said I understand how to do loop and and stuff but things like this stump me.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18710
    
  40

So can anyone lead me done the right path I would really like to get this working since it seems good to know like I said I understand how to do loop and and stuff but things like this stump me.


The trick here -- as in any program -- is to break the problem down into smaller pieces.

Hint 1: How would you do a single line? Such as...

1 2 3 ... n ... 3 2 1

Hint 2: How would you create a method that will do a single line with any value of n?

Hint 3: Given this method, how would you use it to solve the problem?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
I been busy with Christmas so I have not had a chance to look at this so I just started today.

This is what I got so far. This for the first hint, I am not sure if this is what you wanted but this was the only way I knew how to get that result.



So thats what I got so far I not sure what you mean with the methods. I know what they are but don't know how to do them well.

[ December 29, 2005: Message edited by: Michael Hubele ]
[ December 29, 2005: Message edited by: Michael Hubele ]
Joseph Sillitoe
Greenhorn

Joined: Dec 30, 2005
Posts: 12
Ok, I think that the semester is prolly over so I think its safe to post an answer to this...

As noted before, this looks like a homework assignment to practice loops. So here is my really loopy answer:



I'm also working on a slightly more convoluted way using the % operator... stay tuned.
Joseph Sillitoe
Greenhorn

Joined: Dec 30, 2005
Posts: 12
Ok, Here is another way to do this... using the % operator and a little help from our friend "Math.min()". It still uses loops but not in the way you might think.




I hope this helps
jacob vk
Greenhorn

Joined: Dec 30, 2005
Posts: 2
you could always just do something like this


[code]
class irox {
public static void main (string[] args) {
System.out.println("1")
//and so on and so forth
}
}
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182


I'm also working on a slightly more convoluted way using the % operator... stay tuned.[/qb]<hr></blockquote>


I am working through the first version here then I want to study your other one using the array.

I am breaking up your code right now and I want to see if I am understand this right so I going to break up your code peice by peice.

I working on this part



Ok I know what the public class & main method is for so I will just skip that.

Now you declare an int called "n" that holds 4 in it.

Then you have "for (int x = 1; x < n; x++){" and this is like the master for loop right? you are saying that x is 1 and as long as x is less then n then do the loop and after add one to x right?

Then you got the print line that is for just making it a new line right? everytime you finish the loop you conclude that is one line so the next time it does the for line it starts on a new line.

Now we come to:



So this for statement has a y that holds a 1. Then it goes if y is less then x do this the statement right? so that would be 1 < 1 right? so how does this statement work maybe I just getting confused again with the less then symbol again. Or am I missing something in my logic?
[ December 31, 2005: Message edited by: Michael Hubele ]
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
I am working on this part



Ok I know what the public class & main method is for so I will just skip that.

Now you declare an int called "n" that holds 4 in it.

Then you have "for (int x = 1; x < n; x++){" and this is like the master for loop right? you are saying that x is 1 and as long as x is less then n then do the loop and after add one to x right?

; yes


So this for statement has a y that holds a 1. Then it goes if y is less then x do this the statement right? so that would be 1 < 1 right?


Only the first time through.
Because the "y" for loop is nested within the "x" for loop, it will run with each value of x until the x-loop is done. It might help you to run this code:


Note that I changed the original only by adding a printout of succesive x-values and putting y values on new lines.
[ January 02, 2006: Message edited by: Adam Price ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am new to Java programming