• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Trouble with system.in manipulation

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So 2 would be the value 50 in ascii, but then how should I set it so it is 2?
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic