Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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]
# 1 Re: Problem with java program
It looks to me like this part of your code is wrong..
or(x=0;x>n.length;x++)
It should be:
(x=0;x<n.length;x++)
Code_Nerd at 2007-11-11 22:38:21 >
# 2 Re: Problem with java program
Also, this is odd:

while (n[x] != 0 || n[x] == 10)

This says: loop the input value is zero or 10....

Have you tried to print out these ints that your program is reading, right after the
System.in.read() .... ? :cool:
sjalle at 2007-11-11 22:39:27 >
# 3 Re: Problem with java program
I haven't tried printing those ints that my program is reading, right after the
System.in.read() because of the prob that i had said a while ago but i will try what you had suggested..Thank you.
jareivy05 at 2007-11-11 22:40:25 >
# 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..
jareivy05 at 2007-11-11 22:42:29 >
# 6 Re: Problem with java program
have you tried using a Scanner object for input?

Scanner sc = new Scanner(System.in);
viviensiu at 2007-11-11 22:43:28 >
# 7 Re: Problem with java program
When you do it like int i=System.in.read(), then its the (int)byte value of the char '1' you get, and that goes from 0 to 255.
sjalle at 2007-11-11 22:44:26 >
# 8 Re: Problem with java program
To viviensiu:
I haven't tried using Scanner object for my input.
To sjalle:
So i will declare my variable to char and do like this
char input = (char)System.in.read();?? by the way thanks a lot for your help..
To all:
Thanks a lot for all your suggestions. :)
jareivy05 at 2007-11-11 22:45:29 >
# 9 Re: Problem with java program
You should read like I've done, using a BufferedReader wrapper for System.in, or use
the scanner class.
The code is easier to read when you isolate the user prompt&input in a method, wether you use scanner or reader.
sjalle at 2007-11-11 22:46:32 >