3/3/08

Another Java Keyboard example

This example is similar to the previous one but uses the class StringTokenizer.
Reads two numbers and prints the sum and then reads and prints anything you write.

import java.util.StringTokenizer;
import java.util.Scanner;

class TokenizerExample {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Give 2 numbers followed by letters ");

int x = new Integer(keyboard.next());
int y = new Integer(keyboard.next());
System.out.println("x+y = " +(x+y));

StringTokenizer test;
test = new StringTokenizer(keyboard.nextLine());

while (test.hasMoreTokens()) {
System.out.println(test.nextToken());
}

}
}
Del.icio.us

No comments:

Related Posts Plugin for WordPress, Blogger...