s: Refers to the Scanner variable we created earlier for handling user input. userInput: The name of our newly declared variable. String: The type of our newly declared variable. Let’s do exactly that: String userInput = s.nextLine() We want to get the user input and store it in a variable. For now, just know this line creates a new Scanner that we can use to read input from the user. System.in: The standard system console input stream, which is where we will be getting the user input from.ĭon’t worry too much about classes and objects, for now, we will cover them in a later article. new Scanner(System.in): Indicates we want to create a new Scanner object from the Scanner class with an argument of System.in. s: The name of our newly declared variable. Scanner: The type of our newly declared variable. We can initialize Scanner by doing: Scanner s = new Scanner(System.in) It can be reading from a file, accepting data over the network, or in our case, reading from console inputs. There are many different forms of input streams. We need a way to grab whatever they type and store it in a variable.Įnter Scanner, a built-in tool provided by Java to accept input from input streams. They could, but that would be of no use to us. Obviously the user cannot just type blankly into the console. Now the 60,000 dollar question: how can we accept input from the user? Getting User Input with Scanner We want the user to type in the first number right after the prompt on the same line, so we are using print instead. println automatically starts a new line after displaying the information while print doesn’t. Notice how we used print instead of println. Let’s print out a new message: (“Please enter the first number: ”) After that, we need to prompt the user for the first number. We first declared a variable named welcomeMessage of type String with content “Welcome to my Java calculator!” and then printed it to the console.
I’m only demonstrating how to declare a String.Īgain, DO NOT copy and paste as the quotation characters can cause issues! Let’s display “Welcome to my Java calculator!”: String welcomeMessage = “Welcome to my Java calculator!” (welcomeMessage)
We want to do the same with our calculator - to display a friendly message at startup.
When we start up Windows XP, we see a big “Welcome” at the login screen. import we meet someone, we usually say our greetings first. If some one can explain me how to continue this, or guide me, I will appreciate it.
The code above only puts the operands in a char array, and the numbers in a double array, then I didn't know how to continue. However, I don't know how to make the operations, I was thinking that maybe a switch could do the job, but with BEMDAS it gets complicated. This code is an example with the basic maths - I wanted to do with the basics first, and then add the other functions using methods. I have to make the calculator with the possibilities to do the simple maths (+-*) and some other not so basic operations (log, sin, tan, exponential). I'm trying to make a calculator (code only) using StringTokenizer, but I'm lost.