简体   繁体   中英

keep getting an error

why do i keep getting an error on "int numLength = palNum1.length();?" im trying to finish this lab that im doing for my java class and im stuck on this part. any help would be appreciated.

import java.util.*;
public class Lab6
{
    public static void main (String [] args)
    {
      String pal1, pal2="";
      int palNum1, palNum2, 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: ");
          palNum1 = in.nextInt();

          int numLength = palNum1.length();


          for ( int j = numLength - 1 ; j >= 0 ; j-- )
            palNum2 = palNum2 + palNum1.charAt(j);

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

As dasblink said, primitave types don't have methods. Given what you're trying to do if they enter an integer, you should convert it to a String then basically repeat the code from the first part. Instead of:

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

      int numLength = palNum1.length();

      for ( int j = numLength - 1 ; j >= 0 ; j-- )
        palNum2 = palNum2 + palNum1.charAt(j);

Easy thing to do is convert the int they enter into a String, then repeat code from before:

      System.out.println("Enter a bunch of numbers: ");
      palNum1 = in.nextInt();
      // Conver it to String to allow you to easily check if its a palindrome.
      pal1 = Integer.parseInt(palNum1);

      int numLength = pal1.length();

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

palNum2 is never needed.

EDIT: Sorry if I confused you, I meant that you read in the palindrome as an int(same as you originally did), but then you convert it to a String, then treat it as before. complete code for the else:

  else{
      // If you are here, user is entering an int.
      System.out.println("Enter a bunch of numbers: ");
      palNum1 = in.nextInt();
      pal1 = Integer.parseInt(palNum1);

      int numLength = pal1.length();

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

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

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