Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Need help with Addition Calculator!

  1. #1
    Laon8orsSon is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Question Need help with Addition Calculator!

    Im trying to make it so that you can input 2 numbers and they get added together then outputed to the screen! Help Please!
    It doesnt work like this because of the following:
    Cannot convert String to int

    Java Code:
    import java.util.Scanner;
    
    public class scannerTest {
    	public static void main(String[] args) {
    	
    	Scanner in = new Scanner(System.in);
    	System.out.println("Hello!");
    	System.out.println("What is your name?");
    	String name = in.next();
    	System.out.println("Hello " + name + ", what two numbers");
    	System.out.println("would you like to add together?");
    	System.out.println("Number One?");
    	int numbOne = in.next();
    	System.out.println("Number one is " + numbOne + ".");
    	System.out.println("Number Two?");
    	int numbTwo = in.next();
    	System.out.println("Number two is " + numbTwo + ".");
    	float sum = numbOne + numbTwo;
    	System.out.println ("Number one plus number two equals " + sum + "!");
    	}
    }
    Last edited by Laon8orsSon; 05-24-2012 at 03:54 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Need help with Addition Calculator!

    What is your problem? Errors, incorrect output, code causes a block hole? We don't read minds.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help with Addition Calculator!

    Cannot convert String to int
    It's a good idea to say what line the compiler message refers to as we are very lazy here!

    Java Code:
    int numbOne = in.next();
    Have a look at the API documentation for next(). Do you see that it returns a String? That's what the compiler is complaining about: you take this String and attempt to assign it to an int (numbOne). Look for a more appropriate method.

  4. #4
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with Addition Calculator!

    As above in.next(); refer to a string were you would like a int input so if you look on that page you will see you made an easy mistake, in.nextInt(); should be used so the numbers are input as a int not a string.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need help with Addition Calculator!

    j0rdan, don't spoonfeed. The guidance already given by pbrockway2 was adequate for Laon8orsSon to find that method by himself.

    Spoonfeeding denies questioners the opportunity to learn and mature toward solving their own problems (and hopefully helping others thereafter).

    db
    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 9
    Last Post: 02-06-2012, 06:11 AM
  2. layout addition
    By droidus in forum Android
    Replies: 0
    Last Post: 11-17-2011, 08:08 PM
  3. addition
    By rithish in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 08:15 AM
  4. Addition of Two time
    By jithan in forum New To Java
    Replies: 5
    Last Post: 12-29-2008, 11:09 AM
  5. Addition Problem
    By tornado in forum New To Java
    Replies: 6
    Last Post: 12-06-2008, 11:37 PM

Posting Permissions

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