Results 1 to 3 of 3

Thread: Problem...

  1. #1
    jpnym15 is offline Member
    Join Date
    Nov 2007
    Posts
    19
    Rep Power
    0

    Default Problem...

    import java.util.*;

    public class SentenceSplit
    {
    public static void main (String[] args)
    {
    Scanner in = new Scanner (System.in);

    String userInput;
    String output;
    int stringsize;

    System.out.println("Enter a sentence");
    userInput = in.nextLine();

    stringsize = userInput.length();
    output = userInput.substring(stringsize/2);
    System.out.println(output);




    }
    }

    That is my program, and it works fine and everything...but what would i need to do in order for each letter of the sentence to go on its own line?

    ie. H
    e
    l
    l
    o

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Something like this should work.

    Java Code:
    for(int i = 0; i <= stringSize; i++)
    {
    	System.out.println(userInput.indexOf(i,i));
    }
    I didn't test it but that should be the right idea.

  3. #3
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Default

    well i don't know if u like this, u can use charAt()
    u got to form the loop

    Java Code:
    for (int i = 0; i<stringsize; i++) {
       System.out.println(userInput.charAt(i) + "\n");
    }
    this way it will read the char from the string by the location and then output it to the command prompt and the \n is for next line

Posting Permissions

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