2016-08-02 83 views
-1

這段代碼應該檢查一個數是否是迴文。 每當我運行方法檢查迴文時,它出現java.lang.StringOutOfBoundsExceptionjava.lang.StringOutOfBoundsException找不到

請幫忙。

import java.util.*; 

/** 
* Lab 1 . 
* @author Kevin Rasquinha 
* @version 30 July 2016 
*/ 
public class Lab1 
{ 
    private Scanner scan = new Scanner(System.in); 

    /** 
    * count the number of digits in a number 
    * @param num the number to analyse 
    * @return the number of digits it has 
    */ 
    public int numDigits (int num) 
    { 
     int nDigits = 0; 
     int digit; 
     while (num>0) 
     { 
      digit = num % 10;  // take off the last digit 
      num = num /10;   // reduce the number 
      nDigits = nDigits + 1; // increment count of digits 
     } 
     return nDigits; 
    } 

    /** 
    * Read a number from the keyboard, and report how many digits it has. 
    * Ensure the number is within a desired range. 
    */ 
    public void countDigits() 
    { 
     int num=0; 
     while (num<1 || num > 1000) 
     { 
      System.out.print("What number (1 to 1000)? "); 
      num = scan.nextInt(); 
      scan.nextLine(); 
      System.out.println("Num = " + num); 
      if (num<1 || num > 1000) 
       System.out.println("1 to 1000, please"); 
     } 
     System.out.println (num + " has " + numDigits(num) + " digits"); 
    } 

    /** 
    * method used to if a number is the sum of the cube of its digits 
    * @param args (not used) 
    */ 
    public void sumCubesDigits() 
    { 
     for (int initial = 1; initial < 1000; initial ++) 
     { 
      int num = initial; 
      int thirddig = num%10; 
      num = num/10; 
      int secdig = num%10; 
      num = num/10; 
      int firstdig = num%10; 
      int sum = (thirddig*thirddig*thirddig) + (secdig*secdig*secdig) + (firstdig*firstdig*firstdig); 
      if (sum == initial) 
      { 
       System.out.println ("The number " + initial + " is equal to the sum of the cube of its digits."); 
      } 
     } 
    } 

    /** 
    * Recieves an int and writes the same int backwards 
    * @param args (not used) 
    */ 
    public int backwards(int num) 
    { 
     int rev = 0; 
     int value = num; 
     while (value != 0) 
     { 
      rev = rev*10; 
      rev = rev + value%10; 
      value /= 10; 
     } 
     return rev; 
    } 

    /** 
    * Receives digit from method backwards 
    * Asses whether backwards = the original number 
    * @param args (not used) 
    */ 
    public boolean palindrome (int num, int digit) 
    { 
     boolean a = false; 
     int normal = num; 
     int reversed = digit; 
     if(reversed == digit) 
     { 
      a = true; 
     } 
     return a; 
    } 

    /** 
    * Recieves int from user - num 
    * Sends num to backwards 
    * Sends num to palindrome 
    * Recieves boolean from palindrome 
    * Outputs message to user 
    */ 
    public void checkPalindrome() 
    { 
     System.out.println ("Enter number to be see if it is a palindrome"); 
     int num = scan.nextInt(); 
     int digit = backwards(num); 
     boolean check = palindrome (num, digit); 
     if (check = true) 
     { 
      System.out.println("Your number is a palindrome"); 
     } 
    } 

    /** 
    * Present a menu to the user, and obtain their selection. If they 
    * type an erroneous value, report it and try again. Either upper 
    * case or lower case input is accepted. 
    * @return an upper case character showing the user's choice 
    */ 

    public char menuChoice() 
    { 
     System.out.println(""); 
     System.out.println("What do you want to do?"); 
     System.out.println("(c) Count the digits in a number"); 
     System.out.println("(g) Find out the numbers where the the sum of the cube of its digits is equal to it"); 
     System.out.println("(p) Find out if a number is a palindrome"); 
     System.out.println("(q) Quit"); 
     System.out.print("Your choice? "); 
     char answer = ' '; 
     boolean ok = false; 
     while (! ok) 
     { 
      answer = scan.nextLine().trim().toUpperCase().charAt(0); 
      ok = (answer == 'C' || answer == 'Q' || answer == 'G' || answer == 'P'); 
      if (! ok) 
      { 
       System.out.println("Please type one of c,C,q,Q,g,G,p,P"); 
       System.out.print("Your choice? "); 
      } 
     } 
     return answer; 
    } 

    /** 
    * test driver for the program 
    */ 
    public void test() 
    { 
     char answer = ' '; 
     while (answer != 'Q') 
     { 
      answer = menuChoice(); 
      switch (answer) 
      { 
       case 'C': countDigits(); break; 
       case 'G': sumCubesDigits(); break; 
       case 'P': checkPalindrome();break; 
       case 'Q': break; 
      } 
     } 
    } 

     /** 
    * main program: create a test driver and let it loose 
    * @param args (not used) 
    */ 
    public static void main (String [] args) 
    { 
     Lab1 l1 = new Lab1(); 
     l1.test(); 
    } 
} 
+0

這不是你的異常的原因,但如果'(檢查=真)'是一個賦值,而不是一個比較。你的意思是「如果(檢查)」。 –

+1

這裏有各種不相關的代碼,比如'sumCubesDigit'。請刪除與您遇到的錯誤無關的所有內容,併發布堆棧跟蹤。 –

+0

在「迴文」方法的中間,你有'int reversed = digit;'後面跟着'if(reversed == digit)'。這真的是你想寫的嗎?在我看來,這將永遠是事實。 –

回答

0

一個StringIndexOutOfBoundsException當您使用charAt選擇從一個字符串,它是不是有一個角色大多是拋出。

我猜你的問題就在這裏:

answer = scan.nextLine().trim().toUpperCase().charAt(0); 

,因爲這是在你的代碼的唯一charAt

如果用戶什麼都不輸入,該行會怎麼做?當然投擲一個Exception

0

你應該改變if (check = true)if (check == true)answer = scan.nextLine().trim().toUpperCase().charAt(0);answer = scan.nextLine(); if(answer != null) answer = answer.trim().toUpperCase().charAt(0);