September 09, 2010, 01:40:53 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Scanner class  (Read 341 times)
0 Members and 1 Guest are viewing this topic.
The Soul
Member
*

Reputation: 2
Offline Offline
Posts: 12
Referrals: 1

Awards
« on: August 08, 2009, 09:08:31 PM »

The Scanner class is a class that's able to parse data types when you input a value.

For example, I'm going to make a class that prints back what you've inputted.

In order to use the Scanner class, you're going to have to import the package like so:
Code:
import java.util.*;
Create our class:
Code:
public class Test {
Add our main method:
Code:
public static void main(String[] args) {
Now we'll have to create an object in order to use the class:
Code:
Scanner scan = new Scanner(System.in);
Declare a String:
Code:
String testing = "";
Print a line:
Code:
System.out.println("Type a word: ");
Make it so 'testing' is scanned:
Code:
testing = scan.next();
Print back the inputted word:
Code:
System.out.println("You entered: "+testing);

The full source code:
Code:
import java.util.*;
       public class Testing {
              public static void main(String[] args) {
                     Scanner scan = new Scanner(System.in);
                     String testing = "";
                     System.out.println("Type a word: ");
                     testing = scan.next();
                     System.out.println("You've entered: "+testing);
           }
    }

I recommend using a InputStreamReader or a BufferedReader rather than this, though.
Logged
Javaforums.net :: a community about Java software development.
« on: August 08, 2009, 09:08:31 PM »

Your Ad Here
 Logged
two0426
Junior Member
*

Reputation: 0
Offline Offline
Posts: 6
Referrals: 0

Awards
« Reply #1 on: May 31, 2010, 01:44:43 AM »

spam
« Last Edit: May 31, 2010, 02:16:40 AM by Arkie » Logged
Javaforums.net :: a community about Java software development.
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.238 seconds with 33 queries.

Google visited last this page September 07, 2010, 07:15:18 PM