jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes problem with final output value after input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with final output value after input" Watch "problem with final output value after input" New topic

problem with final output value after input

David Lafo

Joined: Oct 28, 2003
Posts: 3
im writing a program with input using System.in.read and when i imput a value no matter what it is the value that is returned is always 10.I'm not sure why but if someone can help me it would be greatly appreciated. I'm only allowed to use the System.in.read and string methods. This is the code so far that i hafve written so if someone could look at it and tell me what im doing wrong it would be a big help. thanks. the problem seems to be with the S.O.P right after the first two while loops but there is probably something else wrong im jsut not sure what it is.
import java.io.*;
public class old3
public static void main(String[] args) throws IOException
int inputLine1[] = new int[72];
int inputLine2[] = new int[72];
String answer = "";
char number = 0;
char base = 0;
int line1 = 0, line2 = 0;
int remainder = 0;
System.out.print ("Please select a base from 2 to 36:");
base = (char)System.in.read();
while ( (base != 10) && (base != 13) )
inputLine1[line1] = base;
base = (char)System.in.read();
base = (char)System.in.read();
System.out.println("The base selected is base "+(int)base );
System.out.print ("Please enter a number to be converted: ");
number = (char)System.in.read();
while ( (number != 10) && (number != 13) )
inputLine2[line2] = number;
number = (char)System.in.read();
number = (char)System.in.read();
System.out.println("The number in base "+(int)base+ " is: "+(int)number);
if (number == 0)
answer = ("0");
number= (char)((number-remainder)/base);
answer= remainder+answer;
System.out.print("The number in base "+(int)base + " is: "+ answer);
}e a big help. Thanks.
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73

Why do you have this? base will never be both 10 AND 13, right? So those whiles you have will never run.
Then you first ask the user which base to have, and then after the while-statement, the user should input base again, but there is no output to clue the user what to input unless he reads the code. Same for number, input twice.
I'm unsure about how you calculate the number in the base too... And why do you have that in a while too? Are you sure that you must use System.in.read() by the way? A BufferedReader would be better, wouldn't it? At least you don't have the problem that it will "skip" a input
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi David
From what you say, you have no need of those 2 arrays at all. I am guessing you want the user to input one number, then one base. So, bear in mind that System.in.read only returns the next byte from the stream, which is one character. What you need to do is get all the characters the user input up until they hit the return key, right? Look at the following code:

The character '\n' represents a newline. See how all the user input is captured, even if it is more than 1 character? I think you need to use this technique to get your input. You will then need to convert these strings to ints - I leave that as an exercise for you.
Hope this helps a bit.

"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
I agree. Here's the link: http://aspose.com/file-tools
subject: problem with final output value after input
It's not a secret anymore!