When I create a class, I receive “illegal start of type” error in “try-catch” block

后端 未结 2 1487
暖寄归人
暖寄归人 2021-01-17 06:40
class address
{

String address;
String newaddr = address.trim();
final int ziplength =4;
    String input;
    Scanner in = new Scanner(System.in);
    String temp          


        
2条回答
  •  春和景丽
    2021-01-17 07:25

    class address
    {
        String address;
        String newaddr = address.trim();
        final int ziplength =4;
        String input;
        Scanner in = new Scanner(System.in);
        String temp = in.next();
        String zipcode = input.substring(input.length()-ziplength);
    public address() //this is the only thing I add, but it eliminate "illegal start type error"
            {
        try 
        {
            Integer.parseInt(zipcode);
            System.out.println("PO is: "+zipcode);    
        }
        catch( Exception e) 
        {
            System.err.println("Last 4 chars are not a number.");
        }
            }
    }
    

    Special thank you for @Jägermeister . He gives me valuable hint.

    Since I am a beginner, I am thinking a better way to improve my skills. I will try more.

提交回复
热议问题