Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Noob needs constructive feedback with small program

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Noob needs constructive feedback with small program

    Hi
    Just started a basic programming course which uses Java. I use the Netbeans IDK for coding and running my programs. I'm only 1 week into the course but I want to get my head around the coding. I haven't done any Java programming before. Gotta start somewhere I guess.

    My program is supposed to ask for the user's name, give a feedback message and then prompt the user for 2 integers which it will use to perform some basic arithmetic. The program should then print the output of the calculations.

    I didn't seem to get any errors during the build and compile, but when I run it I get errors. Here's the code and output...

     
    /* A program to ask user name and then calculate the sum, product, difference
     * and quotient of two integers defined by user
     */
     
    import java.util.Scanner;
     
    public class Arithmetic
    {   
        public static void main(String[] args) 
        { 
     
            String usersName; // The user's name as entered by user.
            String upperCaseName; // The user's name converted to upper case letters
     
            System.out.print("Please enter your name: ");
     
            Scanner input = new Scanner ( System.in );
     
            usersName = input.next();
            upperCaseName = usersName.toUpperCase();
     
            System.out.println("Hi " + upperCaseName + ",");
     
            System.out.printf("%s\n%s\n", "This program will", "do some calculation for you!");
     
     
            int number1;
            int number2;
            int sum;
            int product;
            int differencel;
            int differenceh;
            int quotient;
     
            System.out.print("Enter first number: ");
                number1 = input.nextInt();
     
            System.out.print("Enter second number: ");
                number2 = input.nextInt();
     
        sum = number1 + number2;
            System.out.printf( "Sum is %d\n", sum);
     
        product = number1 * number2;
            System.out.printf( "Product is %d\n", product);
     
        differencel = number2 - number1;
        differenceh = number1 - number2;
            if ( number1 == number2 )
                System.out.println( "The difference is zero." );
            if ( number1 < number2 )
                System.out.printf( "The difference is %d\n", differencel );
            if ( number1 > number2 )
                System.out.printf( "The difference is %d\n", differenceh );
     
        quotient = number1 / number2;
            System.out.printf( "Quotient is %d\n", quotient);
     
        } //end of method 
     
    } //end of class

    OUTPUT:

    run:
    Please enter your name: JN
    Hi JN,
    This program will
    do some calculation for you!
    Exception in thread "main" java.util.InputMismatchException
    Enter first number: at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Arithmetic.main(Arithmetic.java:36)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 9 seconds)


    Am I close? How's my style? Is it too muddled? Any help or guidance is greatly appreciated. Cheers.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: Noob needs constructive feedback with small program

    This is a good time to suggest that you make a habit of scanning (reading with an intent to understand what's there, remembering the high points) the API page for each new Java core (or SE) class you use. When you have difficulties using that class, return to the API page and dig a little deeper.

    In this case, if you read about Scanner.next() you'll see that it collects the 'next complete token,' whatever that is, and returns a String. If you read more on that page, you may figure out what 'next complete token' means. Compare next() to nextLine().

    Most everybody who has ever used Scanner for the first time has had to navigate this minor hurdle, recognizing that some methods leave something in the input buffer for the next method to find, messing everything up. Study that a bit and come back if you can't figure it out.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Noob needs constructive feedback with small program

    Thank you Greg. Your advice was just what I needed. I did some reading on next() to nextLine(). So instead of usersName = input.next(); in my program I replaced with usersName = input.nextLine();

    This has the effect of clearing memory buffer so the integer can then be clearly read by Scanner in the next method. Is this correct?

    The program seems to be running smoothly now.

    --- Update ---

    Just one other thing...

    Since the change to nextLine(), the Scanner reads all of the input of the user. What is the correct code, using the Scanner class and strings, for reading and outputting just the first name entered if the user enters their first and last names when prompted for their name?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: Noob needs constructive feedback with small program

    There are multiple ways to do what you've described. The most elementary way, one that a beginner might use, is to properly match the Scanner methods to the user's input. For example, if the user has been told to enter their first and last name on a single line (followed by a linefeed or return, of course), the user's input would be:

    FirstName LastName<return>

    To then collect the input and assign it to the desired variables, the program should collect the first token and the second token separately. Knowing what you know now, how do you think that would be done?

Similar Threads

  1. [SOLVED] Simple Library Program / Need feedback.
    By ziplague in forum What's Wrong With My Code?
    Replies: 22
    Last Post: May 2nd, 2014, 09:03 PM
  2. Requesting Constructive Criticism About My Code
    By Wumbo in forum Java Theory & Questions
    Replies: 6
    Last Post: July 26th, 2013, 05:48 PM
  3. Simple question about a small program
    By azizmaiden in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2013, 04:52 PM
  4. Game for Small Children Program
    By Javazoid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2012, 03:02 PM