Biting off more than I can chew...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nostradormouse
    New Member
    • Mar 2010
    • 1

    Biting off more than I can chew...

    Hi,

    My Perl is functional, but I finally decided to bite the bullet and learn Java, so please be gentle, I'm a newbie.

    I have 14 lines of tab seperated data in an external file, comprising an integer value 'n' (which may or may not be present), and two double values 't' and 'c' as follows;
    Code:
    2	0.5773502692	1
    	-0.5773502692	1
    3	0.7745966692	0.5555555556
    	0	0.8888888889
    	-0.7745966692	0.5555555556
    4	0.8611363116	0.3478548451
    	0.3399810436	0.6521451549
    	-0.3399810436	0.6521451549
    	-0.8611363116	0.3478548451
    5	0.9061798459	0.2369268850
    	0.5384693101	0.4786286705
    	0	0.5688888889
    	-0.5384693101	0.4786286705
    	-0.9061798459	0.2369268850
    I'd like to read the data into a multi-dimensional array with the following structure;
    Code:
    int [B]n[/B][]
    double [B]t[/B][], [B]c[/B][], [B]x[/B][], and [B]f[/B][]
    to allow for the products of number-crunching.

    I'm not yet familiar enough with java syntax to create, initialise, insert, manipulate, and write multi-dimensional array data of more than one type. Here I'd like help creating the data structure, and reading data in.

    Any suggested code segments, or direction to tutorial resources would be warmly appreciated.

    Thank you in advance.

    Regards,
    Nostradormouse
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Scanner class, if you're using Java 1.5 or up.

    Looks to me like you're reading in roots of complex numbers.
    I would suggest rearranging the data into objects or structures.

    Base level: original number
    --2nd level: root number
    ----3rd level: values of roots.

    So building backwards, you might have something like:
    Code:
    class ComplexRoot(
            public int t, c;
    )
    Then work up one level, use an array of ComplexRoots. Sorry couldn't think of a good name for this class.
    Code:
    class Solutions{
            public int n;
            ComplexRoot[] cr;
    }
    And finally have an array of Solutions
    Solutions [] s;

    code would go something like:
    Code:
    Scanner sc = new Scanner("inputfile.txt");
    int n;
    while (sc.hasNextLine()){
      if(! sc.hasNextInt()){ // just in case of newLine at end.
         break;
      }
      n = sc.nextInt();
      Solutions sol = new Solutions(n);
      s.add(sol);
      for (int i = 0; i < n; i++){
         t = sc.NextDouble();
         c = sc.NextDouble();
         sol.add(new ComplexRoot(t,c));
      }
    }
    The constructors are relatively easy. Sorry, didn't get the x[] and f[]
    Hope this helps, and doesn't confuse you too much.

    Comment

    Working...