1 Attachment(s)
About Float and readLine() begginer problem.
Hi!
ATTENTION!!!: My first post on this forum
Note: I'm spanish, so, forgive me about my pathetic english fails like gramar and lack of bocabulary, and oaf course, terrifying hortographic fails (I'm shure that just now I have more than 10) for more information: I travel to Norway and I learn to SPEACK english, not to writte it XD.
Well!!, first of all, just to say that I try to search a solution to my "wandering" in this forum, but I still dont found it yet, I gess its because shoud be extreemly easy, but still imposible for me (I start this week with the book "Java programming for the absolute begginer" and its the first time in my life I put serious abaut to learn something of programming.
Ok after this small introduction of myself that I know noone cares a damn about it here it goes my cuestioning.
- as a part of the course I start one app to learn about mathematic posibilities in java, the program its called MathGame, withc the code look like this (working and with any problem:
Code:
import java.io.*;
public class MathGame{
public static void main(String args[]) {
int num1, num2;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("\nFirst Number: ");
num1 = Integer.parseInt(reader.readLine( ));
System.out.print("Second Number: ");
num2 = Integer.parseInt(reader.readLine( ));
reader.close( );
}
//simple exception Handling
catch (IOException ioe) {
System.out.print("some shit happens.... try again");
num1 = num2 = 1;
}
catch (NumberFormatException nfe) {
System.out.print("Number format incorrect, put a fucking number you assholle!");
num1 = num2 = 1;
}
//avoid this pitfal, ej: 1+1=11:
//System.out.print(num1 + " + " + num2 + " = " + num1 + num2);
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
System.out.println(num1 + " % " + num2 + " = " + (num1 % num2));
}
}
and in the console looks like this:
Attachment 3049
well that's nice , ok. But I wanna make 2 modifications (for my own learning pride)
1: Add decimals! so I tryed to ad Float to the code
2: make a last ecuation line with all mathematic thing I learn (like multiplication, division, parenthesis...)
then:
i add to the code the last line
Code:
System.out.println(num1 + "*" + num2 + "/" + "(" + num1 + " + " + num2 + ")" + "-" + num1 + " = " + ((num1 * num2 / (num1 + num2)) - num1));
and as far as I know it works well (any tip will be cool)
then I get stuk in the float stuff, searching by internet found this code, so I added:
Code:
float myFloat;
myFloat = Float.parseFloat(myInputStream.readLine());
so the final code looks like this:
Code:
import java.io.*;
public class MathGame{
public static void main(String args[]) {
int num1, num2;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
//my own:
float myFloat;
myFloat = Float.parseFloat(myInputStream.readLine());
try {
System.out.print("\nFirst Number: ");
num1 = Integer.parseInt(reader.readLine( ));
System.out.print("Second Number: ");
num2 = Integer.parseInt(reader.readLine( ));
reader.close( );
}
//simple exception Handling
catch (IOException ioe) {
System.out.print("some shit happens.... try again");
num1 = num2 = 1;
}
catch (NumberFormatException nfe) {
System.out.print("Number format incorrect, put a fucking number you assholle!");
num1 = num2 = 1;
}
//avoid this pitfal, ej: 1+1=11:
//System.out.print(num1 + " + " + num2 + " = " + num1 + num2);
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
System.out.println(num1 + " % " + num2 + " = " + (num1 % num2));
//my own:
System.out.println(num1 + "*" + num2 + "/" + "(" + num1 + " + " + num2 + ")" + "-" + num1 + " = " + ((num1 * num2 / (num1 + num2)) - num1));
}
}
then the console give me errors like:
(the parts in red are the leetter that the console shows me)
MathGame.java:19:cannot find simbol
symbol: variable myInputStream
myFloat = Float.parseFloat(myInputStream.readLine());
Then I erase the part "my" for pure intuition (see what the hell happens) and appears:
MathGame.java:19:cannot find simbol
symbol:method readLine()
myFloat = Float.parseFloat(InputStream.readLine());
Well, question is: how I put decimals in this tiny and useless aplication? amI in the right direction? or: were is the fucking problem!!!!!
XD
well, solution +explanation could be extreemly cool,
tusen takk!!!
Re: About Float and readLine() begginer problem.
May be to much explanation for so stupid problem....
Re: About Float and readLine() begginer problem.
oooooh! and if my hero can aslo explain me how to change the avatar will be even cooler!!!
Re: About Float and readLine() begginer problem.
i see how to change the avatar now, thanksXD
Re: About Float and readLine() begginer problem.
i don't understand (i don't know about java actually), why you use:
Code:
myFloat = Float.parseFloat(myInputStream.readLine());
where you declare "myInputStream" variable? and you said, you changed it to be like this?
Code:
myFloat = Float.parseFloat(InputStream.readLine());
(the same question as the above...)
wasn't it should be:
Code:
myFloat = Float.parseFloat(reader.readLine());
right? because "reader" is your input stream...
CMIIW
1 Attachment(s)
Re: About Float and readLine() begginer problem.
jajajjaa now I see ok that was preety stupid mistake XD ffff...
ok I changed and when compiling apears me this error:
Attachment 3076
I gess I sould put some values in the parenthesis?
here the new code:
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MathGame{
public static void main(String args[]) {
int num1, num2;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
//my own:
float myFloat;
myFloat = Float.parseFloat(reader.readLine());
try {
System.out.print("\nFirst Number: ");
num1 = Integer.parseInt(reader.readLine( ));
System.out.print("Second Number: ");
num2 = Integer.parseInt(reader.readLine( ));
reader.close( );
}
//simple exception Handling
catch (IOException ioe) {
System.out.print("some shit happens.... try again");
num1 = num2 = 1;
}
catch (NumberFormatException nfe) {
System.out.print("Number format incorrect, put a fucking number you assholle!");
num1 = num2 = 1;
}
//avoid this pitfal, ej: 1+1=11:
//System.out.print(num1 + " + " + num2 + " = " + num1 + num2);
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
System.out.println(num1 + " % " + num2 + " = " + (num1 % num2));
//my own:
System.out.println(num1 + "*" + num2 + "/" + "(" + num1 + " + " + num2 + ")" + "-" + num1 + " = " + ((num1 * num2 / (num1 + num2)) - num1));
}
}
Thank you, by the way...