Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: help with loops

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with loops

    hi,

    i am trying to write a programme that reads in a word, and outputs the last charecter, then the last two then the last three and so on
    e.g
    input: java
    output: a, va, ava, java

    i have written the code but i have mixed up my loop, insteed of reading the last character first, it will start with the first one
    e.g
    input: java
    output: j, ja, jav, java

    here is my code, any advice where i have gone wrong would be greatly appreciated
    package Loop;
    import java.util.Scanner;
     
    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
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loops

    Java indexes strings starting from the zeroth (furthest left) character.

    word.substring(0, i + 1)

    This line needs to be changed to reflect that fact (hint: you have it slightly backwards).

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loops

    i've spent hours trying but i still can't seem to get it, any more tips ?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loops

    What have you tried? That hint was one step from giving you the complete answer. Think about what you're getting and why (use the information in my last post).

Similar Threads

  1. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 01:38 PM
  2. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 04:31 PM
  3. Array and loops.
    By Melawe in forum Loops & Control Statements
    Replies: 46
    Last Post: August 15th, 2010, 04:49 PM
  4. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 09:11 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 04:12 AM