Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Greg Kitchin
Greg Kitchin
31,522 Points

Accepting different variable types of input using one command.

So I'm writing my own programs to help consolidate my Java knowledge, and I have a working program. Basically, it asks for a number n, then will calculate the sum of all the numbers 1 through n (so 1 through 100 adds to 5050 for example). I have my own MVP.

However, I want to expand on it. I'd like it so that the user can either type a number in to calculate the value as normal, or type 'HELP' to get help, or 'ABOUT' to get some history about the formula. But my understanding of Java, is that you have to declare a variable type when getting input, and some methods require a specific type of variable to be used anyway.

Right now I'm using the console.readLine method to get input, but I have to declare a string to get that information, then parse it to an int. I think maybe using some form of loop to check to see if HELP or ABOUT have been typed, then if not, try parsing the string, or maybe using some kind of exception, but they seem a little too convoluted to me.

Anyone have any idea's? It seems fairly straightforward to check for correct input if it's one specific type of value being entered (ie, check to see if a number is greater than 0, or true or false are entered), but it becomes more complicated when different types of variables are implemented. Thanks.

By the way, here's my code.

import java.io.Console;

public class GaussCalculation
{
    public static void main ( String[] args)
    {

    //  Create Console object, allowing input request   
    Console console = System.console();     

    //  Variable declaration
    int gauss_calculation = 0;

    //  console.readLine takes Strings, need to parse other variable types
    String number = console.readLine("Enter a number: ");

    //  Parsing String into integer value
    int parsedNumber = Integer.parseInt(number);

    //  Gauss formula.
    //  Sum from 1 to n = n(n + 1) / 2
    gauss_calculation = ((parsedNumber * (parsedNumber + 1)) / 2);
    console.printf("The sum of numbers from 1 to %s is %s." , parsedNumber , gauss_calculation);


    }
}

2 Answers

You could use a scanner to read input. It can read multiple types of variables.

For help and about...declare new methods for the same...you could implement a do/while loop that runs at least once and implement a switch case within the do-while loop on the choice entered by the user...In this case you could prompt the user again and again until he types "quit" or something else that breaks the loop.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

This might be what you are looking for...typically how menus are used in console apps:

Read the part about Strings specifically: Java SE switch statement