Results 1 to 7 of 7
  1. #1
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default read only double input from text file

    hello everyone,

    i have some problems regarding reading the double input and ignore the integer input..
    i am writing a progran that can read the input fron a file which contains number like this:
    1 1 3.0376 0.0273 3.8290
    1 2 4.0762 3.7267 4.7282
    2 1 5.2373 3.2828 4.2728
    ...
    and i would like to read only the double value and ignore the integer at the first two row in the file.

    i currently used inpfile.hasNextDouble() but the reader just read all the number in input file as a double..

    anyone got some idea or tips please..??

    :confused:

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by napi1234 View Post
    hello everyone,

    i have some problems regarding reading the double input and ignore the integer input..
    i am writing a progran that can read the input fron a file which contains number like this:
    1 1 3.0376 0.0273 3.8290
    1 2 4.0762 3.7267 4.7282
    2 1 5.2373 3.2828 4.2728
    ...
    and i would like to read only the double value and ignore the integer at the first two row in the file.

    i currently used inpfile.hasNextDouble() but the reader just read all the number in input file as a double..

    anyone got some idea or tips please..??

    :confused:
    You can read only Strings from the input; if your files are like you described each String contain just digits and maybe a decimal dot. Check if the String contains a decimal dot and convert it to a double; otherwise simply ignore the String.

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,550
    Rep Power
    23

    Default

    Read each line of the file into a String. Split the String/line into words/tokens using String.split() or StringTokenizer then look at each word as JosAH suggested to determine if it is an integer or real number.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Norm View Post
    Read each line of the file into a String. Split the String/line into words/tokens using String.split() or StringTokenizer then look at each word as JosAH suggested to determine if it is an integer or real number.
    There's no need to read entire lines and split them; the OP is probably using a Scanner and it can read Strings delimited by white space.

    kind regards,

    Jos

  5. #5
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    thank you for the reply,
    ok, igot the idea, and as i am new in java so could you please give more details.
    heres my code:


    try {
    inpfile = new Scanner(
    new BufferedReader(new FileReader("input.dat")));
    }
    while (inpfile.hasNext()) {

    if (inpfile.hasNextDouble() && (firstnum==1)) {
    i = inpfile.nextDouble();
    }

    i am going to used "i" fron the input file which is double number and not the integer in further calculation.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by napi1234 View Post
    thank you for the reply,
    ok, igot the idea, and as i am new in java so could you please give more details.
    heres my code:


    try {
    inpfile = new Scanner(
    new BufferedReader(new FileReader("input.dat")));
    }
    while (inpfile.hasNext()) {

    if (inpfile.hasNextDouble() && (firstnum==1)) {
    i = inpfile.nextDouble();
    }

    i am going to used "i" fron the input file which is double number and not the integer in further calculation.
    You have a try block there without a catch clause nor a finally clause; that doesn't even compile.Summarizing: suppose you have read a String s; check if it has a dot in it (the indexOf( ... ) method would do fine for that). If it has convert it to a double (read the API for the Doubke class), otherwise ignore the String s and read a next one.

    kind regards,

    Jos

  7. #7
    napi1234 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    ok got it thank you very much !
    i will start from there and if i got any problems i will post next thread.
    thankyou very much..

Similar Threads

  1. Read text file
    By Sean04 in forum New To Java
    Replies: 18
    Last Post: 06-16-2010, 01:43 AM
  2. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  3. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 10:40 PM
  4. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

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