Question

Investigate 3 method from the scanner class (java.util package ) - List them and briefly describe...

Investigate 3 method from the scanner class (java.util package )
- List them and briefly describe their purpose
- show (code) examples using them

You will need to type this report using MS word submit your report that includes source code examples with their output.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Investigate 3 method from the scanner class (java.util package ) - List them and briefly describe...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing...

    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...

  • Assignment 3: Word Frequencies Prepare a text file that contains text to analyze. It could be...

    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...

  • 42) Create a toString method for the LinkedStack class. This method should create and return a...

    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...

  • Import java.util.*; public class PairFinder { public static void main(String[] args) { Scanner sc...

    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...

    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...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    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...

    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....

    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...

  • Please code this in java: The purpose of this assignment is to: Read double data in...

    Please code this in java: The purpose of this assignment is to: Read double data in to a TreeSet where each datum inserted is Math.abs(datum) (i.e., no negative values are inserted). NOTE: This will (i) sort the data placed into the TreeSet, and, (ii) remove duplicate values since no duplicates are allowed (i.e., duplicates will not be inserted). Create a list iterator. Iterate backwards through the TreeSet (i.e., from the largest value to the smallest) outputting all elements * -1.0....

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT