This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

I am new to Java programming

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tip: Try to use more specific subject lines. The more meaningful your subject, the more meaningful your replies.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do you become:

"old to Java programming"

as opposed to "new to Java programming" ?

- Stuart
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic