0
   

Explain Scanner class in Java?

 
 
Kolyo
 
Reply Thu 8 Aug, 2013 09:15 pm
I have written a Java class called "Helper" to simplify I/O for beginner Java programmers. I've tested it on some simple input and it seems to work alright. Could anyone offer a subtle, constructive critique of my use of the Scanner class?

Code:// This class simplifies input and output for beginners

import java.util.*;

public class Helper {

private static Scanner s1;

//Scanner methods
public static double getDouble() {
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
double x = s1.nextDouble();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter double:");
}
}
System.out.println("I give up! I'm entering 0.0 FOR you.");
return 0;
}

public static int getInt(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
int x = s1.nextInt();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter int:");
}
}
System.out.println("I give up! I'm entering 0 FOR you.");
return 0;
}

public static long getLong(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
long x = s1.nextLong();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter long:");
}
}
System.out.println("I give up! I'm entering 0 FOR you.");
return 0;
}

public static float getFloat(){
for (int i=0; i < 50; i++){
try{
s1 = new Scanner(System.in);
float x = s1.nextFloat();
return x;
}
catch(InputMismatchException e){
System.out.println("Invalid input. Enter float:");
}
}
System.out.println("I give up! I'm entering 0.0 FOR you.");
return 0f;
}

public static String getString() {
s1 = new Scanner(System.in);
String s = s1.nextLine();
return s;
}

public static double getDouble(String msg){
System.out.print(msg + " ");
return getDouble();
}

public static int getInt(String msg){
System.out.print(msg + " ");
return getInt();
}

public static long getLong(String msg){
System.out.print(msg + " ");
return getLong();
}

public static float getFloat(String msg){
System.out.print(msg + " ");
return getFloat();
}

public static String getString(String msg){
System.out.print(msg + " ");
return getString();
}

//Print methods
public static void print(String msg){
System.out.println(msg);
}

public static void print(double d){
System.out.println(d);
}

public static void print(long n){
System.out.println(n);
}
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 2,140 • Replies: 2
No top replies

 
Kolyo
 
  1  
Reply Thu 8 Aug, 2013 09:16 pm
BTW, feel free to use it for input and output if you're learning to program.

It's free.
0 Replies
 
Kolyo
 
  1  
Reply Thu 8 Aug, 2013 09:18 pm
Here's the class I used to test it. This will show you how to use Helper:

Code:public class Tester {
public static void main(String[] args) {
double d = Helper.getDouble("Enter double:");
Helper.print(d);
int n = Helper.getInt("Enter int:");
Helper.print(n);
Helper.print("Enter message:");
String s = Helper.getString();
Helper.print("Your message is:");
Helper.print(s);
}
}
0 Replies
 
 

Related Topics

Clone of Micosoft Office - Question by Advocate
Do You Turn Off Your Computer at Night? - Discussion by Phoenix32890
The "Death" of the Computer Mouse - Discussion by Phoenix32890
Windows 10... - Discussion by Region Philbis
Surface Pro 3: What do you think? - Question by neologist
Windows 8 tips thread - Discussion by Wilso
GOOGLE CHROME - Question by Setanta
.Net and Firefox... - Discussion by gungasnake
Hacking a computer and remote access - Discussion by trying2learn
 
  1. Forums
  2. » Explain Scanner class in Java?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.04 seconds on 11/12/2024 at 02:15:07