Results 1 to 5 of 5
  1. #1
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question How to store 5 numbers from a file into 5 seperate variables?

    Ok, I have 5 numbers in a file. Example: 5 8 7 2 1

    they are stored in the file just like that with no commas and I need to read that file and store each number into a variable so I can compare each number seperately to another number.

    I tried reading the post earlier from someone else but I don't think that this is the same kind of problem.

    I could store a seperate file for each number and compare that way, but I am trying to be more efficient and save space. Could someone please tell me how to do this.

    Thank you.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Do it with Scanner class....

    have some experiments on nextInt() method.....
    freedom exists in the world of ideas

  3. #3
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    Ok, I did a little research on this and I found this code. I modified it a little to work for me, but I know it is not correct, I think that I am closer though, could you please help me understand what I am doing wrong, or need to do next?? thank you


    Java Code:
    import java.util.Scanner;   
    import java.io.*;   
      
    \\this section will be in an event created by a calculate button
     
            Scanner scanner = new Scanner(new File("test.txt"));   
               
            while (scanner.hasNext())   
                int a = (scanner.next());
                int b = (scanner.next());
                int c = (scanner.next());
                int d = (scanner.next());
                int e = (scanner.next());

    Is this correct?

    I need 5 variables that will store the 5 numbers on the same line in text file so each variable will be available for use later in the code.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Put it on a method instead... and invoke it when some event fired....

    Don't forget to use close() method after that process....

    Java Code:
    while (scanner.hasNext())   
                int a = (scanner.next());
                int b = (scanner.next());
                int c = (scanner.next());
                int d = (scanner.next());
                int e = (scanner.next());
    be careful when to use braces and when not to use it....

    that code would only store the data from the file to variable a.

    And about that implementation, i think you should initialize those variables on member's field....(global variable)

    So that, you can just implement directly setters/getters method...
    Last edited by sukatoa; 06-09-2008 at 04:54 PM.
    freedom exists in the world of ideas

  5. #5
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default how-store-5-numbers-file-into-5-seperate-variables

    Thank you for your help sukatoa

    I used the scanner class and nextInt() method like you said, and it almost looked similar to the code I posted earlier but I didn't use the has next, and put nextInt() instead of just next when adding to the variables of the type int.

    If I had my code in front, I would have posted it for all to benefit if they are as novice as myself.

    Thanks again.

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 10:30 AM
  2. Replies: 3
    Last Post: 04-07-2008, 07:02 PM
  3. How to store property file into key value pair
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-05-2008, 11:16 AM
  4. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 10:45 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
  •