Reputation: 121
This is very similar to this post except that the code is now part of an object instead of a method.
Again, this is how it should look:
Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9 10 11.2 12.5You entered:
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.2 12.5The sums are:
15.0 18.0 21.2 24.5
This is my code:
else if (choice == 2) {
// Prompt user
System.out.println("You Selected Sum Columns");
// Enter array values
double[][] myList = new double[3][4];
double[][] userList = new double[3][4];
feature2 f2 = new feature2();
f2.run(myList, input);
f2.getInput(userList);
}
and
import java.util.Scanner;
public class feature2 {
public void run(double[][] myList,Scanner input) {
// 2D arrays are like spreadsheets ROW and Columns
System.out.println("Enter " + myList.length + " rows and " + myList[0].length + " columns: ");
for (int i = 0; i < myList.length; i++){
for (int j = 0; j < myList[0].length; j++){
myList[i][j] = input.nextDouble();
}
}
}
public void getInput(double[][] userList) {
System.out.println("You Entered:");
for (int i = 0; i < userList.length; i++) {
for (int j = 0; j < userList[0].length; j++) {
// chars are just ints!
System.out.print(userList[i][j] + " ");
}
// every row
System.out.print("\n");
}
System.out.println("\n The sums are:");
for (int column = 0; column < userList[0].length; column++) {
double total = 0.0;
for (int row = 0; row < userList.length; row++){
// sum column
total += userList[row][column];
}
System.out.print(total + " ");
}
// space for formating
System.out.print("\n");
}
}
This is what happens instead:
Enter 1 Random 2D Chars
Enter 2 Sum Columns
Enter 3 Identical Arrays
Any other input will exit2
You Selected Sum Columns
Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9.1 10.2 11.3 12.9
You Entered:
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
The sums are:
0.0 0.0 0.0 0.0
Now I have the opposite problem of the last post. Everything formats correctly, but it doesn't calculate properly. This is my only problem with the entire code.
Upvotes: 1
Views: 39
Reputation: 691635
You're asking the user to fill an array, and then displaying that array. So you need only one array, right?
Yet you have two arrays in your code: myList
, which is filled by the user, and userList
, which is displayed.
double[][] myList = new double[3][4];
double[][] userList = new double[3][4];
feature2 f2 = new feature2();
// ask the user to fill myList
f2.run(myList, input);
// but display userList
f2.getInput(userList);
Note that your naming is very confusing. The method which gets the input from the user is named run()
, and the user which displays the array is named... getInput()
.
Upvotes: 2
Reputation: 2823
Well, you are first instantiating 2 empty arrays, then writing to one array and finally reading from second one (which is empty - actually filled with default double values, which is 0.0
).
Basically, instead of:
double[][] myList = new double[3][4];
double[][] userList = new double[3][4];
feature2 f2 = new feature2();
f2.run(myList, input);
f2.getInput(userList);
try with:
double[][] myList = new double[3][4];
feature2 f2 = new feature2();
f2.run(myList, input);
f2.getInput(myList);
Upvotes: 2