Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple Addition Program

  • 08-07-2016 2:26pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭


    Trying to write a simple program in Java that will add two numbers that the user inputs. I can get the input but it won't add the numbers together, was hoping someone here could take a look for me please :) below is the code I wrote.


    import java.util.Scanner;
    public class Ball
    {



    public static void main(String[] args) {

    int numone = 0;
    int numtwo = 0;
    int total = 0;
    Scanner scan= new Scanner(System.in);

    System.out.println("Please enter a number");
    int userInput = scan.nextInt();
    System.out.println("Please enter a Second number");
    int userInput2 = scan.nextInt();

    total= (numone + numtwo);



    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 4,532 ✭✭✭Harika


    You allocate two integers for the values and then assign the input to two different integers. And there is no output for the total value. That's the error. :)


  • Closed Accounts Posts: 48 RianF2


    It's adding them, you just need to print the totel:

    System.out.println(total);


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    Ah thanks lads :)


  • Registered Users, Registered Users 2 Posts: 882 ✭✭✭moneymad


    Trying to write a simple program in Java that will add two numbers that the user inputs. I can get the input but it won't add the numbers together, was hoping someone here could take a look for me please :) below is the code I wrote.


    import java.util.Scanner;
    public class Ball
    {



    public static void main(String[] args) {

    int numone = 0;
    int numtwo = 0;
    int total = 0;
    Scanner scan= new Scanner(System.in);

    System.out.println("Please enter a number");
    int userInput = scan.nextInt();
    System.out.println("Please enter a Second number");
    int userInput2 = scan.nextInt();

    total= (numone + numtwo);



    }

    }
    How is it adding them? as the previous poster said you allocate the output to 2 integers , i don't see where you allocate the output to numone,numtwo ?


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    moneymad wrote: »
    How is it adding them? as the previous poster said you allocate the output to 2 integers , i don't see where you allocate the output to numone,numtwo ?

    It isn't but the code is computing the total of 0 + 0 and outputting it to the console

    A Solution would be
    import java.util.Scanner;
    public class Ball 
    {
    
    
    
    public static void main(String[] args) {
    
    int numone = 0;
    int numtwo = 0;
    int total = 0;
    Scanner scan= new Scanner(System.in);
    
    System.out.println("Pleaser a number");
    numone = scan.nextInt();
    System.out.println("Pleaser a Second number");
    numtwo = scan.nextInt();
    
    total= (numone + numtwo);
    
    System.out.println(total);
    
    }
    
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,380 ✭✭✭timmyntc


    :)

    Numone and Numtwo are initialized to 0, and never updated.
    The user inputted ints are assigned to new variables, userInput and userInput2
    import java.util.Scanner;
    public class Ball
    {



    public static void main(String[] args) {

    int numone = 0;
    int numtwo = 0;
    int total = 0;
    Scanner scan= new Scanner(System.in);

    System.out.println("Pleaser a number");
    int userInput = scan.nextInt();
    System.out.println("Pleaser a Second number");
    int userInput2 = scan.nextInt();

    total= (numone + numtwo);



    }

    }

    OP, you should replace userInput and UserInput2 with numone and numtwo respectively. Then add a print statement after
    total= (numone + numtwo);


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    timmyntc wrote: »
    Numone and Numtwo are initialized to 0, and never updated.

    Yes, check my updated post


  • Registered Users, Registered Users 2 Posts: 882 ✭✭✭moneymad


    It isn't but the code is computing the total of 0 + 0 and outputting it to the console

    A Solution would be
    import java.util.Scanner;
    public class Ball 
    {
    
    
    
    public static void main(String[] args) {
    
    int numone = 0;
    int numtwo = 0;
    int total = 0;
    Scanner scan= new Scanner(System.in);
    
    System.out.println("Pleaser a number");
    numone = scan.nextInt();
    System.out.println("Pleaser a Second number");
    numtwo = scan.nextInt();
    
    total= (numone + numtwo);
    
    System.out.println(total);
    
    }
    
    }
    
    And i thought i was losing my mind.
    I have to mention as well. Java is horrible. I don't know how anyone can code it in.

    Python is so easy to read.
    first, second, total = 0,0,0
    
    
    first = input("first number ")
    second = input("second number ")
    total = first + second
    print total
    


  • Registered Users, Registered Users 2 Posts: 7,380 ✭✭✭timmyntc


    moneymad wrote: »
    And i thought i was losing my mind.
    I have to mention as well. Java is horrible. I don't know how anyone can code it in.

    Python is so easy to read.
    first, second, total = 0,0,0
    
    
    first = input("first number ")
    second = input("second number ")
    total = first + second
    print total
    

    Python is fine for small scripts or for stuff you've written, but once you've come across somebody else's work thats larger than 500-1000 LOC you'll realise its way worse than C or Java. The lack of static typing makes large projects incredibly difficult to keep track of all the types unless you've written it yourself.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 81,083 Mod ✭✭✭✭Sephiroth_dude


    I figured it out in the end, the reason I had int = 0 is because I didn't want to declare a value as I wanted the user to enter value so thought that was the way to do it.


  • Advertisement
  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,242 Mod ✭✭✭✭L.Jenkins


    timmyntc wrote: »
    Python is fine for small scripts or for stuff you've written, but once you've come across somebody else's work thats larger than 500-1000 LOC you'll realise its way worse than C or Java. The lack of static typing makes large projects incredibly difficult to keep track of all the types unless you've written it yourself.

    I've seen upto 250 lines of code written in Perl and Python, grabbing data from Manufacturing Equipment. It was absolutely fucking criminal, as there was no documentation or organisation to the code. One genius wrote a script to monitor necessary scripts on a Unix box and if they weren't running, the script would start or restart them.


  • Registered Users, Registered Users 2 Posts: 1,465 ✭✭✭Anesthetize


    L.Jenkins wrote: »
    I've seen upto 250 lines of code written in Perl and Python, grabbing data from Manufacturing Equipment. It was absolutely fucking criminal, as there was no documentation or organisation to the code. One genius wrote a script to monitor necessary scripts on a Unix box and if they weren't running, the script would start or restart them.
    I've worked on a large codebase written in Python and the only issues I was seeing with it were the same kind of stuff you'd see in Java. Ridiculously long functions, complex nested if statements, unclear variable names etc. Lack of documentation and code organisation are nothing to do with the programming language itself, they're the result of a lack of standards being applied.

    And yes, well-written Python is so much nicer to read than Java :)


  • Registered Users, Registered Users 2 Posts: 7,380 ✭✭✭timmyntc


    I've worked on a large codebase written in Python and the only issues I was seeing with it were the same kind of stuff you'd see in Java. Ridiculously long functions, complex nested if statements, unclear variable names etc. Lack of documentation and code organisation are nothing to do with the programming language itself, they're the result of a lack of standards being applied.

    And yes, well-written Python is so much nicer to read than Java :)

    If it's only primitives being manipulated in Python its okay, but any custom types and it gets incredibly confusing because there are no declarations, unlike Java which is statically typed, so at least you know what type each variable is.


Advertisement