Results 1 to 16 of 16
- 03-13-2012, 07:17 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
how to make a string go diagonaly
hi im new and wanted to know how to make a string go diagonal i have some code that prints out te second part of the string each time but i do not want that i want the string to be diagonal like e.g
input:hello
output:h
e
l
l
o
pls help me
here is code i got so far
String jo = JOptionPane.showInputDialog("Enter a sentence: ");
int len = jo.length();
char[] ca = new char[len];
for(int i = 0; i < len; i++)
{
ca[i] = jo.charAt(i);
System.out.println(ca);
}
- 03-13-2012, 07:22 PM #2
Re: how to make a string go diagonaly
What exactly does it mean for a String to "go diagonally"? Can you type something out (use code tags to preserve highlighting) that shows us an example?
When you type it out, do you notice any patterns?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 07:36 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: how to make a string go diagonaly
say the string is hello then the output should be h, (next line with 1 space) e,(next line with 2 spaces) l,(next line with 3 spaces) l,(next line with space) o
sort of like
h
#e
##l
###l
####o
but without the #
- 03-13-2012, 07:40 PM #4
Re: how to make a string go diagonaly
Seems reasonable. Do you see a pattern?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 07:41 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: how to make a string go diagonaly
So the first line (line #0) has zero leading spaces, the next line (line #1) has one leading space; line #i has i leading spaces. Print those spaces before you print the character.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-13-2012, 07:51 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: how to make a string go diagonaly
how do i print a space before the word
- 03-13-2012, 07:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
-
Re: how to make a string go diagonaly
You could also do this:
Java Code:for(int i = 0; i < len; i++) { // no need for storing a copy? // ca[i] = jo.charAt(i); //add an extra space for each line char[] spaces = new char[i]; //fill the array with spaces Arrays.fill(spaces, ' '); //build the output string String output = new String(spaces); ouput += jo.charAt(i); System.out.println(output); }
- 03-14-2012, 01:37 AM #9
Re: how to make a string go diagonaly
Why are you posting code? Why not help the OP think through the problem and come up with a solution themselves?
-
Re: how to make a string go diagonaly
I thought i'd introduce the OP to a library method (Arrays.fill), so that they might search what other useful tools they have in the java packages in future.
-
Re: how to make a string go diagonaly
And yes, I have an answer to everything. I can't help it - it's my generation.
- 03-14-2012, 01:45 AM #12
Re: how to make a string go diagonaly
The OP should learn to walk before trying to run.
-
Re: how to make a string go diagonaly
Ok confucious. How did you come to know that in the first place? Is it because somebody tried to run before they walked, hurt themself, and then learnt from their mistakes? Everyone has their own way of learning anyway. I always jumped into code I didn't understand, and then learnt it backwards myself by playing with it and understanding how it worked.
- 03-14-2012, 01:54 AM #14
Re: how to make a string go diagonaly
You may be lucky. Not everyone can do that.
I agree with this. You have to let the OP make mistakes. Writing their code for them keeps them from doing that.learnt from their mistakes
-
Re: how to make a string go diagonaly
If thats the case then why does a site like stackoverflow have as many hits as photobucket, whereas java-forums is off the radar.
I can't speak for you, but I know that a lot of people require an example first to learn by, myself included.
- 03-14-2012, 01:09 PM #16
Re: how to make a string go diagonaly
The problem with handing out code like that is exactly what has already been said, but also the fact that a huge part of programming is learning how to solve problems. Taking a problem and breaking it up into smaller pieces, and then solving those pieces one at a time, is a skill that's just as important as learning the syntax- and it's a lot harder to teach. Giving somebody the code helps them understand the syntax, but it takes away the process of breaking a problem down, which actually hurts the person you're trying to help.
Looking at an example is fine, but what you provided is a full solution, not an example. Showing them the syntax of a for loop or showing how to solve a similar problem would provide an example that would help with the syntax without taking away the problem solving process.
What happens when the OP is taking a test that contains a similar problem that can't be solved in the same syntactic way? Is he going to be able to go look at a full solution on Stack Overflow? Or should he understand the underlying process of solving a problem?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Can't make a textfield = string.
By Tainted in forum New To JavaReplies: 10Last Post: 01-29-2012, 01:10 AM -
Make String into chars
By myst in forum New To JavaReplies: 19Last Post: 06-20-2010, 04:24 PM -
make a string as a link
By pharo in forum AWT / SwingReplies: 1Last Post: 04-26-2009, 12:23 PM -
how do i make a string return a number?
By pjr5043 in forum New To JavaReplies: 6Last Post: 09-15-2008, 04:56 AM -
make a variable name from a string?
By Kinnikinnick in forum New To JavaReplies: 3Last Post: 11-13-2007, 03:54 PM


5Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks