Results 1 to 2 of 2

Thread: How to use parseInt

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    hongkong
    Posts
    251
    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?

  2. #2
    Guest
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width