Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用字符串标记器和缓冲区。_Java_String_Parsing_Stringtokenizer_Stringbuffer - Fatal编程技术网

Java 如何使用字符串标记器和缓冲区。

Java 如何使用字符串标记器和缓冲区。,java,string,parsing,stringtokenizer,stringbuffer,Java,String,Parsing,Stringtokenizer,Stringbuffer,我正在开发一个程序来反转一个电话号码,删除所有分隔符,并将其与原始号码进行比较,以确定它是否是回文。它还会将电话号码转换为带逗号的整数。我有几个问题。我有错误,不知道为什么。此外,它将无法正确确定该数字是否为回文。任何帮助都将不胜感激 //Phone String Palindrome Conversions //This program will turn a phone number around and check to see if it is a palindrome //This p

我正在开发一个程序来反转一个电话号码,删除所有分隔符,并将其与原始号码进行比较,以确定它是否是回文。它还会将电话号码转换为带逗号的整数。我有几个问题。我有错误,不知道为什么。此外,它将无法正确确定该数字是否为回文。任何帮助都将不胜感激

//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) );
}
}

由于要比较字符串,请使用equals方法

换成

    if(phonebkwd.equals(phoneshort2))

另外,请指定您遇到的错误。

您的程序中确实存在一些问题

  • 正如wipindipy10所说,使用equal方法更改字符串比较

  • 若打印条件是否为回文,则更改为该条件

    if (!pal) {
        System.out.println("Your phone number is a palindrome!");
    } else {
        System.out.println("Your phone number is not a palendrome...");
    }
    
  • 正如你提到的,异常发生在底部附近。这可能是因为您输入的手机超过Integer.MAX_值,即2147483647[0x7fffffff]


  • 代码中有许多错误。 对于字符串比较,请使用equals方法

    if(phonebkwd.equals(phoneshort2))
    
    你下面的if语句是错误的

    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...");
    
    你应该交换if,否则阻塞。
    Integer.parseInt(phoneshort2)的最后一个错误;似乎您正在尝试解析一个大于整数的数字。MAX_VALUE

    int number=Integer.parseInt(phoneshort2);它接近底部,导致程序崩溃。我不确定这是否是问题所在,或者是我的其他原因导致了这个问题。谢谢你检查你打印“你的电话号码是回文!”和“你的电话号码不是回文…”的条件,应该是相反的。好吧,我这么做了,但有时我还是会出错,我的电话号码总是回文。我似乎不明白为什么。你的比较。在比较两个值时,请记住使用==。另外,当您的条件语句是布尔语句时,您可以只使用“if(bVal)”表示true,或者使用“if(!bVal)”表示false。您可以将代码更改为just if…els如果您的输入少于或等于10位,则不会显示错误。问题:您对允许的位数有限制吗?应该是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...");