Wrapper classes

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 801 Views - Last Post: 10 March 2013 - 12:36 PM Rate Topic: -----

#1 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Wrapper classes

Posted 08 March 2013 - 02:17 AM

OK. I'm about to pull out all the hair on the top of my head. I can't figure out what I'm doing wrong.

Here is the problem that I am working on :

Use the wrapper classes to create an application that reads a 5 digit number from the user and then reports back the sum of those digits.

Here is my non-working code. Please help me understand what I'm doing wrong.

   public static void main(String[] args) {
        
       
        String num = null;

        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a five digit number: ");
        num = scan.next();
        System.out.println(num);
        
        for (int i = 0; i <1; i++) {
            num = Integer.parseInt();
            System.out.println(num);
        
    }
}
}


The error is in the second line of the for loop, "num = Integer.parseInt.();" The error says, "No suitable method found for parseInt()"

Is This A Good Question/Topic? 0
  • +

Replies To: Wrapper classes

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Wrapper classes

Posted 08 March 2013 - 03:03 AM

Well, that is because Integer.parseInt takes string as parameter and the variable you are storing the result in should be an integer variable.

I would suggest you do something like this.

for (int i = 0; i <1; i++) 
{
  int newNum = Integer.parseInt(num);
  System.out.println(newNum);
}


regards,
Raghav

This post has been edited by raghav.naganathan: 08 March 2013 - 03:05 AM

Was This Post Helpful? 2
  • +
  • -

#3 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 197
  • Joined: 13-July 12

Re: Wrapper classes

Posted 08 March 2013 - 03:04 AM

You have to import the Integer wrapper class. The compiler can't see the static method parseInt()
import java.lang.Integer

Place the import statement at the top of your code.
*EDIT*
Do what Raghav says also. Here is the API for parseInt().

This post has been edited by jjh08: 08 March 2013 - 03:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Wrapper classes

Posted 08 March 2013 - 03:08 AM

View Postjjh08, on 08 March 2013 - 03:34 PM, said:

You have to import the Integer wrapper class. The compiler can't see the static method parseInt()
import java.lang.Integer

Place the import statement at the top of your code.


Well, not really. The program will work even if you don't do the import.

The problem was happening because he was not specifying a string parameter to Integer.parse method.

regards,
Raghav
Was This Post Helpful? 2
  • +
  • -

#5 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 08 March 2013 - 03:10 AM

View Postjjh08, on 08 March 2013 - 03:04 AM, said:

You have to import the Integer wrapper class. The compiler can't see the static method parseInt()
import java.lang.Integer

Place the import statement at the top of your code.
*EDIT*
Do what Raghav says also. Here is the API for parseInt().


OMG! Rofl. I can't believe I forgot to import. lol I'm even worse than I thouht I was. /sigh

Thank you very much for the help.
Was This Post Helpful? 0
  • +
  • -

#6 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 197
  • Joined: 13-July 12

Re: Wrapper classes

Posted 08 March 2013 - 03:10 AM

View Postraghav.naganathan, on 08 March 2013 - 03:08 AM, said:

View Postjjh08, on 08 March 2013 - 03:34 PM, said:

You have to import the Integer wrapper class. The compiler can't see the static method parseInt()
import java.lang.Integer

Place the import statement at the top of your code.


Well, not really. The program will work even if you don't do the import.

The problem was happening because he was not specifying a string parameter to Integer.parse method.

regards,
Raghav

Yes, you are correct. I edited my other post. I had not looked in the API at first.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Wrapper classes

Posted 08 March 2013 - 03:12 AM

How exactly are wrapper classes involved here?

Also, if you're using Scanner, why aren't you using nextInt()?
Was This Post Helpful? 0
  • +
  • -

#8 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 08 March 2013 - 03:22 AM

Both of you were right. Thank you for the help. It's so frustrating being a rookie!
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Wrapper classes

Posted 08 March 2013 - 03:23 AM

Also, how is the sum of the digits involved in your code?

A small hint I can give you is you need to use the % operator in your for loop.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#10 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 08 March 2013 - 03:31 AM

View Postbaavgai, on 08 March 2013 - 03:12 AM, said:

How exactly are wrapper classes involved here?

Also, if you're using Scanner, why aren't you using nextInt()?


I'm using Scanner to get a string from the user. This allows the user to enter any number they want (double) and not just ints. The wrapper takes that string and stores it as an Integer which will then allow me to perform a math operation with it.
Was This Post Helpful? 0
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Wrapper classes

Posted 08 March 2013 - 03:58 AM

View Postlouisianapadawan, on 08 March 2013 - 04:01 PM, said:

View Postbaavgai, on 08 March 2013 - 03:12 AM, said:

How exactly are wrapper classes involved here?

Also, if you're using Scanner, why aren't you using nextInt()?


I'm using Scanner to get a string from the user. This allows the user to enter any number they want (double) and not just ints. The wrapper takes that string and stores it as an Integer which will then allow me to perform a math operation with it.


Well, that can be done, but even if you use Double.parseDouble() method for getting a double value from a string, there will be a problem when you use the % operator as % operator will only work with integer values.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7650
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: Wrapper classes

Posted 08 March 2013 - 10:56 AM

View Postraghav.naganathan, on 08 March 2013 - 05:58 AM, said:

Well, that can be done, but even if you use Double.parseDouble() method for getting a double value from a string, there will be a problem when you use the % operator as % operator will only work with integer values.


Not true. It works just as you'd expect.


public class Junk
{
    public static void main (String[] args)
    {
        double d = 7.5;
        System.out.println(d % 0.5);
        System.out.println(16 % d);
    }
}




/home/OMGEO/jon.kiparsky/junk:2030 $ java Junk
0.0
1.0
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,644
  • Joined: 16-October 07

Re: Wrapper classes

Posted 08 March 2013 - 11:27 AM

View Postlouisianapadawan, on 08 March 2013 - 05:31 AM, said:

I'm using Scanner to get a string from the user. This allows the user to enter any number they want (double) and not just ints.


So would nextDouble(). Only with less chance of failure.

View Postlouisianapadawan, on 08 March 2013 - 05:31 AM, said:

The wrapper takes that string


Sorry, wrapper class has a specific meaning in OO... wait, you mean the Integer wrapper class, not anything your wrote? Got it.

So, you wanted Integer n = new Integer((int)scan.nextDouble());?
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Wrapper classes

Posted 08 March 2013 - 11:56 AM

View Postjjh08, on 08 March 2013 - 05:04 AM, said:

You have to import the Integer wrapper class. The compiler can't see the static method parseInt()
import java.lang.Integer

Place the import statement at the top of your code.

False ! The java.lang classes are auto,aticaly imported
Was This Post Helpful? 0
  • +
  • -

#15 louisianapadawan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 13

Re: Wrapper classes

Posted 10 March 2013 - 12:40 AM

Ok. I haven't made much progress on this. Here is what I have. Anytime I try to get the first or second character from newNum, I get an error saying newNum can't be dereferenced. So how do I get the sum of newNum ?

    public static void main(String[] args) {
        
        int num6;
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter a five digit number: ");
        String num = scan.next();
//        System.out.println(num);
        int newNum = Integer.parseInt(num);
        System.out.println(newNum);
        
        // This gets the first number in the string

        System.out.println(num.charAt(0));
        int num1 = num.charAt(0);

        // This gets the second number in the string
          
        System.out.println(num.charAt(1));
        int num2 = num.charAt(1);

        num6 = num1 + num2;
        System.out.println(num6);



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2