使用jGRASP的Java编译器错误

时间:2012-05-09 03:08:31

标签: java jgrasp

我是Java的新手,在编译时遇到以下两个错误:

stringConvert3.java:29: illegal start of expression
 public static void main (String [] args) throws Exception //Main
 ^
 stringConvert3.java:57: ';' expected
private class InnerToLowerCaseString //Second class (Inner) to convert to lower case
            ^

以下是我正在使用的代码。 。

import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;

//java.lang.needed to perform the "squeeze" method on strings.


public class stringConvert3 //First class and actual program.
{

static Scanner console = new Scanner (System.in); 
public void printEven (){
String str;

 public static void main (String [] args) throws Exception //Main
    {

    String str;

    //The following imports the 5 line test text from a saved file (test.txt).
    Scanner inFile = new Scanner (new FileReader("c:\\test.txt"));
    while (inFile.hasNextLine()){
    String line = inFile.nextLine();
    System.out.printf("Scan = %s\n", line);

        String save;
        save = line;
        str = save;
        System.out.printf ("Receive: <%s>\n", str);

             InnerToLowerCaseString innerString = this.new InnerToLowerCaseString ();
             str = InnerToLowerCaseString.lowerCase (str);
             str = save;
             str = trimmed (str); //Trims and abreviates.
             System.out.printf ("Trimmed: <%s>\n", str);
             str = squeeze (str); //Squeezes text.
             System.out.printf ("Squeezed: <%s>\n", str);

             if (str.length () >= 50)
             str = str.substring (0,20);
    }       

    private class InnerToLowerCaseString //Second class (Inner) to convert to lower case
    { //Brace to start the second class.

    //Following method converts all letters of String to lower case letters.

        private String lowerCase (String str)
            {
            str = str.toLowerCase();
            System.out.printf ("Convert to Lower: <%s>\n", str);
            return str;
            }

    } //Brace to end the second class.
    }

    public class threeMethods //Third class, containing three methods.
    {
        public static String trimmed (String str) //First method.
    {
            Scanner inFile = new Scanner (new FileReader ("c:\\test.txt"));     
            return str.trim ();
    }

    public static String trimmed (String str, int len) //Second method.
    {
        str = trimmed (str);
        if (str.length () > 10)
            return str.substring (0, 10);

        else
            return str;
    }

    public static String squeeze (String str) //Third method.
    {
        int length;

        do
        {
            length = str.length ();
            str = str.replaceAll ("  ", " "); 

            }  while (length != str.length ());  
            return (str);

    } //End of squeeze section.


} // End of the main.

}//End of stringConvert3 program.

1 个答案:

答案 0 :(得分:0)

代码开头附近的printEven()方法定义中的左大括号永远不会与右大括号匹配,因此main()似乎位于printEven()内。这是不合法的 - 你不能在另一个方法中定义一个方法 - 所以编译器会抱怨。

我查看了剩下的代码,看看你有哪些其他错误,实际上有很多。它几乎就像是故意试图在其他方法中定义方法;如果你是你将不得不停止尝试并正确实施你的程序。另请注意,在方法内定义的内部类(实际上是合法的)不能具有访问限定符:如果在方法中定义InnerToLowerCaseString,则无法将其标记为private

最后请注意,尽管您发表了评论,但导入java.lang 从不是必要的,因为它总是隐式导入。明确地做它只会使你的代码看起来很业余,所以不要这样做!