Results 1 to 9 of 9
  1. #1
    jp011 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Help with program

    getting incorrect output, all help would be useful...

    INPUT: Hello
    OUTPUT(NOW):H, He, Hel, Hell, Hello
    OUTPUT (NEEDED): o, lo, llo, ello, Hello

    Code till now:

    public class Loop {

    public static void main(String[] args) {

    String word;
    String output = "";
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter a string: ");
    word = scan.next();

    int numbers = word.length();

    for (int i = 0; i < numbers; i++)
    {
    output += word.substring(0, i + 1) + ",";
    }
    System.out.println(output+ "\n");

    }

    }

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

    Default

    reverse your loop

    Java Code:
    for (int i=numbers-1; i>=0; i--)
    that way you start from the last character, and go down to the first.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    You used substring() incorrectly. You had it start from the beginning of "Hello" and you want it to start from the end of "Hello".

    Java Code:
    public class Loop {
    
        public static void main(String[] args) {
         
        String word;
        String output = "";
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a string: ");
        word = scan.next();
        
        int numbers = word.length();
    
    for (int i = 1; i <= numbers; i++)
    {
    output += word.substring(numbers-i) + ",";
    }
    System.out.println(output+ "\n");
    
    }
        
    }
    Last edited by Solarsonic; 03-29-2011 at 12:04 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do not spoon feed code solar. I feel like a broken record. Try to be more vague with your help so they think about it and solve it themselves. I do understand his mistakes was small but a vague statement can definitely be more helpful.

  5. #5
    jp011 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Worked but...

    It is now giving me this:

    Input: Hello
    Output: o, lo, llo, ello,

    It is not going on from "ello" to "hello"
    don't know what's wrong. Please help.

    Quote Originally Posted by Solarsonic View Post
    You used substring() incorrectly. You had it start from the beginning of "Hello" and you want it to start from the end of "Hello".

    Java Code:
    public class Loop {
    
        public static void main(String[] args) {
         
        String word;
        String output = "";
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a string: ");
        word = scan.next();
        
        int numbers = word.length();
    
    for (int i = 1; i < numbers; i++)
    {
    output += word.substring(numbers-i) + ",";
    }
    System.out.println(output+ "\n");
    
    }
        
    }

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

    Default

    if you need to iterate once more,
    do this:
    i <= numbers

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Think about why it isn't working instead of just asking for the answer. How many times does this loop happen?

    Java Code:
    String  s = "Hello";
    for(int i = 0; i < s.length(); i++){}

  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

    Quote Originally Posted by sunde887 View Post
    Think about why it isn't working instead of just asking for the answer. How many times does this loop happen?

    Java Code:
    String  s = "Hello";
    for(int i = 0; i < s.length(); i++){}

    that doesn't change anything because he's already written this:
    Java Code:
    int numbers = word.length();

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    He just did the same as me, indirectly. It still helps him to think about it instead of continuously coming here asking for the answer.

    If he doesn't take the time to really understand what the problem is, later assignments are only going to be more difficult.

Similar Threads

  1. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  3. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •