aspose file tools*
The moose likes Beginning Java and the fly likes Trouble with system.in manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with system.in manipulation" Watch "Trouble with system.in manipulation" New topic
Author

Trouble with system.in manipulation

George Han
Greenhorn

Joined: Aug 17, 2005
Posts: 17
I keep getting 1507045888, when i do 2 for base and 2 for exponent.
Here is my code
import java.lang.String;
class values {
int base;
int exponent;
}
class baseExponent {
public static void main(String args[]) throws java.io.IOException{
values important = new values();
System.out.println("What is the base?");
important.base = (char)System.in.read();
System.in.read();
System.out.println("What is the exponent?");
important.exponent = (char)System.in.read();
System.in.read();
important = math(important);
System.out.println(important.base);
}
public static values math(values Assistance) throws java.io.IOException{
values myAssistance = new values();
int power = Assistance.exponent;
int start = Assistance.base;
int keepValue = 1;
int timekeeper = 1;
if (power == 0) {
System.out.println("The Answer is 1");
} else{
if (power > 0) {
do{
keepValue = keepValue * start;
timekeeper = timekeeper + 1;
}while(timekeeper < power);
} else{
do{
keepValue = keepValue * start;
timekeeper = timekeeper + 1;
}while((timekeeper * -1) > power );
}
if (power < 0) {
keepValue = 1/keepValue;
}
}
myAssistance.base = keepValue;
return myAssistance;
//System.out.println("The answer is " + keepValue);
//System.out.println("What is the base?");
//int base2 = (int)System.in.read();
//System.in.read();
//System.in.read();
//System.out.println("What is the exponent?");
//int exponent2 = (int)System.in.read();
//System.in.read();
//System.in.read();
//math(base2, exponent2);
}
}
Plz help me fix this problem
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10


Your code is much more readable in code tags.

I think you do not want to cast your input as a char.

For example:
important.base = (char)System.in.read();

If you do a System.out.println( "important.base = " + important.base );
immediately after the above line, you will see that what is printed out is not what you thought it would be.

You might find this thread on getting user input helpful.
[ August 19, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
From another view,
What you enter thru the keyboard is ASCII characters. The binary value of the character 2 from the keyboard is not decimal 2 but rather decimal 34 (x22). To get the decimal value, you need to convert the input from character to int. See the Integer class for methods to do this.
George Han
Greenhorn

Joined: Aug 17, 2005
Posts: 17
So 2 would be the value 50 in ascii, but then how should I set it so it is 2?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
Whoops, in looking the hex value for '2' I got to the wrong row.
Yes, the decimal value of the char '2' is 50 or 0x32.
Not sure what "in ascii" means.

The contents of a byte of computer memory can be read/interpreted many ways.
You can talk of it as an ASCII char ('2') or as hex 0x32 or as decimal 50.


The Integer class has methods for converting char/String to int values.
parseInt() and decode() via Integer.intValue() for example.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
Or a quick and dirty way would be to subtract '0' from what is input and test that the result's not greater than 9. '2' - '0' = 2
George Han
Greenhorn

Joined: Aug 17, 2005
Posts: 17
umm yeah I tried reading parseInt, but I still dont know how to use it could you give me a sample of how im suppose to do it?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
One problem is that when you use the read() method, you get back a byte in the form of an int; you don't get a char. So methods that convert a char to an int are not going to help you. I actually think Norm's "quick and dirty" way may be useful (although not very readable/maintainable) if you really have some reason that you don't want to use the BufferedReader way of reading input that I linked to earlier.
[ August 22, 2005: Message edited by: Marilyn de Queiroz ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
As an academic exercise:
To get a String from int/byte you can cast the int to char and then convert to a String.


Output:
int=50, char=2, str=2
George Han
Greenhorn

Joined: Aug 17, 2005
Posts: 17
I didnt understand how i could use Buffered and for the turning into char would not help me cause I need to manipulate it so that I get the base ^ exponent.
Josh Peter
Greenhorn

Joined: Aug 21, 2005
Posts: 1
import java.io.*;


public class Test {


public static void main (String[] args) throws Exception {
int x,y;


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Base: ");
x=Integer.parseInt(br.readLine().trim());
System.out.print("Exponent: ");
y=Integer.parseInt(br.readLine().trim());
System.out.println("Result: "+result(x, y));



}

public static double result(int x, int y) {

return Math.pow(x,y);
}
}

O_o is that that u want?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with system.in manipulation