简体   繁体   中英

keep getting an error with this code

how do i make the else statement input whether the numbers that i entered are palindrome or not? the first part works nad im just stuck in the else statement trying to figure out how to make it work. heres my code

import java.util.*;
public class Lab6
{
    public static void main (String [] args)
    {
      String pal1, pal2="";
      int choice;
      Scanner in = new Scanner(System.in);


      System.out.println("Word(w) or Number(n)?");
      choice = in.nextLine().charAt(0);

      if (choice == 'w') {

          System.out.println("Enter a word: ");
          pal1= in.nextLine();

          int length = pal1.length();


          for ( int i = length - 1 ; i >= 0 ; i-- )
            pal2 = pal2 + pal1.charAt(i);


         if (pal1.equals(pal2))
            System.out.println("The word you entered is a palindrome.");
         else
            System.out.println("The word you entered is not a palindrome.");
        }
      else{

          System.out.println("Enter a bunch of numbers: ");
          pal1 = in.nextLine();

          pal1 = String.valueOf(in.nextInt());
          int numLength = pal1.length();

          for ( int j = numLength - 1 ; j >= 0 ; j-- )
            pal2 = pal2 + pal1.charAt(j);

         if (pal1.equals(pal2))
            System.out.println("The numbers you entered is a palindrome.");
         else
            System.out.println("The numbers you entered is not a palindrome.");
        }
    }
}

Your question is very ambiguous, if you are trying to tell the user that the string entered is NOT a palindrome, then see bellow...

Have you tried putting the if statement in brackets? you have to careful when writing if/for statement without them.

public class Lab6
{
    public static void main (String [] args)
    {
      String pal1, pal2="";
      int choice;
      Scanner in = new Scanner(System.in);


      System.out.println("Word(w) or Number(n)?");
      choice = in.nextLine().charAt(0);

      if (choice == 'w') {

          System.out.println("Enter a word: ");
          pal1= in.nextLine();

          int length = pal1.length();


          for ( int i = length - 1 ; i >= 0 ; i-- )
            pal2 = pal2 + pal1.charAt(i);


         if (pal1.equals(pal2)){
            System.out.println("The word you entered is a palindrome.");
         } else{
            System.out.println("The word you entered is not a palindrome.");
         }
        }
      else{

          System.out.println("Enter a bunch of numbers: ");
          pal1 = in.nextLine();

          pal1 = String.valueOf(in.nextInt());
          int numLength = pal1.length();

          for ( int j = numLength - 1 ; j >= 0 ; j-- )
            pal2 = pal2 + pal1.charAt(j);

         if (pal1.equals(pal2))
            System.out.println("The numbers you entered is a palindrome.");
         else
            System.out.println("The numbers you entered is not a palindrome.");
        }
    }
}

Good luck with your lab ;)

Also something like this might be more efficient:

boolean isPal(String input) {
    // go through half the string length
    for (int i = 0; i < input.length() / 2; i++) {
        // match first half to second half (regardless if odd or even) 
        // -1 because strings starta at a 0 index
        if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
            return false;
        }
    }
    return true;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM