Results 1 to 12 of 12

Thread: char to double

  1. #1
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default char to double

    I've searched the forum and the web and tried many tips that I've found but they don't seem to work, I'd like to know what I'm doing wrong.
    I'm opening an ascii file (I'm pretty sure it is) with characters and I'd like to convert them to double so I may perform arithmetic on them.
    Below is my unaltered code so far, can't seem to attach the file I want to convert, 'manage attachments' doesn't seem to work, perhaps due to pop-up blocker etc?
    Hope this is sufficient, it's my first post, please let me know if my etiquette is off.
    Much thanks in advance.

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.nio.Buffer;
    import java.lang.*;


    public class ReadAsciiFile4 {



    public static void main( String[] args )

    {

    //Creating HiRes Conversion Factor and printing it to screen

    double x = 5.964;
    double a = 10;
    double b = -7;
    double y = Math.pow(a,b);
    double h = x * y;

    System.out.println("HiRes Unit of Conversion for Acii Counts to Volts is: " + h);


    Scanner scan = new Scanner(System.in);
    System.out.print("Enter File Name with Extension: ");
    String myFile = scan.nextLine();

    StringBuffer buf = null;
    FileReader fr = null;

    try {
    fr = new FileReader( myFile );


    buf = new StringBuffer();

    double theChar;

    while( ( ( theChar = fr.read() ) != -1 ) ) {



    buf.append( (char) theChar );


    }
    }
    catch( IOException ioe ) {
    ioe.printStackTrace();
    }
    finally {
    if( fr != null ) {
    try {
    fr.close();
    }
    catch( IOException ioe ) {
    }
    }
    }

    if( buf != null )
    System.out.println( "file contents: " + buf );
    }
    }

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: char to double

    Please edit your post and wrap the code with code tags. See: BB Code List - Java Programming Forum
    characters and I'd like to convert them to double so I may perform arithmetic on them.
    Can you explain what you are trying to do? Say you have two char values: 's' and 'q' what do you want to do with them?

    What problems are you having with the code you posted?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: char to double

    Thank you for the tips on code tags and BB Code List.

    The file I am opening is a single-column array of ascii characters but they are integers such as -300, 475, 22, etc.
    The file is a .00 file, essentially a list of ascii 'counts' (seismic data)

    so if s = any or all array elements, and q = the double value I have assigned as h in my code, I want to multiply h*s and generate and print precision numbers into an array of doubles.

    I am guessing my code terminology is not precise at the moment, I am not CS.

    The code I posted works fine at the moment, but as soon as I try to do arithmetic on my array I get weird results, from what I could find on web it seems my 'numbers' aren't in numeric form yet.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: char to double

    Does the file contain ASCII text consisting of numeric digits like: -300, 475, 22, with spaces and commas (,) separating the groups of digits? Does the file contain separate lines of data or is the data all on one line?

    You could use the Scanner class's methods to read the text digits into numeric variables in the program.

    What do you want to do with the numeric data in the file?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: char to double

    Yes, ASCII text with numeric digits as you referenced. Each set of numeric digits are on a new line, no commas or spaces

    I want to multiply each number from each line by a constant double I assigned.

    Eventually I would like to plot the data in a GUI.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: char to double

    Are you saying that there is only one set of digits on each line? For example: 123 would be the only 3 characters on a line.
    The Scanner class's method should be very easy to use to read those numbers, one by one into program variables.
    Once you read the numbers into program variables, you will be able to work with them as desired.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: char to double

    Yes, one set of digits per line, example:

    123
    456
    789

    I found this:
    Java Code:
         Scanner sc = new Scanner(System.in);
         int i = sc.nextInt();
    I am assuming I can read each line as a string?

    Java Code:
         Scanner sc = new Scanner(System.in);
         doube i = sc.nextString();

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: char to double

    There are methods the will read the digits and convert them to numeric values like int and double. The first example does that.
    No need to read Strings and then convert them.

    Read the API doc for the Scanner class to see the methods and how to use them.
    http://docs.oracle.com/javase/7/docs/api/
    Use browser's Find for: Scanner, click on link and doc is shown in main frame.
    Last edited by Norm; 09-24-2012 at 11:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: char to double

    Yes thanks, I've been looking through these.
    My current issue is that the small program I have written uses a FileReader and StringBuffer and I'm not sure how to incorporate this Scanner method I need, I have tried various things and it does not work.
    To use these methods I might have to get rid of the FileReader and StringBuffer and take another path but I'm not sure. I'm sure there's some easy step to incorporate this method but I am a beginner so it's not that obvious to me.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: char to double

    it does not work.
    Can you explain what you want to do with the contents of the file?
    The FileReader and StringBuffer are for getting String data.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: char to double

    If you already have a FileReader then wrap it in a BufferedReader.
    You'll find the API in the link Norm posted above.
    That has a readLine method.
    If you use that for each line you will get a String with the number you are interested in.
    The Double class has a method for parsing a String into a double.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    shred is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: char to double

    Thank you both! With your help I was able to do it!
    There are more steps I need to take because now my results are no longer delimited, i.e printing doubles with no spaces between values, but I was able t get to this point which I am happy about.
    See below, thank you again!


    Java Code:
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.nio.Buffer;
    import java.lang.*;
    import java.io.BufferedReader;
    import java.lang.Object;
    import java.io.Reader;
    
    public class ReadAsciiFile7 {
    
      public static void main( String[] args )
    
    {
    
    //Creating HiRes Conversion Factor and printing it to screen
    
         double x = 5.964;
         double a = 10;
         double b = -7;
         double y = Math.pow(a,b);
         double h = x * y;
    
         System.out.println("HiRes Unit of Conversion for Acii Counts to Volts is: " + h);
    
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter File Name with Extension: ");
        String myFile = scan.nextLine();
    
        StringBuffer buf = null;
        FileReader fr = null;
    
        try {
    
    BufferedReader in
          = new BufferedReader(new FileReader( myFile ));
    
          buf = new StringBuffer();
    
    String theChar;
    
          while( ( ( theChar = in.readLine() ) != null ) ) {
    
    Double dub = Double.valueOf(theChar);
    
            buf.append( (Double) dub*h );
    
          }
        }
        catch( IOException ioe ) {
          ioe.printStackTrace();
        }
        finally {
          if( fr != null ) {
            try {
              fr.close();
            }
            catch( IOException ioe ) {
            }
          }
        }
    
        if( buf != null )
          System.out.println( "file contents: " + buf );
      }
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  2. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  3. Converting double to char array?
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 08-29-2010, 07:32 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM

Posting Permissions

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