How to fix “cannot find symbol error” for my program?

ⅰ亾dé卋堺 提交于 2019-12-02 19:41:15

问题


I am struggling to find why I keep receiving this message:

Integer.java:13: error: cannot find symbol
         num = Integer.parseInt(numStr);
                      ^
  symbol:   method parseInt(String)
  location: class Integer
Integer.java:16: error: cannot find symbol
         num2 = String.parseInt(numStr2);
                      ^
  symbol:   method parseInt(String)
  location: class String
2 errors

Is there something I missed? Thanks.

import javax.swing.JOptionPane;

public class Integer
{
   public static void main (String[] args)
   {
      String numStr, numStr2, sum, product;
      int num, num2, again;

      do
      {
         numStr = JOptionPane.showInputDialog("Enter an integer: ");
         num = Integer.parseInt(numStr);

         numStr2 = JOptionPane.showInputDialog("Enter another integer: ");
         num2 = Integer.parseInt(numStr2);

         sum = "The sum is " + ((num + num2));
         product = " and  the product is " + ((num * num2));

         JOptionPane.showMessageDialog(null, sum);
         again = JOptionPane.showConfirmDialog(null, "Do Another?");
      }

      while (again == JOptionPane.YES_OPTION);
  }
}

回答1:


You should rename your class to another name, perhaps call it "IntegerMachine" and not "Integer". Java already has a native class with the name "Integer", and by naming your own class the same would mean that instead of calling java.lang.Integer.parseInt(string), which you intended, you are calling .Integer.parseInt(), for which the latter does not exist.




回答2:


@Ricardo Desu Perez

change your below line

num = Integer.parseInt(numStr);

to following

num = java.lang.Integer.parseInt(numStr);

if you intend to call default parseInt method in Integer class else write your own version of parseInt(String) method



来源:https://stackoverflow.com/questions/27163008/how-to-fix-cannot-find-symbol-error-for-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!