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!
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.
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 12:40 PM.
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:
- 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";- Use the String length() method to find the number of characters in the String. Print that value with println()
- Use the String charAt() method to find the first character in the String. Print that character with println()
- 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().
- 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.