Results 1 to 16 of 16
Like Tree5Likes
  • 1 Post By jaki312
  • 1 Post By Norm
  • 1 Post By Norm
  • 2 Post By KevinWorkman

Thread: how to make a string go diagonaly

  1. #1
    jaki312 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default 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);
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

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

  3. #3
    jaki312 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

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

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,456
    Blog Entries
    7
    Rep Power
    20

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jaki312 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to make a string go diagonaly

    how do i print a space before the word
    ozzyman likes this.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,456
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to make a string go diagonaly

    Quote Originally Posted by jaki312 View Post
    how do i print a space before the word
    Are you serious? Here:

    Java Code:
    System.out.print(' ');
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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);
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

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

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: how to make a string go diagonaly

    The OP should learn to walk before trying to run.
    lannie1980 likes this.

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: how to make a string go diagonaly

    You may be lucky. Not everyone can do that.

    learnt from their mistakes
    I agree with this. You have to let the OP make mistakes. Writing their code for them keeps them from doing that.
    lannie1980 likes this.

  15. #15
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

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

  16. #16
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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?
    Norm and lannie1980 like this.
    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

  1. Can't make a textfield = string.
    By Tainted in forum New To Java
    Replies: 10
    Last Post: 01-29-2012, 01:10 AM
  2. Make String into chars
    By myst in forum New To Java
    Replies: 19
    Last Post: 06-20-2010, 04:24 PM
  3. make a string as a link
    By pharo in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2009, 12:23 PM
  4. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 04:56 AM
  5. make a variable name from a string?
    By Kinnikinnick in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •