Results 1 to 10 of 10

Thread: hi new to java

  1. #1
    vodkayum is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default 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 03:07 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    much luck to ya

  3. #3
    vodkayum is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Found this in a book, now just got to make it work

    Integer.parseInt(temp);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]
    Best of luck.

  5. #5
    vodkayum is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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();
    }
    }


    }

  6. #6
    vodkayum is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 04:56 PM.

  9. #9
    vodkayum is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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.)

    Java 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");
    	
          }
        
    
    }
    C:\Programming\myfiles\java\misc>javac Dbm.java
    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 05:09 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you are going to read in your data using the BufferedReader, then it should be read into a String, not a double variable:
    Java Code:
    // not this:
    //double dbm = System.in.read();
    // but this:
    String dbmString = in.readLine();
    double dbm = Double.parseDouble(dbmString);
    It's important to place only String data in String variables and likewise double data in double variables. they are not interchangeable here.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •