techpreparation-homepage

Home  Interview Questions  Aptitude Questions  Tutorials  Placement Papers  Search  Resume Guide  Soft Skills  Video  Forum  Blog


Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++ Interview Questions
XML Interview Questions
EJB Interview Questions
JSP Interview Questions
                              .........More

Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

Subscribe to our Newsletters
Name:
Email:

 

 

  

Java Source Codes

read numbers and strings

package corejava;

/**
An easy interface to read numbers and strings from
standard input

*/

public class Console
{ /**
print a prompt on the console but don't print a newline

@param prompt the prompt string to display
*/

public static void printPrompt(String prompt)
{ System.out.print(prompt + " ");
System.out.flush();
}

/**
read a string from the console. The string is
terminated by a newline

@return the input string (without the newline)
*/

public static String readLine()
{ int ch;
String r = "";
boolean done = false;
while (!done)
{ try
{ ch = System.in.read();
if (ch < 0 '' (char)ch == '\n')
done = true;
else if ((char)ch != '\r') // weird--it used to do \r\n translation
r = r + (char) ch;
}
catch(java.io.IOException e)
{ done = true;
}
}
return r;
}

/**
read a string from the console. The string is
terminated by a newline

@param prompt the prompt string to display
@return the input string (without the newline)
*/

public static String readLine(String prompt)
{ printPrompt(prompt);
return readLine();
}

/**
read an integer from the console. The input is
terminated by a newline

@param prompt the prompt string to display
@return the input value as an int
@exception NumberFormatException if bad input
*/

public static int readInt(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Integer.valueOf
(readLine().trim()).intValue();
} catch(NumberFormatException e)
{ System.out.println
("Not an integer. Please try again!");
}
}
}

/**
read a floating point number from the console.
The input is terminated by a newline

@param prompt the prompt string to display
@return the input value as a double
@exception NumberFormatException if bad input
*/

public static double readDouble(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Double.parseDouble(readLine().trim());
} catch(NumberFormatException e)
{ System.out.println
("Not a floating point number. Please try again!");
}
}
}
}


<<<----- Return to Java Source Code Questions Page.


 

Have a Question ? post your questions here. It will be answered as soon as possible.

Check Java Interview Questions for more Java Interview Questions with answers

Check Servlet Interview Questions for more Servlet Interview Questions with answers

Check Structs Interview Questions for more Structs Interview Questions with answers