|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
We can have our programs read input from the terminal
by using a
java.util.Scanner scnr; // Make a new Scanner which reads input from the keyboard (System.in): scnr = new java.util.Scanner( System.in ); System.out.print( "Please enter your favorite color: " ); String userFaveCol; userFaveCol = scnr.next(); // Ask scnr for the next word it reads. (Throw away any whitespace.) System.out.print( "Please enter your age: " ); int userAge; userAge = scnr.nextInt(); // Ask s for the next int it reads. System.out.println( "What, a " + userAge + "-year-old who likes " + userFaveCol + "?!" ); |
Full docs:
Of course, code like this can go inside a method, no problem:
class TempConverter2 { static double fahrToCelc( double tempF ) { return (tempF - FAHR_FREEZING) * CELC_PER_FAHR; } static double FAHR_FREEZING = 32.0; static double FAHR_BOILING = 212.0; static double CELC_PER_FAHR = 100.0 / (FAHR_BOILING - FAHR_FREEZING); static void testInteractive() { java.util.Scanner s; // Declare a variable. /* make a new Scanner which reads from the keyboard (System.in): */ s = new java.util.Scanner( System.in ); System.out.print( "What is your name? " ); String usersName; usersName = s.next(); double temperature; System.out.print( "Please enter a temperature in Fahrenheit: " ); temperature = s.nextDouble(); System.out.println( "Hi " + usersName + "; " + temperature + "°F = " + fahrToCelc(temperature) + "°C." ); System.out.print( "Please enter a temperature in Fahrenheit: " ); temperature = s.nextDouble(); System.out.println( "Hi " + usersName + "; " + temperature + "°F = " + fahrToCelc(temperature) + "°C." ); System.out.print( "Please enter a temperature in Fahrenheit: " ); temperature = s.nextDouble(); System.out.println( "Hi " + usersName + "; " + temperature + "°F = " + fahrToCelc(temperature) + "°C." ); System.out.println( "As Mark Twain noted, " + usersName + ",\n" + "everybody complains about the weather,\n" + "but nobody every does anything about it." ); // Note: No return value, from this function -- // printing results to the console means they're gone forever, // and we can't pass those strings to other methods for further // processing; we'd need to *return* the string for that. } static void testTempConverter2() { System.out.println( "Actual: " + fahrToCelc(32) ); System.out.println( "Expect: " + 0.0 ); System.out.println( "Actual: " + fahrToCelc(212) ); System.out.println( "Expect: " + 100.0 ); System.out.println( "Actual: " + fahrToCelc(-40) ); System.out.println( "Expect: " + -40.0 ); System.out.println( "Actual: " + fahrToCelc(98.6) ); System.out.println( "Expect: " + 37.0 ); } } |
Self-review:
Q: What can we use a local-variable for?
A: To (1) remember a piece of information for future use,
or (2) give a name to a sub-computation that will contribute to the overall return value.
Note how the above code uses
Your Task:
Write a function
named, say,
Reading a number typed in at the keyboard is more complicated than just calling a read method. The reason is, if you read straight from the keyboard, you get raw characters. Suppose somebody has typed “33 hi 44.4” followed by a return, then reading the keyboard merely gives you a dozen characters or so. If you want to convert some of those characters into numbers, and if you want to ignore skip over multiple spaces, then you need an expert who is good at reading that sequence of characters, and who can tokenize it -- turn it into a sequence of an int, a word, and a double.
Java provides such a specialist --
class
We'd like to format our output more nicely, to get rid of the long decimals.
There is a function
System.out.println( String.format( "Hi %s; %f fahr = %.2f celc.", usersName, temperature, fahrToCelc(temperature) ) ); |
By the way:
a
// Any filename will do: s = new java.util.Scanner( new java.io.File( "H:/itec120/lab02a/PizzaServer.java" ) ); s.next() s.next() s.next() s.next() |
java.util.Scanner s3; s3 = new java.util.Scanner( "4 score and 7 years" ); s3.hasNextInt() // If we try reading an int next, will it succeed? s3.nextInt() s3.hasNextInt() // If we try reading an int next, will it succeed? s3.next() s3.next() s3.hasNextInt() s3.nextInt() s3.hasNext() // If we try reading anything next, will it succeed? s3.next() s3.hasNext() s3.next() // Try reading anyway... |
1
In this case, if you try it out, you realize that web pages are written
in html; in html the notion of the “next word” is different
from where the next whitespace occurs.
Indeed, people have written
specialized classes which extend
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2012, Ian Barland, Radford University Last modified 2012.Oct.22 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |