Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,247 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,215 people online right now. Registration is fast and FREE... Join Now!




Multiple Arrays

2 Pages V  1 2 >  
Reply to this topicStart new topic

Multiple Arrays, Double pots merged

bigticket61
12 Oct, 2008 - 06:03 PM
Post #1

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

How do I read a file with 3 columns and 30 rows of data, where each column should go into a separate array

EXAMPLE

-95.7 2.268 297.0
22.2 0.173 303.8
-70.4 1.459 291.1
51.0 1.131 298.8
-15.0 1.515 280.6
-50.0 2.132 280.9
-46.2 1.781 277.3
-7.2 2.123 289.5
-83.9 1.464 284.7
-13.8 1.323 290.0
-37.6 0.272 279.2
-70.9 1.171 293.1
-60.9 0.702 286.5
72.5 2.240 305.6
-67.3 0.452 307.1
-82.3 1.929 308.7
-23.2 0.684 282.2
69.0 1.381 297.7
-48.6 2.966 273.1
49.1 0.066 300.4
-95.1 1.397 285.9
-7.4 0.435 282.6
84.1 1.489 295.9
48.5 1.403 288.9
67.5 2.915 279.8
15.3 2.761 305.0
52.3 1.751 284.0
12.8 0.954 289.8
78.6 0.762 285.5
47.6 2.621 295.9

User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Arrays
12 Oct, 2008 - 06:14 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Multiple different possibilities...
not very elegant but easy to understand:

CODE

Scanner rFile = new Scanner("file.txt");
double[] array1 = new double[30];
double[] array2 = new double[30];
double[] array3 = new double[30];

for(int i = 0; i < 30; i++) {
    array1[i] = rFile.nextDouble();
    array2[i] = rFile.nextDouble();
    array3[i] = rFile.nextDouble();
}


User is offlineProfile CardPM
+Quote Post

bigticket61
RE: Multiple Arrays
13 Oct, 2008 - 02:55 PM
Post #3

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

QUOTE(pbl @ 12 Oct, 2008 - 07:14 PM) *

Multiple different possibilities...
not very elegant but easy to understand:

CODE

Scanner rFile = new Scanner("file.txt");
double[] array1 = new double[30];
double[] array2 = new double[30];
double[] array3 = new double[30];

for(int i = 0; i < 30; i++) {
    array1[i] = rFile.nextDouble();
    array2[i] = rFile.nextDouble();
    array3[i] = rFile.nextDouble();
}



What does rFile do?

Thanks for your help
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Multiple Arrays
13 Oct, 2008 - 03:05 PM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
rFile is an instance of the scanner class. It contains a pointer to the position on your harddrive where you can find the values stored in file.txt

by calling the nextDouble method it reads the values in sequence, interpreting them as doubles.
Dunno if that actually made things any clearer or just more complicated.

The effect however is that the values are read from the file and stored in the array.

This post has been edited by Gloin: 13 Oct, 2008 - 03:07 PM
User is offlineProfile CardPM
+Quote Post

bigticket61
RE: Multiple Arrays
13 Oct, 2008 - 04:10 PM
Post #5

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

Can I use inFile also or is that going to screw things up.
User is offlineProfile CardPM
+Quote Post

bigticket61
RE: Multiple Arrays
13 Oct, 2008 - 06:31 PM
Post #6

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

How do i find the rows where they occur?


-the minimum value and the row at which this occurs
e.g. 95.7at row 1 for the first column

-the maximum value and the row at which this occurs
User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Arrays
13 Oct, 2008 - 06:36 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Not very complicated

CODE

double x[100];
double min = x[0];
int index = 0;

for(int i = 1; i < x.length; i++) {
    if(x[i] < min) {
        min = x[i];
        index = i;
    }
}

System.out.println("min is x[" + index + "] = " + x[index]);

User is offlineProfile CardPM
+Quote Post

bigticket61
RE: Multiple Arrays
13 Oct, 2008 - 07:01 PM
Post #8

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

QUOTE(pbl @ 13 Oct, 2008 - 07:36 PM) *

Not very complicated

CODE

double x[100];
double min = x[0];
int index = 0;

for(int i = 1; i < x.length; i++) {
    if(x[i] < min) {
        min = x[i];
        index = i;
    }
}

System.out.println("min is x[" + index + "] = " + x[index]);



Thank You. But where does the print the row where it is located.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Arrays
13 Oct, 2008 - 07:09 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(bigticket61 @ 13 Oct, 2008 - 08:01 PM) *

Thank You. But where does the print the row where it is located.


There is only one System.out.println in the code I provided you with

User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Arrays
13 Oct, 2008 - 07:11 PM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(bigticket61 @ 13 Oct, 2008 - 05:10 PM) *

Can I use inFile also or is that going to screw things up.

what is inFile ?
You mean you want to rename "rFile" "inFile" ? No problem
User is offlineProfile CardPM
+Quote Post

bigticket61
RE: Multiple Arrays
13 Oct, 2008 - 07:39 PM
Post #11

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 13

Having some problems trying to run this program.

How do I get all of the arrays to execute?
How do I properly output the information correctly?

Any help would be appreciated. Thank you in advance.


Here are the requirements:

Your program should read a file with 3 columns and 30 rows of data....


Each column should go into a separate array.
Once the data is read in, your program should display a summary for each column including:

- the minimum value and the row at which this occurs
e.g. 95.7at row 1 for the first column
● the maximum value and the row at which this occurs
● the average
the sum.




CODE


package ASSIGN3;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class a3 {

    /**
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException {
         final String INPUTFILE = "src/ASSIGN3/colunms.txt";  
        
         /*
          */  
        
         Scanner inFile = new Scanner( INPUTFILE );
         String header = inFile.nextLine();
        
         final int COLUMN_COUNT = 30;
         double[] array1 = new double[ COLUMN_COUNT ];
         double[] array2 = new double[ COLUMN_COUNT ];
         double[] array3 = new double[ COLUMN_COUNT ];

        
         for(int i = 0; i < COLUMN_COUNT; i++) {
             array1[i] = inFile.nextDouble();
             array2[i] = inFile.nextDouble();
             array3[i] = inFile.nextDouble();
         }
         inFile.close();
        
         double sum = 0.0;
        
         for(int i=0; i<array1.length; i++)
             sum+=array1 [i];
                 
         double average = sum / array1.length;
        
        
        
        
         double max= array1[0];
         int index = 0;
         for(int i=1; i<array1.length; i++){
                if (array1 [i]> max)
                 max = array1 [i];
                index = i;
         }
         System.out.println("max is x[" + index + "] = " + array1[index]);
        
         double min = array1[0];
        
         for(int i = 1; i < array1.length; i++) {
             if(array1[i] < min) {
                 min = array1[i];
                 index = i;
             }
         }

         System.out.println("min is x[" + index + "] = " + array1[index]);
            
             System.out.println( header );
        
            
        
         System.out.printf("The information in array1 is: %.2f inches%n" +sum + average + max + min + index);  
         System.out.printf("The information is array2 is: %.2f inches%n" +sum + average + max + min + index);
         System.out.printf("The information is array3 is: %.2f inches%n" +sum + average + max + min + index);
        
        
    
}
                 
        
}



This post has been edited by bigticket61: 13 Oct, 2008 - 07:41 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Arrays
13 Oct, 2008 - 07:43 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Topics merged
Please avoid double posting mad.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:55AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month