Printing from various methods.

Sdot718
Sdot718 used Ask the Experts™
on
When I try to combine the data collected through my two methods and print through main the data ends up null or all zeros.

(1)How do I get  the data collected from the methods to print through main so it can be as such
 Name  -- Score

(2)Also, how can I clear up the System.out prints of sums to have it looped to each Sum Array (Sum 1 through Sum9).  
import java.io.*;
import java.util.*;
import java.lang.*;



public class Assignment10
{
	public static double lowestScore = Double.MAX_VALUE;
	public static double highestScore = Double.MIN_VALUE;
	static String[] names = new String[9];



	public static void getName() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("data.txt"));

		for(int i = 0; i < names.length; i++)
	{
			names[i] = inFile.next() + " " + inFile.next();
			inFile.nextLine();

		}
	System.out.print(Arrays.toString(names));



    }

	public static  void getScore() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("diving_data.txt"));
		double[] scores = new double[72];

		Double total = 0.00;
		Double Sum1, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8, Sum9;
		  int index = 0;



			while(inFile.hasNext()) {
			    if(inFile.hasNextDouble()) {
				scores[index++] = inFile.nextDouble();
			    }
			    else {
				inFile.next();
			    }
			}

Sum1 = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
Sum2 = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
Sum3 = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
Sum4 = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
Sum5 = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
Sum6 = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
Sum7 = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
Sum8 = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
Sum9 = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

System.out.println(Sum1); // test

}
	public static void main(String[] args) throws FileNotFoundException
	{

		getScore();
		getName();


}

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
(2) As your Sum1, Sum2, are not array,
you need to print each one individually:
System.out.println(Sum1);
System.out.println(Sum2);
System.out.println(Sum3);
etc.  

Awarded 2011
Awarded 2011

Commented:
(1) it seems that you combined them reasonably

Please, post data.txt and driviong_data.txt, so that
I could test it in reality and see why you get zeroes.
Awarded 2011
Awarded 2011

Commented:
You should not mix up class Double and primitive type double
All the numbers which you manipuilate with (in particular
doing arithmetic operations) should be prinmirtive double
Class Double is used for special opertaions - like converting strings to
double, etc.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Awarded 2011
Awarded 2011

Commented:

I modified the last part of your code - made indeed array sum
and printed it as array.

Still to fix and test the program we'd like to have the input files

import java.io.*;
import java.util.*;
import java.lang.*;



public class Assignment10
{
	public static double lowestScore = Double.MAX_VALUE;
	public static double highestScore = Double.MIN_VALUE;
	static String[] names = new String[9];



	public static void getName() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("data.txt"));

		for(int i = 0; i < names.length; i++)
	{
			names[i] = inFile.next() + " " + inFile.next();
			inFile.nextLine();

		}
	System.out.print(Arrays.toString(names));



    }

	public static  void getScore() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("diving_data.txt"));
		double[] scores = new double[72];

		//Double total = 0.00;
	//	Double Sum1, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8, Sum9;
        double total = 0.0;
        double [] sum = new double[9]; 
        
		  int index = 0;



			while(inFile.hasNext()) {
			    if(inFile.hasNextDouble()) {
				scores[index++] = inFile.nextDouble();
			    }
			    else {
				inFile.next();
			    }
			}
        /*
Sum1 = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
Sum2 = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
Sum3 = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
Sum4 = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
Sum5 = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
Sum6 = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
Sum7 = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
Sum8 = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
Sum9 = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

System.out.println(Sum1); // test
        */
        
sum[0] = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
sum[1] = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
sum[2] = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
sum[3] = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
sum[4] = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
sum[5] = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
sum[6] = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
sum[7] = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
sum[8] = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];
        
        for(int j=0; j<9; j++){
            System.out.println("Sum [" + j + "]=" + sum[j]);
        }

//System.out.println(Sum1); // test        
        

}
	public static void main(String[] args) throws FileNotFoundException
	{

		getScore();
		getName();


}

}

Open in new window

Awarded 2011
Awarded 2011

Commented:

I modified your program, commented out some of your pieces,
and replaced it with different code,  also invented some input files.


Look at it and let me know if you have any questions.

import java.io.*;
import java.util.*;
import java.lang.*;



public class Assignment10
{
	public static double lowestScore = Double.MAX_VALUE;
	public static double highestScore = Double.MIN_VALUE;
	static String[] names = new String[9];



	public static void getName() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("data.txt"));

		for(int i = 0; i < names.length; i++)
	{
			names[i] = inFile.next() + " " + inFile.next();
			//inFile.nextLine();

		}
	System.out.print(Arrays.toString(names));



    }

	public static  void getScore() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("diving_data.txt"));
		double[] scores = new double[72];

		//Double total = 0.00;
	//	Double Sum1, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8, Sum9;
        double total = 0.0;
        double [] sum = new double[9];

		  int index = 0;



			//while(inFile.hasNext()) {
			    while(inFile.hasNextDouble()) {
                    if(index > 71)break;
				scores[index++] = inFile.nextDouble();
			    }
			   // else {
			//	inFile.next();
			  //  }
			//}
        /*
Sum1 = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
Sum2 = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
Sum3 = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
Sum4 = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
Sum5 = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
Sum6 = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
Sum7 = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
Sum8 = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
Sum9 = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

System.out.println(Sum1); // test
        */

sum[0] = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
sum[1] = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
sum[2] = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
sum[3] = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
sum[4] = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
sum[5] = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
sum[6] = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
sum[7] = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
sum[8] = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

        for(int j=0; j<9; j++){
            System.out.println("Sum [" + j + "]=" + sum[j]);
        }

