Results 1 to 1 of 1
  1. #1
    tangel is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Need help with while loops

    I am supposed to write a program that asks the user to enter an English sentence, phrase or paragraph, and then test the input for various properties.

    The program has to allow the user to enter a sentence (through a dialog), and will respond by telling the user the answer to the following questions:

    Is the input a palindrome?


    here is what i have so far:

    import java.util.*;

    public class Lab4
    {
    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);

    //variables

    String input ;
    String line;
    int length, pos1, pos2;
    char char1, char2;

    // Prompt, get, and save sentence
    System.out.println("Please enter a sentence:");
    input = in.nextLine();
    System.out.println("Your input was: " + input + ".");

    // Is the input a palindrome?
    pos1 = 0;
    pos2 =input.length()-1;

    while (pos2>0)
    {
    char1=input.charAt(pos1);
    char2=input.charAt(pos2);

    if(char1 == char2)
    {
    pos1++;
    pos2--;
    }

    else if (char1 == ',' || char1 == '.' ||
    char1 == '-' || char1 == ':' ||
    char1 == ';' || char1 == ' ')
    pos1++;
    else if (char2 == ',' || char2 == '.' ||
    char2 == '-' || char2 == ':' ||
    char2 == ';' || char2 == ' ')
    pos2--;
    else
    break;
    }
    System.out.println();


    if (pos1< pos2)
    System.out.println ("That string is NOT a palindrome.");
    else

    System.out.println ("That string IS a palindrome.");
    }
    }

    i dont understand what i'm doing wrong. i am comparing the last and first character and moving toward the center. i also attempted to make sure it skipped punctuation and spaces. can somebody please help me?
    Last edited by tangel; 10-04-2011 at 08:45 PM. Reason: Redid the program

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  2. Help with Loops
    By Spyderpig in forum New To Java
    Replies: 10
    Last Post: 02-17-2011, 09:10 AM
  3. loops
    By curioustoknow in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 02:45 PM
  4. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 07:51 PM
  5. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 12:32 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •