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

Thread: Hey fellas whats wrong with my code? Im having problems here

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hey fellas whats wrong with my code? Im having problems here

    Write a Java program that takes as input one word W as a string (as args[0]), and checks if that word is a palindrome.

    Use the function charAt(index) to get the character at the index position in the string, and use
    N = W.length() to record the length of the string.

    Then, create a loop that will start with index = 0 and iterate to index < N / 2, and check if W.charAt(index) equals the character W.charAt(N-1-index).

    After the loop, if any two characters differ, print (W + “ is not a palindrome”); else, if all characters are
    the same, print (W + “ is a palindrome”).



    import java.util.Scanner;

    public class A23P1 {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Type a word");
    String W = input.nextLine();

    {
    int N = W.length();
    for (int i = 0; i < N / 2; i++)
    if (W.charAt(i) != W.charAt(N-1-i))
    return false;
    return false;
    }
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Hey fellas whats wrong with my code? Im having problems here

    You have open the { brace and return false; unnecessarily. Because you don't create any method

    you create the code like this..

    if (W.charAt(i) != W.charAt(N-1-i))
            {
               //print your statement here..
            }
            else{
                 //print your statement here..
            }
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hey fellas whats wrong with my code? Im having problems here

    You should ALWAYS use {}s with if statements and loops to prevent errors when the code is later modified.

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

    Copy the full text of the error messages you get and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help me with a code, whats wrong?
    By Heizzer10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 17th, 2012, 11:30 AM
  2. Can someone see whats wrong with my code, please?
    By DJBENZ10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 11th, 2012, 08:54 PM
  3. Whats wrong with my code?
    By Bryan29 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 5th, 2011, 09:12 AM
  4. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM