0

我对 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.
4

1 回答 1

0

The open brace that's part of your printEven() method definition near the beginning of your code is never matched with a closing brace, so main() appears to be inside of printEven(). This isn't legal -- you can't define a method inside of another method -- so the compiler complains.

I looked over the rest of the code to see what other errors you have, and there are actually a lot. It almost looks like you're deliberately trying to define methods inside of other methods; if you are you're going to have to stop trying and implement your program correctly. Also note that an inner class defined inside a method (which is, actually, legal) cannot have an access qualifier: if you define InnerToLowerCaseString inside a method, then it cannot be marked private.

Finally, note that, despite your comment, importing java.lang is never necessary, as it's always imported implicitly. Doing it explicitly just makes your code look amateurish, so don't do it!

于 2012-05-09T03:13:03.303 回答