Podcast
Questions and Answers
What is the purpose of System.out.println in Java?
What is the purpose of System.out.println in Java?
How is the data to be output given in System.out.println?
How is the data to be output given in System.out.println?
What is the difference between System.out.println and System.out.print?
What is the difference between System.out.println and System.out.print?
What is the purpose of importing packages and classes in Java?
What is the purpose of importing packages and classes in Java?
Signup and view all the answers
Which package is automatically imported into every Java program?
Which package is automatically imported into every Java program?
Signup and view all the answers
What is the benefit of specifying a class instead of using (*) when importing packages?
What is the benefit of specifying a class instead of using (*) when importing packages?
Signup and view all the answers
Which class in Java is used for simple keyboard input starting from version 5.0?
Which class in Java is used for simple keyboard input starting from version 5.0?
Signup and view all the answers
What statement is required to make the Scanner class available to a Java program?
What statement is required to make the Scanner class available to a Java program?
Signup and view all the answers
What object is created by the following code: Scanner keyboard = new Scanner(System.in);?
What object is created by the following code: Scanner keyboard = new Scanner(System.in);?
Signup and view all the answers
Which method reads one int value typed at the keyboard and assigns it to a variable?
Which method reads one int value typed at the keyboard and assigns it to a variable?
Signup and view all the answers
What does the method 'nextDouble' do in Java's Scanner class?
What does the method 'nextDouble' do in Java's Scanner class?
Signup and view all the answers
How are multiple inputs separated when using the Scanner class?
How are multiple inputs separated when using the Scanner class?
Signup and view all the answers
When using 'keyboard.next()', how is a string read?
When using 'keyboard.next()', how is a string read?
Signup and view all the answers
'keyboard.nextLine()' reads what in Java?
'keyboard.nextLine()' reads what in Java?
Signup and view all the answers
What can happen when combining 'nextInt()' method and 'nextLine()' method in Java?
What can happen when combining 'nextInt()' method and 'nextLine()' method in Java?
Signup and view all the answers
What does whitespace refer to in Java's Scanner class context?
What does whitespace refer to in Java's Scanner class context?
Signup and view all the answers
What is the value of the variable n
in the given code?
What is the value of the variable n
in the given code?
Signup and view all the answers
What is the value of the variable s1
in the given code?
What is the value of the variable s1
in the given code?
Signup and view all the answers
What is the value of the variable s2
in the given code?
What is the value of the variable s2
in the given code?
Signup and view all the answers
When should a program prompt the user for input?
When should a program prompt the user for input?
Signup and view all the answers
What is the purpose of echoing the user's input in a program?
What is the purpose of echoing the user's input in a program?
Signup and view all the answers
How can the input delimiter be changed when using the Scanner
class?
How can the input delimiter be changed when using the Scanner
class?
Signup and view all the answers
Study Notes
Output in Java
-
System.out
is an object that is part of the Java language. -
println
is a method invoked by theSystem.out
object, used for console output. - The data to be output is given as an argument in parentheses.
- A plus sign is used to connect more than one item.
- Every invocation of
println
generates a new line after it finishes.
Differences between println
and print
-
print
is a method that can be invoked by theSystem.out
object. -
print
is similar toprintln
, except that it does not generate a new line. - With
println
, the next output goes on a new line. - With
print
, the next output goes on the same line.
Importing Packages and Classes
- Libraries in Java are called packages.
- A package is a collection of classes that is stored in a manner that makes it easily accessible to any program.
- To use a class that belongs to a package, the class must be imported using an
import
statement. - Classes found in the package
java.lang
are imported automatically into every Java program. - It is better to specify the class instead of using the
*
wildcard.
Console Input Using the Scanner Class
- The
Scanner
class is used for simple keyboard input. - The
Scanner
class is included in Java starting from version 5.0. - To use the
Scanner
class, a program must include theimport java.util.Scanner
statement. - The
Scanner
class is used to create an object that reads input from the keyboard. - The
Scanner
object can be given any name.
Methods in the Scanner Class
-
nextInt()
reads oneint
value typed in at the keyboard and assigns it to a variable. -
nextDouble()
reads onedouble
value typed in at the keyboard and assigns it to a variable. -
next()
reads one string of non-whitespace characters delimited by whitespace characters. -
nextLine()
reads an entire line of keyboard input. - Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method.
Pitfalls of Using the Scanner Class
- The
nextLine()
method reads the remainder of a line of text starting wherever the last keyboard reading left off. - This can cause problems when combining it with different methods for reading from the keyboard.
Programming Tips
- A program should always prompt the user when they need to input some data.
- A program should always echo all input that it receives from the keyboard.
Other Input Delimiters
- The delimiters that separate keyboard input can be changed when using the
Scanner
class. - The
useDelimiter()
method can be used to change the input delimiter.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on how to use the System.out.println method in Java for console output. Learn about providing arguments, connecting multiple items, and generating new lines. Also, compare println with the print method.