找不到符号-但是变量在那里

时间:2018-10-19 14:01:19

标签: java find symbols

我对Java有相当的经验,但是最近我写了这个程序来对字符串进行排序(请不要介意效率和类似的东西)。但是我得到了这个奇怪的错误。

这是代码:

import java.io.*;
class Prog3
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader br = new BufferedReader(new 
    InputStreamReader(System.in));
    System.out.print("Enter a String: ");
    String s = br.readLine().toUpperCase();
    String st = "", finalStr = "";
    int count = 1, arI = 0;
    char t = s.charAt(s.length()-1);
    if(!(t=='.'||t=='?'||t=='!'))
    {
      System.out.print("Invalid Input!");
      System.exit(0);
    }
    for(int i = 0; i < s.length(); i++)
    {
      char ch = s.charAt(i);
      if(ch==' ')
      {
        count++;
      }
    }
    String[] ar = new String[count];
    for (int i = 0; i < s.length(); i++)
    {
      char ch = s.charAt(i);
      if(ch!=' '&&ch!='.'&&ch!='?'&&ch!='!')
      {
        st += ch;
      }
      else
      {
        ar[arI++] = st;
        st = " ";
      }
    }
    for ( int i = 0; i < ar.length-1; i++);
    {
      for ( int j = 0; j < ar.length-1-i; j++);
      {
        if (ar[j].compareTo(ar[j+1])>0)
        {
          String temp = ar[j];
          ar[j] = ar[j+1];
          ar[j+1] = temp;
        }
      }
      finalStr = ar[ar.length-1-i] + " " + finalStr;
    }
    System.out.println("Length: " + count);
    System.out.println("Rearranged Sentence:\n" + ar[0] + " " + finalStr);
  }
}

但是我仍然在第41行上出现此“找不到符号”错误,依此类推:

Prog3.java:41: error: cannot find symbol
      for ( int j = 0; j < ar.length-1-i; j++);
                                       ^
  symbol:   variable i
  location: class Prog3
Prog3.java:43: error: cannot find symbol
        if (ar[j].compareTo(ar[j+1])>0)
                               ^
  symbol:   variable j
  location: class Prog3
Prog3.java:43: error: cannot find symbol
        if (ar[j].compareTo(ar[j+1])>0)
               ^
  symbol:   variable j
  location: class Prog3
Prog3.java:45: error: cannot find symbol
          String temp = ar[j];
                           ^
  symbol:   variable j
  location: class Prog3
Prog3.java:46: error: cannot find symbol
          ar[j] = ar[j+1];
             ^
  symbol:   variable j
  location: class Prog3
Prog3.java:46: error: cannot find symbol
          ar[j] = ar[j+1];
                     ^
  symbol:   variable j
  location: class Prog3
Prog3.java:47: error: cannot find symbol
          ar[j+1] = temp;
             ^
  symbol:   variable j
  location: class Prog3
Prog3.java:50: error: cannot find symbol
      finalStr = ar[ar.length-1-i] + " " + finalStr;
                                ^
  symbol:   variable i
  location: class Prog3
8 errors

如果我愚蠢地想念它有些愚蠢,那我将非常抱歉。

1 个答案:

答案 0 :(得分:2)

for循环后有;,有效地关闭了i和j的范围。

for ( int i = 0; i < ar.length-1; i++);
for ( int j = 0; j < ar.length-1-i; j++);