当我创建一个类时,我会收到“非法启动类型”。 “try-catch”中的错误块 [英] When I create a class, I receive "illegal start of type" error in "try-catch" block

查看:250
本文介绍了当我创建一个类时,我会收到“非法启动类型”。 “try-catch”中的错误块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    try **//illegal start type error**
    {
        Integer.parseInt(zipcode);
        System.out.println("PO is: "+zipcode);    
    }
    catch( Exception e) **//illegal start type error**
    {
        System.err.println("Last 4 chars are not a number.");
    }
}

此代码段从字符串中提取最后四个字符,看看它们是否是邮政编码。

This code segment extract the last four characters from a string, and see if they are post code.

我评论了NetBeans中报告非法启动类型错误的要点。

I have commented the point reporting "illegal start type error" in NetBeans.

我想知道,如果我在创建课时不能使用try-catch?或者,这堂课会错过什么?

I wonder, if I cannot use try-catch when creating a class? Or, do this class miss something?

我尝试搜索stackoverflow。但我仍然感到困惑。这是一些链接。

I tried searching stackoverflow. But I am still confusing. Here are some links.

Java非法启动类型

Java错误:非法开始表达

java:为什么程序会给出非法启动类型错误?

推荐答案

Java 允许您简单地将语句放在类的主体中。你总是需要围绕这些陈述使用一个封闭的块。

Java does not allow you to simply put statements in the body of a class. You always need an enclosing "block" around those statements.

换句话说:第一个工作示例的最简单方法是向你的类添加一个main方法,将代码移到那里。含义为签名的方法 public static void main(String [] args)

In other words: the easiest way to a first working example would be to add a main method to your class and to move your code in there. Meaning a method with signature public static void main(String[] args)

除此之外:不要等待直到几个错误汇集在一起​​。从一个空课开始。在那里输入一个新构造。保存;运行编译器。转到你需要的下一个元素。

Beside that: don't "wait" until several errors come together. Start with an empty class. Enter one new construct in there. Save; run the compiler. Go for the next "element" that you need.

对于初学者,你的策略(让我们编写10行,20行代码;然后让希望它起作用)将根本不工作。你这样做是在浪费你的时间(和我们的)。你知道,这是基本的东西,你应该转向其他人向你解释。你应该从小做起,自己想出所有这些事情。因为这是学习编程的本质。

For a beginner, your strategy (lets write 10, 20 lines of code; and then lets hope it works) will not work at all. You are wasting your time (and ours) by doing it like that. You see, this is so basic stuff that you should not turn to other people to explain them to you. You should start small and figure all these things yourself. Because that is the essence of learning programming.

这篇关于当我创建一个类时,我会收到“非法启动类型”。 “try-catch”中的错误块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