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 2 of 2

Thread: Reding a file and storing the string as well as numeric value in array in specific format

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

    Default Reding a file and storing the string as well as numeric value in array in specific format

    I have a input file named file.txt with the contents
    Zone
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    Zone
    5.0 2.0 3.0
    25.0 3.0 4.0
    15.0 2.0 3.0
    25.0 3.0 4.0
    41.0 2.0 3.0
    25.0 3.0 4.0
    16.0 2.0 3.0
    25.0 3.0 4.0
    15.0 2.0 3.0
    25.0 3.0 4.0
    41.0 2.0 3.0
    Zone
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    I am looking for a output like


    I have a input file named file.txt with the contents

    Zone
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    1.0 2.0 3.0
    2.0 3.0 4.0
    Zone
    5.0 2.0 3.0
    25.0 3.0 4.0
    15.0 2.0 3.0
    25.0 3.0 4.0
    41.0 2.0 3.0
    25.0 3.0 4.0
    16.0 2.0 3.0
    25.0 3.0 4.0

    I am looking for a output like

    zone=1
    x=[1.0 2.0 1.0 2.0 1.0.......2.0 1.0]
    similarly y=[]
    z=[]
    then
    zone=2
    x=[5.0 25.0 15 25 41 25 16 25]
    similarly y=[]
    z=[]

    However, I am getting the values of x=[] for zone=2 from the starting of zone=1, as I am using x.add(scanner.nextDouble()).

    Please help me. Thanks in advance.

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package boltcal1103;

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.lang.String;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    /**
    *
    * @author admin
    */
    public class ZoneFille14 {

    public static void main(String[] args) throws FileNotFoundException {
    List<String> s1=new ArrayList<String>();
    List<Double> x = new ArrayList<Double>(); //Defining double arraylist for Fx
    List<Double> y = new ArrayList<Double>(); //Defining double arraylist for Fy
    List<Double> z = new ArrayList<Double>(); //Defining double arraylist for Fz
    String word="Zone",line=null;
    int count = 1;
    double x1=0.0;
    String s="Zone";
    Scanner scanner = new Scanner(new FileReader("file.txt"));


    while (scanner.hasNext())
    {

    line = scanner.next();

    if (line.equals(word))
    {
    System.out.println("zone=:"+count);

    for(int i = 0; i<8 ; i++)
    {

    x.add(scanner.nextDouble());
    y.add(scanner.nextDouble());
    z.add(scanner.nextDouble());

    }

    System.out.println(x);
    System.out.println(y);
    System.out.println(z);
    count++;
    }
    }
    scanner.close();

    }


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Reding a file and storing the string as well as numeric value in array in specific format

    Quote Originally Posted by Neha Suryawanshi View Post
    I have a input file named file.txt with the contents

    However, I am getting the values of x=[] for zone=2 from the starting of zone=1, as I am using x.add(scanner.nextDouble()).
    yes you get all values of x=[] for zone=2 from the starting of zone=1.

    Because you only create the list like below..

    List<Double> x = new ArrayList<Double>(); //Defining double arraylist for Fx
    List<Double> y = new ArrayList<Double>(); //Defining double arraylist for Fy
    List<Double> z = new ArrayList<Double>(); //Defining double arraylist for Fz

    but you need to clear the list inside the while loop that's like below..

    while(condition){
    // you clear the list like below..
    x.clear();
    y.clear();
    z.clear();
    //After you write your code here
    }

    Because every time the process enter the loop first clear the existing list and store the further values


    I think you want this exactly.. I hope this will help to you...
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Replies: 41
    Last Post: February 21st, 2013, 05:42 PM
  2. Reading into a File and storing it into an Array
    By loui345 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 21st, 2012, 11:48 PM
  3. Reading a txt file and storing them in a Java Array
    By FrozenFox in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 27th, 2012, 08:19 AM
  4. Storing data from a file in array and setting it to textbox
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2011, 10:17 PM
  5. Working out the day from numeric date (dd/mm/yy format).
    By ShaunB in forum Java Theory & Questions
    Replies: 6
    Last Post: April 23rd, 2011, 09:55 PM