Summing Columns And Displaying Array
Apr 24, 2015
public static void displayOutputs (int[][] anArray) {
System.out.println("
Here is your 2Dim array:");
for (int i = 0; i < anArray.length; i++) {
System.out.println(Arrays.deepToString(anArray));
}
}
This is my code and I get this as a result when I input 10,20,30,...,90 into array[0][0], array[0][1], ..., array[2][2] (3rows, 3columns array)
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
While I expect to get only
[10, 20, 30]
[40, 50, 60]
[70, 80, 90]
I get no errors at least when I have the same # of rows and # of columns. However, when I have more columns than rows, a compiler stops running when it runs columnSum method.Here's an error message.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at lab10.Array2DMethods.columnSum(Arrays2DDemo.java:70)
at lab10.Arrays2DDemo.main(Arrays2DDemo.java:111)
Java Result: 1
And here's my code
public static void columnSum (int[][] anArray) {
int sum =0;
for (int col=0; col < anArray.length; col++) {
sum=0;
for (int r = 0; r < anArray[col].length; r++ ){
sum += anArray[r][col];
}
System.out.println("Sum of column " + col + " = " + sum);
}
}
why my code doesn't work when I have more columns.
View Replies
ADVERTISEMENT
Mar 23, 2015
My program is supposed to use recursion and iteration to sum and reverse elements in an array of Longs. The array is supposed to be 1000000 cells and I am supposed to compute the average times of 99999 trials. When I try to run the program, it keeps saying running.... but doesnt ever do anything.
public class PA2Delegate {
private long[] start;
private long[] end;
private Long reversal;
private Long sum = (long)(int)0;
[Code] .....
View Replies
View Related
Feb 2, 2015
I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.
This is what I have so far:
public static void main(String[args]){
int size = kbd.nextInt();
int [] myArray = new int [size]
//user inputs the elements of array
[Code]....
View Replies
View Related
Dec 2, 2005
I am very new to Java Swing. I have to create a TreeTable in Java Swing with a Parent Row having say 6 columns and its all child row having just 4 columns. like shown below
Parent row:
-Column1-+-Column2-+-Column3-+-Column4-+-Column5-+-Column6-+
Child row:
--CloumnC1--+--CloumnC2--+--CloumnC3--+--CloumnC4--+
Can this be achieved using JSwing ?Also, Can I be able to Change the Column Headers Correspondingly when user clicks on Parent row and Child rows?
View Replies
View Related
Nov 6, 2014
How can I find the overall averages of the columns in this array?when i run my code it gives me an output that looks like this (showing me wrong averages): 0 1 2 3 overall grade
Badger,Bradley 37 70 51 48 68.66666666666667
Emu,Emma 35 81 75 40 72.83333333333334
Aardvark,Alice 42 85 22 0 65.11111111111111
Dodo,Donald exc 12 25 0 54.17391304347826
Cassowary,Cassie 50 97 72 68 62.758620689655174
my code is :
Java Code: public static double[] computeAllGrades(int[][] scoreTable, int[] itemPointValues)
{
{
double totalPoints = 0;
double totalPointsPossible = 0;
double allGrades[];
double grade =0;
[code]....
View Replies
View Related
Apr 14, 2015
how to switch the elements in a 2D array. As an example row 1 would become column 1 in a second array.
View Replies
View Related
Aug 20, 2014
I am trying to write a code for multidimensional array, allocate memory for variables and access the value of the arrays. I want them to be shown as rows and columns. but my code only shows one column and no rows. here is my code:
public static void main(String[] args) {
int[ ][ ] aryNumbers = new int[2][2];
aryNumbers [0][0] = 1;
aryNumbers [0][1] = 2;
aryNumbers [1][0] = 3;
aryNumbers [1][1] = 4;
int rows = 2;
int columns = 2;
[code]....
View Replies
View Related
Nov 21, 2014
I wrote this code which print a black image depends on the number of rows and columns you give it
public class BlackImg {
private Mycolor[][] colorArr; //Mycolor is a class i wrote that represents colors.
// no need for showing the class here.
// so i created here an array of type Mycolor, for example:
// { {(255,255,255), {(127,127,0)} }
[Code] .....
my problem is that my output comes good except the last line ,
Output:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
BlackImg@1db9742 //what is this line , why does it showing up ?
View Replies
View Related
Feb 10, 2015
I have a problem with one method for my assignment. This is the requirement for this method: "This method uses a loop to list all accounts contained in the Array, adding each account details to a String, before outputting to screen in the format specified in the screenshot below. Ensure that there are no out of bounds exceptions by checking if each array slot has an account object before adding its details to the output String. (arrayname[index] != null)"This is my code for this method:
public void listAllAccounts() {
String allAccountsString = "List of all accounts:
";
for(int i = 0; i < ACCOUNT_SPACES; i++) {
//allAccountsString += accountArray[numAccounts];
if (accountArray[i] !=null) {
allAccountsString += accountArray[i].toString() + "
" ;
}
}
JOptionPane.showMessageDialog(null, allAccountsString);
The problem is that the Message Dialog does not display the accounts I have already created. It just displays "List of all accounts:
";
View Replies
View Related
Mar 27, 2015
When i am trying to display the contents of this array its showing arrayindexoutofbounds exception...how do i display this array?
<%
String names[][] = new String[10][2];
for(int i=0;i>=10;i++){
for(int j=0;j<=i;j++){
names[i][j]="anu";
if(j>0){
[Code] .....
View Replies
View Related
Feb 9, 2014
My project was to create an array holding 10 integers and populate the array with 10 random numbers. Then ask the user to guess the number. Prompt the user with a while loop if their input is out of range. Determine if the users number is in the array, and display which index location the number is in. I got most of the code done but am having trouble displaying the index location.
import javax.swing.*;
public class Homework4 {
public static void main(String[] args) {
int[] numarray = new int [10];
char repeatcode = 'y';
[code]....
View Replies
View Related
Apr 30, 2015
What I'm trying to do is display an entire deck of cards in 4 hands, randomly shuffled. These will be displayed as images in a pane. The images are numbered 1-52(not using Jokers) so what I have done is created an array that contains those numbers. Then I am randomly shuffling that array. What I can't figure out how to do is display the images using the randomly shuffled numbers in the array to do it? Is this even possible or am I totally on the wrong track on how to do this? This seemed like the obvious way to go when I started. Showing images isn't a problem, it's showing them using the array's element to get the image name that I'm having trouble figuring out.
View Replies
View Related
Aug 7, 2014
I have this small program that creates a sales opportunity and its related products, and then prints out the opportunity name, total quantity and total price
class Opportunity{
String name;
int Amount;
String Stage;
int Product_quantity;
[Code] .....
The code works as expected, and is successfully prints :
Opportunity Name: Sales
Opportunity Amount: 110
Opportunity Product Quantity: 3
That said, I feel the way the OpportunityCreator class sums up the quantities and prices (see the comments on the code) is very inefficient, and would only work if there were 3 or less products. How can I design this so that it sums the quantities and prices of all the Product objects that I create, regardless of how many of them are? I understand probably I'd need to redesign the whole thing but I'd like to get at least an idea of how this can be achieved.
Originally I was thinking of creating an ArrayList and adding all the prices/quantities there but wasn't sure how to do. I also thought of using a loop to loop through the different object references and sum the quantity/price but was also unsure on how to do so?
View Replies
View Related
Aug 29, 2014
I am brand new to programming and java. I decided I wanted to learn programming so now I'm in a class. I am into my second week and my assignment is to generate 100, 3 digit numbers then add them together. I understand how vast java is, but as of right now we have only studied if/else/switch statements, I was able to get the 100, 3 digit numbers generated, but I cannot add then together.
import java.util.Random;
import java.util.Math;
public class ThreeDigitGenerator {
public static void main(String[] args) {
int sum = 0;
[Code] ....
View Replies
View Related
Feb 8, 2015
"Write a program that takes a word and displays the sum of the numbers comprising its character's Unicode values. "Here is my code:
/**
* Description: Prints the word "Sunny" as well as printing the
*/
public class Assign1{
public static void main(String[] args){
String sun=new String("Sunny");
System.out.println(sun); // Prints Sunny
//sets values as strings
[code]....
View Replies
View Related
Sep 13, 2014
I'm working a an assignment for my IT/215 class and I'm having a little trouble with my arrays. My code compiles just fine but when it comes to displaying the information I've input into my array it doesn't seem to want to and I'm not sure what I'm doing wrong here. On line 136 originally I had [DVDs [count] = count+1]] but it wouldn't compile. I then put line 136 to [ DVDs[count] = new Inventory();] and I think that's were my issue is but I'm not sure....
// Purpose:the purpose of this software is to display inputs as wells as the stocks and price of inputs.
class Inventory
{
private double ItemNumber; //to store Item number
private String ItemName; //to store Item name
private double UnitsInStock; //to store number of units in stock
private double UnitPrice; //to store Unit price
private double InventoryValue; //to store Inventory value
private double TotalInventory; //to store Total Inventory value
[Code] .....
View Replies
View Related
Oct 21, 2014
I couldn't where the problem with this code. The question is : (Write a program that reads in nine integers and sorts the value in the ascending order. Next, the program restores the sorted integers in a 3 x 3 two-dimensional array and display the integers as shown in the result.) And this how i did it:
package test1;
import java.io.*;
import java.util.*;
public class test1{
public static void main(String[] args){
int[] hold = new int [9];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 9 integers, press <Enter> after each");
{
for (int i = 0; i < hold.length; i++);
[Code] ....
View Replies
View Related
Nov 11, 2014
I want to use JTable in which the user can enter the obtained marks of students and automatically add (total) the obtained marks in the total column. The structure of the table is given below.
S.No Name Test1 Test2 Total
View Replies
View Related
Feb 7, 2015
I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.
=Java
import java.util.Random;
public class Task6
{
public static void main(String[] args)
{
int[] numbers = new int[7]; //create array of numbers
Random random = new Random();
boolean displayed = false; //boolean value to determine if number was displayed
[code]....
View Replies
View Related
Apr 5, 2014
I have written some error checking code
File name ErrorPage.jsp
<%@ page language="java" isErrorPage="true" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error</title>
</head>
[code]...
I have put error.jsp and badpage.jsp file in public access folder that is web content in eclipsewhen I am running the code I am status code of 500 and not the errorpage.jsp message .
View Replies
View Related
Mar 1, 2014
Any way i can format this output into columns?
for(int i = 0; i < 5; i++) {
System.out.println(t[i]+n[i]+s[i]);
}
View Replies
View Related
Mar 13, 2015
I'm using opencsv and I want to write two arrays into a csv file and each array should be in a column. For example:
Column1 Column2
2423542 2332332
5242324 4343434
4242352 7565656
4343434 6565656
public class Writer {
[Code] ....
With my code the arrays are all in one column. What do I have to change that my output will be in two columns?
View Replies
View Related
Feb 5, 2014
I have a JScrollPane with two coulmns. In the first column I have an image pane JTable, and in the second a list with names of sections. This second column I try to divide in two columns, one (the second column) to display the names of the sections (each row contains one name), and in the other column (the third) I want to show some values for every section in the row respectively. But, instead of displaying the desired values in the third column, I get the same names of the sections as in the second column. Here is a part of the code I have:
private Vector<Section>daten = new Vector<Section>(0); //These are the values for the first column in the Jscroll
private String[] header = {"Section","calcGYR"}; // These are the values for the second and third column (in this case the header for the both columns
public TrafficObserveModel(Vector<Section> daten) {
setData(daten);
[code]....
But I don't know how to modify the methods in order to render the desired integer values in the third column.
View Replies
View Related
Apr 16, 2015
I have a 5x5 array of which I read from an external file and I am supposed to print out the original matrix and then add up each row and column, proceeding to store them into the sixth positions in my matrix. After that I print out the new matrix.
For example this just using a 2 by 2,
Original
1 2 3 4 5
1 2 3 4 5
New 3 by 3
1 2 3 4 5 15
1 2 3 4 5 15
2 4 6 8 10 30
I am confused as to how to isolate rows and print them out. I know how to add the entire matrix up but isolation is my issue.
Here is what I have including how to read the matrix and the sum of the whole thing
import java.io.*;
import java.util.*;
public class prog470cAddingRandC {
public static void main (String [] args) {
//read the file
Scanner inFile=null;
[code].....
View Replies
View Related
Apr 21, 2014
I need to write a class,that will give me output like this:
*
***
****
***
*
I have to use for loop,i know that i have to use nested for loops, for rows and columns. I just cant figure it out the thing with spaces,and how to turn it to count back.
View Replies
View Related
Mar 12, 2014
I am making rows and columns in the form of a multiplication table, listed below is my code:
package assignments;
public class MultTable {
public static void main (String [] args) {
int row, column, x, y;
for(row = 0; row < 8; row++)
[Code] .....
If you see my sample run you can see that I have the multiplication table down but, I haven't completed it. I have to make the bottom left half of the whole table blank somehow. For example, I have to make it halfway through the middle of the table the bottom left half full of white space...
5 6 7 8 9
12 14 16 18
21 24 27
32 36
90
hm, it's supposed to be the other way around horizontally.
View Replies
View Related