如何修复语法错误?

时间:2014-07-20 19:15:57

标签: java syntax

我是初学者,对java的理解很差。请你帮我纠正错误说:

         // [line 45] Syntax error on token " (", ; expected

在线:

      private static int countWords(String str) {

此错误在此行上显示两次。我尝试过添加和删除括号,我尝试添加';'到我的代码但它只会使代码显示更多错误。在下面有我的代码可以帮助更好地识别错误:

import java.util.*;

 public class HDtest9 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (true) { // have created infinite loop
        System.out.print("Enter text: ");
        String sentence = in.nextLine();
          System.out.println("You have entered: " + sentence); // to Print string
        System.out.println("The total number of characters is " + sentence.length()); // to print Entered string's length
        System.out.println("This piece of text has " + countWords(sentence) + " words.");

        if (sentence.equals("quit")) { // if enterd value is "quit" than it comes out of loop
            break;
        } else {

            String[] words = sentence.split(" "); // get the individual words

 int maxWordLength = 0;
 int wordLength = 0;
 for (int i = 0; i < array.length; i++) {

     wordLength = array[i].length();
     if (wordLength > maxWordLength) {
         maxWordLength = wordLength; 
   }
    int[] intArray = new int[maxWordLength + 1];
  for (int i = 0; i < array.length; i++) {
 intArray[array[i].length()]++;
 for (int i = 1; i < intArray.length; i++) {
 out.printf("%d word(s) of length %d<br>", intArray[i], i);
   }
       }
            for (int i = 0; i < words.length; i++)
                System.out.println( "word " + i + ": " + words[i] + " = " + words[i].length() + " characters");


        }


    }
    in.close();
}

private static int countWords(String str) {
    String words[] = str.split(" ");
    int count = words.length;
    return count;
}
   }
  }

非常感谢您的帮助,非常感谢!

3 个答案:

答案 0 :(得分:3)

您正在尝试在countWords()方法中声明main()方法,这在Java中是不合法的,因此编译器会窒息尝试解析方法签名。

如果您将其移到其所属的位置之外,您将只剩下其他8个未声明和重复的变量错误。

请在代码中使用适当的缩进和命名。它会帮助你(和其他人)阅读它,并防止出现这种错误。

答案 1 :(得分:2)

在线:

for (int i = 0; i < words.length; i++)

你没有开口大括号

更改为:

for (int i = 0; i < words.length; i++) {

另外,请修改您的缩进,很难阅读并查看是否还有错误。

答案 2 :(得分:1)

更正并正常工作的代码

import java.util.*;

public class HDtest9 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) { // have created infinite loop
            System.out.print("Enter text: ");
            String sentence = in.nextLine();
            System.out.println("You have entered: " + sentence); // to Print string
            System.out.println("The total number of characters is " + sentence.length()); // to print Entered string's length
            System.out.println("This piece of text has " + countWords(sentence) + " words.");

            if (sentence.equals("quit")) { // if enterd value is "quit" than it comes out of loop
                break;
            } else {

                String[] words = sentence.split(" "); // get the individual words

                int maxWordLength = 0;
                int wordLength = 0;
                for (int i = 0; i < words.length; i++) {

                    wordLength = words[i].length();
                    if (wordLength > maxWordLength) {
                        maxWordLength = wordLength;
                    }
                }
                int[] intArray = new int[maxWordLength + 1];
                for (int i = 0; i < words.length; i++) {
                    intArray[words[i].length()]++;
                }
                for (int i = 1; i < intArray.length; i++) {
                    System.out.printf("%d word(s) of length %d<br>", intArray[i], i);
                }

                for (int i = 0; i < words.length; i++) {
                    System.out.println("word " + i + ": " + words[i] + " = " + words[i].length() + " characters");
                }

            }

        }
        in.close();
    }

    private static int countWords(String str) {
        String words[] = str.split(" ");
        int count = words.length;
        return count;
    }

}