使用 jGRASP 的 Java 編譯器錯誤 (Java compiler errors using jGRASP)


問題描述

使用 jGRASP 的 Java 編譯器錯誤 (Java compiler errors using jGRASP)

I am very new to Java and got the following two errors when I compiled:

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
            ^

The following is the code I was working . . .

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:

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!

(by user1383533Ernest Friedman‑Hill)

參考文件

  1. Java compiler errors using jGRASP (CC BY‑SA 3.0/4.0)

#java #jgrasp






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論