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 2 of 2

Thread: How to print numbers using do while, for loop and if else statement?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to print numbers using do while, for loop and if else statement?

    This is the problem and you are assuming that the String, word, has been initialized.

    I'm honestly not even sure where to start; I'm looking for some help more than anything right now.

    Thanks!
    Last edited by Deep_4; November 7th, 2012 at 11:40 AM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Print every character in the String, word, in backward order on a separate line?

    Quote Originally Posted by techlover View Post
    ...
    I'm honestly not even sure where to start;...
    Sometimes the very first time is the hardest. (Unfortunately, sometimes they get harder and harder. But you still have to get past that first one, right?)

    Anyhow...

    Here's how I might start:

    1. Make a program that defines a String and uses the System.out.println() method to print it. Eventually you may want to allow the program to read a String from user input. For now, just define the String it with something like
          String str = "whatever";

    2. Use the String length() method to find the number of characters in the String. Print that value with println()

    3. Use the String charAt() method to find the first character in the String. Print that character with println()

    4. Use the String charAt() method along with the result from the length() method to find the last character in the String. Print that character with println().

    5. Make a for(){} loop that has an integer variable that starts with the index value of last character in the string and goes down to the index value of the first character in the String. Inside the loop, use println() to print the String character for that index value.


    Here. I'll even get you started:
    public class StringCharacters {
     
        public static void main(String [] args) {
     
            String str = "Lycanthrope";
     
            // Print the entire String
            System.out.println("The string is " + str);
     
            // Use the String length() method to find the number of characters
            System.out.println("The number of characters in the String is " + str.length());
     
            // Use the String charAt() method to print the very first character
            System.out.println("The first character in the String is " + str.charAt(0));
     
            // Use the String charAt() method with the length() method to print the
            // very last character in the string
            System.out.println("The last  character in the String is " + str.charAt(str.length()-1));
     
            // This is a warmup exercise.  It is not what your assignment
            // requires, but it's good practice:
            //
            // 1.
            // Make a loop that prints all the characters, one at a time.
            // Inside the loop, print each character on a separate line
            // with println();
            //
            // Use a for(){} loop that starts at index value for the first
            // character in the String and increments until it has printed
            // the last character.
            //
     
            // This is the "real deal."  If you have followed everything up
            // to this point, it should be a snap.
            //
            // 2.
            // Make a loop that prints all of the characters, one at a time.
            // Inside the loop, print each character on a separate line
            // with println();
            //
            // Use a for(){} loop that starts at index value for the last
            // character in the String and decrements until it has printed
            // the first character.
            // 
        } // End main()
    } // End class definition

    Output of the Program, so far:

    The string is Lycanthrope
    The number of characters in the String is 11
    The first character in the String is L
    The last character in the String is e


    Cheers!

    Z
    Last edited by Zaphod_b; October 30th, 2012 at 02:16 PM.

Similar Threads

  1. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  2. line plot Print statements
    By lf2killer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 20th, 2012, 06:47 AM
  3. Print out a triangle by character *
    By Genky in forum What's Wrong With My Code?
    Replies: 26
    Last Post: August 20th, 2012, 06:33 AM
  4. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  5. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM