Announcement

Collapse
No announcement yet.

Reading numbers from text files in Java

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading numbers from text files in Java

    Although, I am in enginnering line, I enjoy programming a lot (EE has got a connection with programming). In my spare time, I do fun programming. Below is Java code of an FSM (Finite State Machine) for identifying and reading numbers from a text file. It strips every thing off (e.g. white space, line feeds, carriage returns, text, or any character) and just reads numbers (integer and real).

    Although not very neatly designed, it will never fail you.
    public static double readValue() throws FileNotFoundException, IOException {

    String myString = "";
    String chString = "0123456789.";
    int state = 1;
    double value;

    do
    {
    char buffer = (char)fptr.read();
    if(chString.indexOf(buffer) == -1)
    {
    if((state == 1) | (state == 2))
    state = 2;
    if(state == 3)
    {
    value = Double.parseDouble(myString);
    break;
    }
    }
    if(chString.indexOf(buffer) >= 0 && (state == 1) | (state == 2) | (state == 3))
    {
    myString = (new StringBuilder()).append(myString).append(buffer).toString();
    state = 3;
    }
    } while(true);

    return value;

    }

    fptr is the handle of the text file you would be reading data from. It is a globally defined variable of type FileInputStream with definition given below.

    FileInputStream fptr = new FileInputStream(new File("C:/Files/ABC.txt"));

    ABC.txt is the file name to be read from. If locally defined in another function and passed as parameter to the FSM above, then the FSM function header would look like below.

    public static double readValue(FileInputStream fptr) throws FileNotFoundException, IOException
    Last edited by Toramana; 11-02-2011, 09:44 PM.

  • #2
    and u can do this with a single line in matlab with a netlist :|
    It's enough that I know, Our time had a home, In your heart..Was a place..But the glass always breaks..

    Comment


    • #3
      Originally posted by Talal View Post
      and u can do this with a single line in matlab with a netlist :|
      Yes, but such standard tools are not adequate for tokenizing plain text or data e.g., breaking the string {(56+78/89)+678.90*5^2} into individual categories (as required in language processing) as below,

      { left braces
      } right braces
      ( left parenthesis
      ) right parenthesis
      + addition operator
      *multiplication operator
      /division operator
      ^ exponentiation operator
      56 78 89 678 5 2 integer numbers
      678.90 real number

      java.util has got classes e.g. tokenizer, scanner, etc. that do simple lexical taks but cannot handle complex tokenization as above.
      Last edited by Toramana; 11-05-2011, 10:54 PM.

      Comment


      • #4
        Yeah but believe me toramana you're not gonna be using java much in EE ..other than a first year java course it's all matlab..but you got good coding skills, Admin would be proud
        It's enough that I know, Our time had a home, In your heart..Was a place..But the glass always breaks..

        Comment


        • #5
          Talal, EE's can fill a CS job, but the same can't be said about a CS guy. Yes, I agree, CS majors might be stronger in programming but an EE with sufficient practice can be just as competitive. In the US, thousands of EE's are hired as Software Engineers. You do not need a degree in CS to be taken seriously. Generally, in interviews, they ask you to code a problem on the spot. If an EE can come up with the algorithm, the job might be his. EE's are applied Physicists so their versatility is unparalleled.

          Canada unfortunately doesn't have as big of a market as the US, so the demand won't be as higher. Here in the US, E.E's get scooped instantly, along with CpE's and CS's.
          Even Adobe Photoshop can't change me.

          A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

          For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


          The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

          -Ferishta, 1560–1620

          Comment


          • #6
            I love programming but don't know where to start from. I have tried some sources but could not get the basic idea what programming is and how is it done.

            Comment


            • #7
              Originally posted by emkhan View Post
              I love programming but don't know where to start from. I have tried some sources but could not get the basic idea what programming is and how is it done.
              Let's start from the basic. Start from Java, let's code a program that will find an even number.

              First, download Eclipse IDE.
              Once downloaded:
              Click file>New Java project>
              then,
              file, new class.

              Once you have eclipse up and running,

              code the following:

              import java.util.*;

              public class PF {
              public static void main(String[] args){
              Scanner sc = new Scanner(System.in);
              int emkhan;

              System.out.println("Hi, thanks to PF, I will learn how the algorithm of finding an even number");
              emkhan=sc.nextInt();

              if(emkhan%2==0){
              System.out.println("The number, with all due respect, is even");
              }
              else{
              System.out.println("Not even");
              }
              }
              }




              Start from Java, let's code a program that will find an even number.
              Even Adobe Photoshop can't change me.

              A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

              For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


              The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

              -Ferishta, 1560–1620

              Comment


              • #8
                Thanks but it will not work I think. I need a proper way to go on that I have asked you in pm.

                Comment


                • #9
                  Originally posted by emkhan View Post
                  Thanks but it will not work I think. I need a proper way to go on that I have asked you in pm.
                  Buy that book, and read it.
                  Even Adobe Photoshop can't change me.

                  A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

                  For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


                  The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

                  -Ferishta, 1560–1620

                  Comment


                  • #10
                    Originally posted by Admin Khan View Post
                    Buy that book, and read it.
                    I will inshallah.
                    Thank you a lot.

                    Comment


                    • #11
                      emkhan i posted a website way back, it has tons of books on programming and stuff i believe its freebookspot.com
                      It's enough that I know, Our time had a home, In your heart..Was a place..But the glass always breaks..

                      Comment


                      • #12
                        public static int getSize(String inputString){

                        String myStr="";

                        for(int j=0; j < inputString.length();j++)
                        if ("1234567890".indexOf(inputString.charAt(j)) >= 0)
                        myStr+=inputString.charAt(j);
                        return Integer.parseInt(myStr);

                        }//getSize(String inputString)

                        If you want to return double-type number, then make the following changes...

                        change "1234567890" to "1234567890.E" and public static int to public static double and return Integer.parseInt(myStr) to return Double.parseDouble(myStr).

                        Comment


                        • #13
                          i thought simple getting numbers from a "text area" or "text field" which is pretty much simple

                          iNumbers = double.parseDouble(txfAB.getText() )

                          anyway thank you.
                          High achievement always takes place in the framework of high expectation.

                          Comment


                          • #14
                            Originally posted by Afghanistan2010 View Post
                            i thought simple getting numbers from a "text area" or "text field" which is pretty much simple

                            iNumbers = double.parseDouble(txfAB.getText() )

                            anyway thank you.
                            was talking of parsing strings or text files for numbers or other syntatic categories....not extracting values from text fields...

                            Comment


                            • #15
                              Can you tell me how to get values from an array that i already have prepared.

                              like array [] = new String [3]

                              array [0]= "apple"
                              array [1] = "mango"
                              array [2] = "banana"

                              now i want to get one of the three fruits randomly
                              Last edited by Afghanistan2010; 11-24-2011, 12:40 PM.
                              High achievement always takes place in the framework of high expectation.

                              Comment

                              Working...
                              X