473,473 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Input Through Scanner Class

sreekandank
45 New Member
Java provides the classes and methods that supports to read input and print output. To read the input, you first construct a Scanner that is attached to the standard input stream System.in.

Scanner in=new Scanner(System.in);

Methods of Scanner class:
  • nextLine() - to read a line of input
  • next() - to read a single word
  • nextInt() - to read an integer
  • nextDouble() - to read an double floating point number
For example, the following code used to read a line of input:

System.out.println("Enter Your Name:");
Scanner in=new Scanner(System.in);
String name=in.nextLine();

ScannerDemo.java
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. /**
  3.  *@author Sreekandan.K
  4. */
  5. class ScannerDemo
  6. {
  7.  public static void main(String args[])
  8.  {
  9.   Scanner in=new Scanner(System.in);
  10.   System.out.println("What is ur name..?");
  11.   String name=in.nextLine();
  12.   System.out.println("What is ur age..?");
  13.   int age=in.nextInt();
  14.   System.out.println("Hello Mr."+name+",Your Age Is:"+age);
  15.  }
  16. }
Note: To be run this program in JDK1.5 or Higher version.
Attached Images
File Type: jpg output.jpg (42.3 KB, 407 views)
Oct 29 '11 #1
0 3703

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: slickn_sly | last post by:
For some reason, I'm getting an error. It doesn't seem to be reading my "input.txt" file. I'm trying to read the "input.txt" file which consits of random integers and use insertion sort to sort...
11
by: W. Cerven | last post by:
I have a list of n inputs of varying type I wish to read from a file: For example: string string int int double double int int int double ... My input file gives its values on a single line,...
1
by: Joe | last post by:
Can some one tell me how to insert some blank spaces into a integer or string input variable. For example; * A user is asked to enter a 5 digit number * After the use enters the number eg. 12345...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
14
tolkienarda
by: tolkienarda | last post by:
hi all i read the scanner class documentation from sun's website and i thought i would have some fun trying to write a program that stores a line from the user to var input and then parses it on...
3
by: thename1000 | last post by:
Hi, I'm trying to create this output: Input team 1's name: Team 1 Input team 1's ranking: 90.4 etc.
0
by: piradie | last post by:
Hello, I've been googling for a while and I can't really find any help with my issue and I'm starting to get frustrated. I have to use the useDelimiter to get certain stuff from the file I...
7
by: kidosai | last post by:
hi i need a guide on how to use the scanner class... i need to use Java Scanner class to read a text file and be able to print the text inside of it... example : i have a text file named...
3
by: NaN | last post by:
Hi I'm using Dev-C++. Here is my sourcecode. /* GETCH.C: This program reads characters from * the keyboard until it receives a 'Y' or 'y'. */ #include <conio.h>
24
by: arnuld | last post by:
I have a function named getword that read every single input from std. input. WHAT I WANTED: I want it read the word if it has less than or equal to 30 characters. Anything else beyond that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.