Results 1 to 10 of 10
Thread: hi new to java
- 02-01-2009, 02:03 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
hi new to java
Hey my name's Brent and I presently got some extra time so I have decided to teach my self java. I have very little experience in programming. I've tinkered a bit but I have always wanted to learn, so now I'm dedicated and motivated. Wish me luck.
First question, I'm starting with command line code. I've played with html scripts in the past but now I want to learn the guts.
I am trying to enter a number using the system.in.read(); command.
I set example x=system.in.read();
then System.out.println(x);
if I enter 50 it displays 53
Why is this? weird!Last edited by vodkayum; 02-01-2009 at 02:07 PM.
-
much luck to ya
- 02-01-2009, 03:37 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Found this in a book, now just got to make it work
Integer.parseInt(temp);
-
out of context, it's hard to know what trouble you may be having with it. If you can't get it to work, you may wish to show us more code.
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Best of luck.Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 02-01-2009, 03:40 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
import java.io.*;
public class Dbm
{
public static void main(String [] args)
{
try
{
System.out.println("enter dBm");
InputStreamReader reader =new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
String dbm = in.readLine();
dbm=Integer.parseInt(temp);
double tdbm=1.0*dbm;
System.out.println(tdbm );
double tempdbm= 1.0 * dbm;
System.out.println(tempdbm );
double w= .001 * Math.pow(10, 50 *0.1);
System.out.println(w +" Watts");
}catch(IOException e)
{
e.printStackTrace();
}
}
}
- 02-01-2009, 03:45 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Seemed like a simple project, convert dBm to watts
got the formula right. I also made a menu that's pretty cool. I am making like a quick electronics converter program and all the pieces are in place, just can't input numbers properly.
-
You need to parse the String which is dbm. If you want it to be parsed to a double, then don't parse it first as an int, use Double.parseDouble(...):
Java Code:String dbm = in.readLine(); double tdbm = Double.parseDouble(dbm); // you are parsing the string entered, dbm
-
also, you'll need to include this tdbm into your formula for calculating Watts. Scanner would simplify your input I think.
Java Code:import java.util.Scanner; public class Fubar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter dBm: "); double dBm = scanner.nextDouble(); scanner.nextLine(); // swallow the end of line char double power = Math.pow(10, dBm/10) / 1000; } }Last edited by Fubarable; 02-01-2009 at 03:56 PM.
- 02-01-2009, 04:03 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
cool, thanks. I cleaned it up a bit and now I'm down to 1 error. What does this one mean? (error is at bottom of post.)
C:\Programming\myfiles\java\misc>javac Dbm.javaJava Code:import java.io.*; public class Dbm { public static void main(String [] args) { System.out.println("enter dBm"); InputStreamReader reader =new InputStreamReader(System.in); BufferedReader in =new BufferedReader(reader); double dbm = System.in.read(); dbm=Double.parseDouble(dbm); double tdbm=1.0*dbm; System.out.println(tdbm ); double tempdbm= 1.0 * dbm; System.out.println(tempdbm ); double w= .001 * Math.pow(10, 50 *0.1); System.out.println(w +" Watts"); } }
Dbm.java:15: parseDouble(java.lang.String) in java.lang.Double cannot be applied
to (double)
dbm=Double.parseDouble(dbm);Last edited by vodkayum; 02-01-2009 at 04:09 PM.
-
If you are going to read in your data using the BufferedReader, then it should be read into a String, not a double variable:
It's important to place only String data in String variables and likewise double data in double variables. they are not interchangeable here.Java Code:// not this: //double dbm = System.in.read(); // but this: String dbmString = in.readLine(); double dbm = Double.parseDouble(dbmString);
Also note that angle brackets <> don't work for code tags. See my post above for more info.
Also, I much prefer the use of Scanner rather than BufferedReader to get user input, but YMMV.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks