Scanner class:-
It is used to create an object which is used to read/take data from the keyboard.
Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
It is found in Java.util. Package.
We need to import this package before using the methods of this class.
Ex:- Scanner obj_name = new Scanner(System.in).
System.in the reference of the keyboard.
Methods in the Scanner class:-
Example :-
// creating object for the scanner class.
Scanner obj = new Scanner(System.in);
1) Int nextInt():-
Description:- It is used to read integer from the keyboard.
Syntax:- Scanner obj_name .nextInt().
Example:- obj .nextInt()
2)Int nextFloat():-
Description:- It is used to take float type from the keyboard.
Syntax:- Scanner obj_name .nextFloat().
Example:- obj .nextFloat()
3)String nextLine():-
Description:- It is used to take string from the keyboard.
Syntax:- Scanner obj_name .nextLine();
Example:- obj.nextLine();
Program for Scanner class methods:-
First compile the java file :- javac HomeworkLibjava.java
It will create a HomeworkLibjava.class file
Then run the file :- java HomeworkLibjava
//import the package for the Scanner class methods
import java.util.*;
class HomeworkLibjava{
public static void main(String []args){
String fname;
int number;
float marks;
//creating object of Scanner class
Scanner obj = new Scanner(System.in);
System.out.print("Enter your full name:- ");
//take name from the user by keyboard
fname = obj.nextLine();
System.out.print("Enter your favourite integer:- ");
//take number(integer) from the user by keyboard
number= obj.nextInt();
System.out.print("Enter your marks:- ");
//take marks(float) from the user by keyboard
marks = obj.nextFloat();
System.out.println("Full Name: " + fname + ", number: "+ number+ ", Marks: "+ marks);
}
}
output:-
Enter your full name:- HomeworkLib.com
Enter your favourite integer:- 123
Enter your marks:- 99.9
Full Name: HomeworkLib.com, number: 123, Marks: 99.9
Investigate 3 method from the scanner class (java.util package ) - List them and briefly describe...
Need a help with this one FoodListInterface.java specifies a Java interface for an ADT that maintains a list of foods, dishes or meals. This interface includes two operations: add(String food) – add the name of a food, dish or meal to the list onTheMenu(String food) – check if a given food item is on the list Develop three implementations of this interface, each with a different underlying data structure: Array Linked list ArrayList from the java.util package (a Java Collections...
Submit Chapter6.java with a public static
method named justifyText that accepts two parameters; a Scanner
representing a file as the first parameter, and an int width
specifying the output text width. Your method writes the text file
contents to the console in full justification form (I'm sure you've
seen this in Microsoft Word full.docx ). For example, if a Scanner is
reading an input file containing the following text:
Four score and
seven years ago our
fathers brought forth
on this...
42) Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class. 46) Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The...
Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be song lyrics to your favorite song. With your code, you’ll read from the text file and capture the data into a data structure. Using a data structure, write the code to count the appearance of each unique word in the lyrics. Print out a word frequency list. Example of the word frequency list: 100: frog 94: dog 43: cog 20: bog Advice: You can...
import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
//method that...
Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...
Design class House to describe a house object by using houseID, town, and zip code of String type and price as integer. Write a program that reads a sequence of houseIDs, towns, zip codes and prices for up to 100 houses from an input file. Store the data in an array that has capacity to hold 100 house objects. Keep track of number of houses in variable count. Assume that each line of input has houseID, town, zip,and price data...
Homework 3: Input Validation 1 Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2 User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...
Open BlueJ and create a new project (Project->New
Project...).
Create a new class named ListsDemo.
Open the source code and delete all the boilerplate code.
Type in the code to type (code to type with bigger font)
exactly as shown, filling in your name and the date in the places
indicated.
The code is provided as an image because you should type it in
rather than copy-paste. If you need the code as text for
accessibility, such as using a...
import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...