Sdot718
asked on
Printing from various methods.
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).
(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();
}
}
(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.
Please, post data.txt and driviong_data.txt, so that
I could test it in reality and see why you get zeroes.
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.
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.
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();
}
}
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();
}
}
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
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
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]
ASKER
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
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
You use two files in the program data.txt and diving-data.txt
This is just one.
This is just one.
ASKER
Wasn't aware the code had this particular typo in the inFile location. when I posted. The program runs off one file.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
you need to print each one individually:
System.out.println(Sum1);
System.out.println(Sum2);
System.out.println(Sum3);
etc.