What we need is a way to take the String that we have and turn it into a double. What we got back from the console is of type String but we cannot do the math on a string, much like how we cannot expect an answer to the question “What is the sum of green walls, questionable air quality, and depression”. However, there is a problem… Parsing String into Double Great! Now we have what the user has typed. Then, everything the user typed will be stored into the userInput variable of type String. For now, just know that this line will make the program wait for the user to type stuff into the console and capture that input the moment the user hits Enter. nextLine(): A method in the Scanner class with a return type of String that will capture whatever the user typed into the console before hitting enter.ĭon’t worry if some terms look foreign to you. 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. When we meet someone, we usually say our greetings first.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |