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: Intro to java assignment help

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

    Default Intro to java assignment help

    hey guys,

    I'm new to java and was given this assignment in class:

    Write an application that determines whether a phrase entered by the user is a
    palindrome. A palindrome is a phrase that reads the same backward and forward
    without regarding capitalization or punctuation. For example, “Dot saw I was Tod”,
    “Was it a car or a cat I saw?”, and “Madam, I’m Adam” are palindromes. Save the file
    as Palindrome.java.

    so far I have this:

    // Purpose:   determines whether a phrase is a palindrome
    // Date:       9/19/2012
    // Filename:   Palindrome.java
    // Input:       a string from the user
    // Output:     "It is a palindrome" or "It isn't a palindrome"
     
     
    import java.util.Scanner;
    public class Palindrome
    {
     
    public static void main(String[] args)
    {
     
    //get input
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter the word or prase: ");
    String testWord = input.nextLine();
     
     
    //create variables
    //upperWord is used to convert the entire phrase to upper so as to ignore the capitalization
    //reverseWord is the uppercase phrase in reverse
     
    StringBuilder upperWord = new StringBuilder(testWord.toUpperCase() + 16);
    StringBuilder reverseWord = new StringBuilder(upperWord.length() + 16);
     
     
    //reverse the word
    for(int x = 0; x != (testWord.length() - 1) ; ++x)
    {
    	reverseWord.setCharAt(x, testWord.charAt(testWord.length() - x - 1)); //ERROR HERE. 0 index is out of bounds.
    }
     
    //compares the two words
    int counter = 0;
    char isEqual = 'y';
    while(counter != upperWord.length() || isEqual == 'y')
    {
    	if(upperWord.charAt(counter) != reverseWord.charAt(counter))
    		isEqual = 'n';
    }
     
    //display output
    if(isEqual == 'y')
    	System.out.println(testWord + "is a palindrome!");
    else
    	System.out.println(testWord + "is not a palindrome.");
     
     
    }
    }


    I've got a problem with my for loop, which I marked above. I feel like this is a simple fix but haven't been able to figure it out yet. Any help fixing this or making it more efficient would be much appreciated. thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Intro to java assignment help

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    index is out of bounds.
    What is the value of the index and what is the size of the String being indexed? Remember indexing starts at 0 and goes to the length-1.

    Print out the index and the String so you can see what the values the computer sees and determine the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. intro
    By Syed Muhammad Hassnain I. in forum Member Introductions
    Replies: 1
    Last Post: October 19th, 2012, 03:50 AM
  2. Intro: New Here
    By patohz in forum Member Introductions
    Replies: 2
    Last Post: March 8th, 2012, 05:15 PM
  3. Intro to java hw assignment
    By coke32 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 07:45 AM
  4. Intro
    By LMO in forum Member Introductions
    Replies: 1
    Last Post: February 4th, 2011, 04:43 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM