2014-02-12 34 views
0

我正在製作一個程序來轉換電話號碼,刪除所有分隔符,並將其與原始數字進行比較以確定它是否是迴文。它還會將電話號碼轉換爲帶逗號的整數。我有幾個問題。我收到錯誤,不知道爲什麼。此外,它不會正確確定該數字是否是迴文。任何幫助,將不勝感激。如何使用字符串標記器和緩衝區。

//Phone String Palindrome Conversions 
//This program will turn a phone number around and check to see if it is a palindrome 
//This program will remove all deasdspace and symbols from the phone number 
//This program will reverse the string and compare it to the original 
//This program will put a phone number in a long intiger format 
import java.io.*; 
import java.util.*; 
import java.text.DecimalFormat; 
public class DottinoN_palindrome 
{ 
    public static void main (String [] args) throws IOException 
{ 
    String phoneshort1; 
    boolean pal; 
BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Please enter your phone number"); 
String phone1=br.readLine(); 
phoneshort1 = spaceremover(phone1); 
System.out.println(phoneshort1); 
pal = palindrometest(phoneshort1); 
System.out.println(pal); 
if(pal = false) 
{ 
    System.out.println("Your phone number is a palindrome!"); 
} 
else if(pal = true) 
    System.out.println("Your phone number is not a palendrome..."); 
numberformat(phoneshort1); 

} 
public static String spaceremover (String phone2) 
{ 
String phoneshort = ""; 
StringTokenizer st = new StringTokenizer(phone2,"()- " ,false); 
while(st.hasMoreTokens()) 
{ 
    phoneshort += st.nextToken(); 
} 
return phoneshort; 
} 
public static boolean palindrometest (String phoneshort2) 
{ 
boolean pal; 
StringBuffer br = new StringBuffer(phoneshort2); 
String phonebkwd = br.reverse().toString(); 
if(phonebkwd == phoneshort2) 
{ 
    pal = true; 
} 
else pal = false; 
System.out.println(phonebkwd + "--" + phoneshort2); 
return pal; 

} 
public static void numberformat (String phoneshort2) 
{ 
DecimalFormat formatter = new DecimalFormat("0,000,000,000"); 
int number = Integer.parseInt(phoneshort2); 
System.out.println("Your phone number as an intiger is: " + formatter.format(number)); 
} 
} 
+0

請標記相應的答案。 – wipindipy10

回答

0

由於您正在比較字符串,請使用方法equals。

if(phonebkwd == phoneshort2) 

將其更改爲

if(phonebkwd.equals(phoneshort2)) 

另外,請註明你所得到的錯誤。

+0

int number = Integer.parseInt(phoneshort2);它靠近底部並導致程序崩潰。即時通訊不知道這是問題行還是如果我有其他東西導致這不起作用。謝謝 – user2933941

+0

什麼是錯誤? – wipindipy10

+0

檢查您的條件以便打印「您的電話號碼是迴文信息!」和「你的電話號碼不是一種古怪的症狀......」應該是相反的方式。 – wipindipy10

1

有真的在你的程序中的一些問題,

  1. 爲wipindipy10說,等方法改變字符串比較

  2. 如果打印條件是否是迴文,更改爲

    if (!pal) { 
        System.out.println("Your phone number is a palindrome!"); 
    } else { 
        System.out.println("Your phone number is not a palendrome..."); 
    } 
    
  3. 正如您所提到的發生在底部附近的異常。這可能是你輸入超過Integer.MAX_VALUE的手機,也就是2147483647 [0x7FFFFFFF的]

+0

它應該只是朋友,而不是!朋友。 – wipindipy10

+0

優秀!它現在正在工作。我將Integer改爲Long,並且使用!pal和pal。謝謝! – user2933941

0

有在代碼中的許多錯誤。 對於字符串比較,使用equals方法

if(phonebkwd.equals(phoneshort2)) 

你如果以下說法錯誤的是

if(pal = false) 
{ 
    System.out.println("Your phone number is a palindrome!"); 
} 
else if(pal = true) 
    System.out.println("Your phone number is not a palendrome..."); 

你應該換,如果,否則塊。 Integer.parseInt(phoneshort2)的最後一個錯誤;似乎是你試圖分析一個大於Integer.MAX_VALUE的數字