//System.out.println(Sum1); // test
        

}
	public static void main(String[] args) throws FileNotFoundException
	{

		getScore();
		getName();


}

}

Open in new window


diving_data.txt:
1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74

Open in new window


data.txt:
item_1 item_2
item_3 item_4
item_5 item_6
item_7 item_8
item_9 item_10
item_11 item_12
item_13 item_14
item_15 item_16
item_17 item_18
item_19 item_20

Open in new window


Output of the above program:

Sum [0]=29.0
Sum [1]=73.0
Sum [2]=125.0
Sum [3]=168.0
Sum [4]=217.0
Sum [5]=263.0
Sum [6]=317.0
Sum [7]=362.0
Sum [8]=417.0
[item_1 item_2, item_3 item_4, item_5 item_6, item_7 item_8, item_9 item_10, item_11 item_12, item_13 item_14, item_15 item_16, item_17 item_18]

Open in new window

Author

Commented:
I left out the most important piece of information, here is the data file that is associated with the code.
The purpose of my code was to extract the names and store into array, and also extract the scores and put them into their own arrays.  The text file should be self explanatory.
diving-data.txt
Awarded 2011
Awarded 2011

Commented:
You use two files in the program data.txt and diving-data.txt
This is just one.

Author

Commented:
Wasn't aware the code had this particular typo in the inFile location. when I posted.  The program runs off one file.
Awarded 2011
Awarded 2011
Commented:

You don't need the score() method to run this
progra, I modified it so that it reads everything and calcultes in the first
method getName()
Try to run it.

import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.lang.*;



public class Assignment10
{
	public static double lowestScore = Double.MAX_VALUE;
	public static double highestScore = Double.MIN_VALUE;
	static String[] names = new String[9];



	public static void getName() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("diving-data.txt"));

		for(int i = 0; i < names.length; i++)
	{
        String buff = inFile.nextLine();
        if(buff.trim().length() == 0)continue;
        StringTokenizer t = new StringTokenizer(buff);
        names[i] = t.nextToken() + " " + t.nextToken();
        double sum = 0.0;
        while(t.hasMoreTokens()){
            sum += Double.parseDouble(t.nextToken());

        }
        DecimalFormat formatter = new DecimalFormat("###.#");
        System.out.println(names[i] + " Sum: " + formatter.format(sum));

			//names[i] = inFile.next() + " " + inFile.next();
			//inFile.nextLine();

		}
	//System.out.print(Arrays.toString(names));



    }

	public static  void getScore() throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("diving_data.txt"));
		double[] scores = new double[72];

		//Double total = 0.00;
	//	Double Sum1, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8, Sum9;
        double total = 0.0;
        double [] sum = new double[9];

		  int index = 0;



			//while(inFile.hasNext()) {
			    while(inFile.hasNextDouble()) {
                    if(index > 71)break;
				scores[index++] = inFile.nextDouble();
			    }
			   // else {
			//	inFile.next();
			  //  }
			//}
        /*
Sum1 = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
Sum2 = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
Sum3 = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
Sum4 = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
Sum5 = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
Sum6 = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
Sum7 = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
Sum8 = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
Sum9 = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

System.out.println(Sum1); // test
        */

sum[0] = scores[0] + scores[1] + scores[4] + scores[5] + scores[6] + scores[7];
sum[1] = scores[8] + scores[9] + scores[11] + scores[12] + scores[13] + scores[14];
sum[2] = scores[16] + scores[17] + scores[20] + scores[21] + scores[22] + scores[23];
sum[3] = scores[24] + scores[25] + scores[26] + scores[28] + scores[29] + scores[30];
sum[4] = scores[32] + scores[33] + scores[35] + scores[36] + scores[37] + scores[38];
sum[5] = scores[40] + scores[41] + scores[42] + scores[43] + scores[45] + scores[46];
sum[6] = scores[48] + scores[49] + scores[52] + scores[53] + scores[54] + scores[55];
sum[7] = scores[56] + scores[58] + scores[59] + scores[60] + scores[61] + scores[62];
sum[8] = scores[66] + scores[67] + scores[68] + scores[69] + scores[70] + scores[71];

        for(int j=0; j<9; j++){
            System.out.println("Sum [" + j + "]=" + sum[j]);
        }

//System.out.println(Sum1); // test
        

}
	public static void main(String[] args) throws FileNotFoundException
	{

		//getScore();
		getName();


}

}

Open in new window




Chen Ruolin 	9.2	9.3	9	9.9	9.5	9.5	9.6	9.8	   
Emilie Heymans	9.2	9.2	9	9.9	9.5	9.5	9.7	9.6	   
Wang Xin		9.2	9.2	9.1	9.9	9.5	9.6	9.4	9.8	   
Paola Espinosa	9.2	9.3	9.2	9	9.5	9.3	9.6	9.8	   
Tatiana Ortiz	9.2	9.3	9	9.4	9.1	9.5	9.6	9.8	   
Melissa Wu		9.2	9.3	9.3	9.7	9.2	9.2	9.6	9.8	   
Marie-Eve Marleau	9.2	9.2	9.2	9.9	9.5	9.2	9.3	9.8	   
Tonia Couch		9.2	9	9.1	9.5	9.2	9.3	9.4	9.6	   
Laura Wilkinson	9.7	9.1	9.3	9.4	9.5	9.4	9.6	9.2	 

Open in new window


Chen Ruolin Sum: 75.8
Emilie Heymans Sum: 75.6
Wang Xin Sum: 75.7
Paola Espinosa Sum: 74.9
Tatiana Ortiz Sum: 74.9
Melissa Wu Sum: 75.3
Marie-Eve Marleau Sum: 75.3
Tonia Couch Sum: 74.3
Laura Wilkinson Sum: 75.2

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial