• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with final output value after input

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
line1++;
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;
line2++;
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");
while(number>0)
{
remainder=(number%base);
number= (char)((number-remainder)/base);
answer= remainder+answer;
}
System.out.print("The number in base "+(int)base + " is: "+ answer);
}
}e a big help. Thanks.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Michael
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic