Problem with java program
Good Day!!..
Because i cannot see the output of my program and the only thing that i can do is to compile them and see if there is syntax error..i really had hard time to visualize what is happening in my program...Because of that, i send my prog to my friend and see what is the output of my program...She said that the this part of my simple program:
public class EnterNumbers
{
public static void main(String[] args) throws Exception
{
int [] n = new int [10];
int x = 0;
System.out.println("Enter 0 when you are finish entering numbers!");
do
{
System.out.println ("Please enter number from (1-9): ");
n[x] = (int) System.in.read();
if (x < 10)
{
x = x+1;
}
else
{
System.out.println("You can only enter up to 10 numbers!");
}
}while (n[x] != 0 || n[x] == 10);
System.out.println("The numbers you entered are : ");
for(x=0;x>n.length;x++)
{
System.out.println(n[x] + "\n");
}
System.exit(0);
}
}
***She said that the numbers i am storing in my array is not actually stored so whenever i will print the values of my array.. it doesn't print anything..
Can someone help me or give can give suggestions abou my prog..
*** The prob is: I can input up to 10 numbers using numbers(1-9)only..then it will be stored in an array then print the values of an array..verify if the users enter only numbers (1-9)..
thank you very much...
[1739 byte] By [
jareivy05] at [2007-11-11 7:26:19]

# 4 Re: Problem with java program
...it reads ints, doubles and strings from the console input
import java.io.*;
import java.text.*;
public class UserInput {
static DecimalFormat df=new DecimalFormat("0.000");
// treat System.in like a buffered reader
static BufferedReader cons=
new BufferedReader(new InputStreamReader(System.in));
public UserInput() {}
/**
* Get an integer from the console
* The Integer.MIN_VALUE is returned on blank input
* @param prompt
* @return
*/
public int getUserInt(String prompt) {
String inLine=null;
while (true) { // keep on until value ok
System.out.println(prompt+" (blank to stop)");
try {
inLine=cons.readLine().trim();
if (inLine.length()==0) {
System.out.println("Bye for now");
return Integer.MIN_VALUE;
}
return Integer.parseInt(inLine);
}
catch (IOException ex) {
System.err.println("An error occurred");
ex.printStackTrace();
}
catch (NumberFormatException ex) {
System.err.println(inLine+" is not an integer");
}
System.err.println("Please reenter");
}
}
/**
* Get a double from the console
* The Double.MIN_VALUE is returned on blank input
* @param prompt
* @return
*/
public double getUserDouble(String prompt) {
String inLine=null;
while (true) { // keep on until value ok
System.out.println(prompt+" (blank to stop)");
try {
inLine=cons.readLine().trim();
if (inLine.length()==0) {
System.out.println("Bye for now");
return Double.MIN_VALUE;
}
return Double.parseDouble(inLine);
}
catch (IOException ex) {
System.err.println("An error occurred");
ex.printStackTrace();
}
catch (NumberFormatException ex) {
System.err.println(inLine+" is not a valid decimal number");
}
System.err.println("Please reenter");
}
}
/**
* Get a string from the console
* @param prompt
* @return
*/
public String getUserString(String prompt) {
System.out.println(prompt+" (blank to stop)");
while (true) { // keep on until input ok
try {
return cons.readLine().trim();
}
catch (IOException ex) {
System.err.println("An error occurred");
ex.printStackTrace();
}
}
}
/**
* Test
*/
public static void main (String [] args) {
UserInput ui=new UserInput();
int n=ui.getUserInt("Enter a whole number");
if (n==Integer.MIN_VALUE) System.exit(0);
System.out.println("You entered: "+n);
double d=ui.getUserDouble("Enter a decimal number");
if (d==Double.MIN_VALUE) System.exit(0);
System.out.println("You entered: "+df.format(d));
String s=ui.getUserString("Enter some text");
if (s.length() > 0) {
System.out.println("You entered: " + s);
}
System.out.println("Bye for now");
}
}
sjalle at 2007-11-11 22:41:25 >

# 5 Re: Problem with java program
Thank you very much for the sample codes..It gave me an idea how to debug my program...by the way, when i used System.in.read for entering integer..it prints other number.ex. when i enter 1, it will print 49. do i need to parseint first the number before i can print it?
also, whenever i will execute this program:
public class LoopProgram
{
public static void main(String[] args) throws Exception
{
char input;
System.out.println("Please Choose One: A,B,C, or Q");
do
{
input = (char) System.in.read();
switch(input)
{
case 'A':
{
System.out.println("Good Job!");
break;
}
case 'B':
{
System.out.println("Good Job!");
break;
}
case 'C':
{
System.out.println("Good Job!");
break;
}
case 'Q':
{
break;
}
default:
{
System.out.println("Choose only A,B,C or Q!");
break;
}
}
}while (input != 'Q');
System.exit(0);
}
}
the output always print the default message twice eventhough the input is correct?..
thank you..