Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Change it to array

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Change it to array

    Hi guys,

    let's assum that we have the following program:

    import java.io.*;
     
     
    class readFile {
    public static void main(String[] args) {
    String fileName = "store"+ ".txt";
    String line;
     
    try {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    line = in.readLine();
    while (line != null) {
     
    System.out.println(line);
    line = in.readLine();
     
    }
     
    in.close();
    } catch (IOException iox) {
    System.out.println("Problem reading " + fileName);
    }
    }
    }

    The file have the following inputs:

    Jcomputer 12 33
    usb 2 34
    maps 44 41
    ups 23 22

    The column are 3 and the line are 4, one string and 2 integers. How can I put them in array (not arraylist)? Any ideas?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Change it to array

    Read the file a whole line at a time using something like:

    // fileInput is a Scanner object on the file
    String[] lineOfData = fileInput.nextLine().split( " " );

    (I didn't look at your code, so Scanner is just a suggestion. Use your own approach to read the file a line at a time.)

    Then process the array lineOfData as needed. Either use as is, or send it to a constructor to create an object using the data in the array. Do not move the data into 3 parallel arrays.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Change it to array

    Quote Originally Posted by mikerousse View Post
    The column are 3 and the line are 4, one string and 2 integers. How can I put them in array (not arraylist)? Any ideas?
    What you have not explained is what type of array you want/need.

    From each line you can have a String[] (as explained by GregBrannon), so you could have a String[][] to contain all datas. This can or cannot be acceptable, depending on what you do subsequently.

    Your file is a "store", so I imagine that each row is a "product". You could create a class Product that "models" the informations of a row. So, at the end, you could have a Product[] array. This is a very good solution (at least better than String[][]).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Change it to array

    How can I do that?
    Any code examples?

Similar Threads

  1. Change Game
    By peter1212 in forum The Cafe
    Replies: 2
    Last Post: October 13th, 2013, 06:49 AM
  2. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  3. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  4. change DATE
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2012, 11:44 AM
  5. change Name
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 3rd, 2012, 05:40 PM