PDA

Click to See Complete Forum and Search --> : How to use parseInt


cantene
Mar 16th, 2001, 07:09 AM
Dear friends,

well my code is like the following in an JAppplet:

int j;
j = parseInt("123",2);
System.out.println(j);

I have import all things like:

import java.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.math.*;
import java.lang.Integer.*;
import java.lang.*;

but the compiler blames:

BinaryCalculator.java:253: cannot resolve symbol
symbol : method parseInt (java.lang.String,int)
location: class BinaryCalculator
j = parseInt("123",2);
^
1 error

unless I use the whole statement:

j = java.lang.Inetger.parseInt("123",2);


I have already import java.lang.Integer

why the compiler still can't recognize this? Can anyone explain this for me.

Also, if I just import java.*;

then is it all classes when they are needed will be included?

so why dun everyone just import the whole to prevent missing classes and compile error?

Mar 16th, 2001, 08:01 AM
java.lang is automatically imported. So all you need is

int j = Integer.parseInt("123", 2);

or

int j = Integer.parseInt("123");

Since parseInt is a static or class method, you do not need an instance of java.lang.Integer, you just call it with the class name and the method as in
Integer.theStaticMethod();

j = java.lang.Integer.parseInt("123",2);
works because you are explicitly calling the method by its complete package name.