Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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_Regex - Fatal编程技术网

Java 我不知道为什么输出会重复多次,也不知道如何格式化输出。

Java 我不知道为什么输出会重复多次,也不知道如何格式化输出。,java,regex,Java,Regex,我想从字符串中提取最后四位数字,并将其视为邮政编码 此代码将接收用户输入,检查最后四个字符是否为数字 如果是,则将其提取,并在屏幕上显示。 如果不是,则返回错误 现在,我想我可以提取它,但我不知道如何用格式显示它(我不能简单地覆盖toString()方法,因为我项目中的许多其他部分需要格式显示,而每个部分使用不同的格式) 另外,为什么代码生成多个输出,而不是一个 代码如下: public static void main(String[] args) { String addre

我想从字符串中提取最后四位数字,并将其视为邮政编码

此代码将接收用户输入,检查最后四个字符是否为数字

如果是,则将其提取,并在屏幕上显示。 如果不是,则返回错误

现在,我想我可以提取它,但我不知道如何用格式显示它(我不能简单地覆盖toString()方法,因为我项目中的许多其他部分需要格式显示,而每个部分使用不同的格式)

另外,为什么代码生成多个输出,而不是一个

代码如下:

    public static void main(String[] args) {
    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();
    String postcode = "1234";
    //this part extract postcode for further process.
    for(int count=1;count<5;count++)
    {
        if(Character.isDigit(address.toCharArray()[address.length()-count]))
        {
            String[] split = postcode.split(address); 
            System.out.format("%s %s %n", "PO is: ", split);
        }
        else
        {
            System.out.println("PO invalid!");
            System.exit(0);
        }
    }
}
publicstaticvoidmain(字符串[]args){
字符串地址;
扫描仪scn=新扫描仪(System.in);
地址=scn.next();
address=address.trim();
字符串postcode=“1234”;
//这部分提取邮政编码以供进一步处理。

对于(int count=1;count我在解决您的问题时采用了不同的方法

public static void main(String[] args) {

    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();
    String postcode = "";

    if(address.length() < 4) {
        System.out.println("The address is less that four characters long.");

    } else {

        postcode = address.substring(address.length() - 4);

        try {
            int code = Integer.parseInt(postcode);
            System.out.println("The post code is :" + code);

        } catch(NumberFormatException ex) {
            System.out.println("ERROR");
        }

    }
}
publicstaticvoidmain(字符串[]args){
字符串地址;
扫描仪scn=新扫描仪(System.in);
地址=scn.next();
address=address.trim();
字符串邮政编码=”;
if(address.length()<4){
System.out.println(“地址长度小于四个字符”);
}否则{
邮政编码=address.substring(address.length()-4);
试一试{
int code=Integer.parseInt(邮政编码);
System.out.println(“邮政编码为:“+code”);
}捕获(NumberFormatException ex){
System.out.println(“错误”);
}
}
}

在解决您的问题时,我采取了不同的方法

public static void main(String[] args) {

    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();
    String postcode = "";

    if(address.length() < 4) {
        System.out.println("The address is less that four characters long.");

    } else {

        postcode = address.substring(address.length() - 4);

        try {
            int code = Integer.parseInt(postcode);
            System.out.println("The post code is :" + code);

        } catch(NumberFormatException ex) {
            System.out.println("ERROR");
        }

    }
}
publicstaticvoidmain(字符串[]args){
字符串地址;
扫描仪scn=新扫描仪(System.in);
地址=scn.next();
address=address.trim();
字符串邮政编码=”;
if(address.length()<4){
System.out.println(“地址长度小于四个字符”);
}否则{
邮政编码=address.substring(address.length()-4);
试一试{
int code=Integer.parseInt(邮政编码);
System.out.println(“邮政编码为:“+code”);
}捕获(NumberFormatException ex){
System.out.println(“错误”);
}
}
}

使用String.substring()怎么样


使用String.substring()怎么样

试试这个:

    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();
    String postcode = "1234";
    //this part extract postcode for further process.
    for(int count=4;count>0;count--)
    {
        if(Character.isDigit(address.toCharArray()[address.length()-count]))
        {
            System.out.format("%s %s %n", "PO is: ", address.toCharArray()[address.length()-count]);
        }
        else
        {
            System.out.println("PO invalid!");
            System.exit(0);
        }
    }
试试这个:

    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();
    String postcode = "1234";
    //this part extract postcode for further process.
    for(int count=4;count>0;count--)
    {
        if(Character.isDigit(address.toCharArray()[address.length()-count]))
        {
            System.out.format("%s %s %n", "PO is: ", address.toCharArray()[address.length()-count]);
        }
        else
        {
            System.out.println("PO invalid!");
            System.exit(0);
        }
    }

我的代码基于您的想法,否则以下是工作代码:

public static boolean isIntegerParseInt(String str) {
    try {
        Integer.parseInt(str);
        return true;
    } catch (NumberFormatException nfe) {}
    return false;
}

 public static void main(String []args){

    String postcode;
    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();

    if(address.length() > 3)
    {
        String testPostCode = address.substring(address.length() - 4, address.length());
        if(isIntegerParseInt(testPostCode))
        {
            postcode = testPostCode;
            System.out.println("postcode:" + postcode);
        }
        else
        {
            System.out.println("there is not valide address.");
        }
    }
 }

我的代码基于您的想法,否则以下是工作代码:

public static boolean isIntegerParseInt(String str) {
    try {
        Integer.parseInt(str);
        return true;
    } catch (NumberFormatException nfe) {}
    return false;
}

 public static void main(String []args){

    String postcode;
    String address;
    Scanner scn = new Scanner(System.in); 
    address = scn.next();
    address=address.trim();

    if(address.length() > 3)
    {
        String testPostCode = address.substring(address.length() - 4, address.length());
        if(isIntegerParseInt(testPostCode))
        {
            postcode = testPostCode;
            System.out.println("postcode:" + postcode);
        }
        else
        {
            System.out.println("there is not valide address.");
        }
    }
 }

您的代码最多打印4次,因为它位于最多运行4次的for循环中;).是的。我知道这一点…现在我想尝试一种不同的方法。你的代码最多打印4次,因为它在一个最多运行4次的for循环中;)。是的。我知道这一点…现在我想尝试一种不同的方法。我想如果我输入
abcdefg1234
结果将是
efg1234
,这将导致代码失败。另外,s由于这是HW,请不要只发布有效的代码,试着解释发生了什么,理想情况下,OP的方法有什么问题。事实上,邮政编码位于地址的结尾部分,因此无法提取正确的邮政编码。无论如何,感谢您的帮助。是的,看看这个,
postcode=address.substring(address.length()-4)
我们将密码作为地址的最后4个字符,这是您想要的。我认为如果我输入
abcdefg1234
结果将是
efg1234
,这将导致代码失败。此外,由于这是硬件,请不要只发布有效的代码,尝试解释发生了什么,理想情况下,是什么是wro不同意OP的方法。事实上,邮政编码位于地址的结尾部分,因此无法提取正确的邮政编码。无论如何,感谢您的帮助。是的,看看这个,
postcode=address.substring(address.length()-4)
我们将密码作为地址的最后4个字符,这就是你想要的是。我想使用子字符串,但我应该说什么,你的代码比我以前的代码更容易理解,而且它确实很好!感谢你的出色工作。是的。我想使用子字符串,但我应该说什么,你的代码太复杂了比我以前的方法更容易理解,而且效果非常好!感谢你的出色工作。现在输出分成四个独立的数字,而不是一个邮政编码。因为for循环不是一个好主意,我打算放弃这种方法。呃,现在输出分成四个独立的数字,而不是一个邮政编码。因为r循环并不是一个好主意,我将放弃这种方法。很抱歉评论太晚。代码很有效。我从中学到了很多。谢谢你的帮助!很抱歉评论太晚。代码很有效。我从中学到了很多。谢谢你的帮助!