Printing Each Value In Multidimensional Array Using For Loop
Dec 5, 2014
I want to print each value in the multi-dimensional array using a for loop but there is a problem.
Here is the script:
Java Code:
public class LearningJava
{
public static void main(String[] args) {
int[][] Grid = {
{12, 24, 36, 48, 60},
{1, 3, 5},
[Code] ....
Printing i: 0, x: 12
Printing i: 0, x: 24
Exception in thread "main" Printing i: 0, x: 36
Printing i: 0, x: 48
Printing i: 0, x: 60
java.lang.ArrayIndexOutOfBoundsException: 5
at LearningJava.main(LearningJava.java:11)
It's suppose to get the length of each array and print all the values in that array and then move to the next one.
I tried adding .length
Java Code: for(int x = 0; x < Grid[i][x].length; x++) mh_sh_highlight_all('java');
but then it doesn't work at all.
View Replies
ADVERTISEMENT
Nov 21, 2014
what would the return be for adding two array's together,
public static int sum (int[][] a, int[][] b) {
int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}
My return is wrong...... I know, Any hints?
View Replies
View Related
May 5, 2015
At the bottom of this code snippet you will see the print statement. I am trying to divide the multidimensional array by its sum. Can this be done simply in the System.out statement, or do I need a separate method?
private static void Histogram() throws IOException {
int[][][] ch = new int[4][4][4]; /* 4 for 64 and 8 bins, 8 for 512 bins */
BufferedImage image = ImageIO.read(new File("airplane_training1.jpg"));
for(int x = 0; x < image.getWidth(); x++)
for(int y = 0; y < image.getHeight(); y++) {
[Code] .....
View Replies
View Related
Mar 13, 2015
import java.util.Scanner;
public class ColumnSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;
int [][] matrix = {{5, 9, 87, 74, 12, 7}, // row 1
[code]...
Right now my code gets all the numbers in a row and adds those up, but I want it to get the numbers in a column and add them instead. The problem is I don't know how to get the userpick (the number that the user picks to determine which column gets added) to be set to that particular column.
View Replies
View Related
Jan 14, 2014
I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:
int[][] multiArr = new int[2][];
the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!
View Replies
View Related
Feb 13, 2014
I tried this:
String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};
I also tried this:
String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};
but I get an error
View Replies
View Related
Mar 2, 2015
public class StuTest {
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
int[][] testScores; // [students][tests]
String[] stuNames;
[Code] ....
The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.
View Replies
View Related
Feb 2, 2015
I've found different examples on line, but none that use the split method with a multidimensional array. I would like a user to input coordinates (x,y) for city locations and store them into a 2-D array. For the user to input the x,y-coordinates on one line I need to use split(',') and then parse the string array to a double which will then be used to calculate the distances from one another.
My issue is how to store the String vales into the 2-D array. I do not want to store the x-value at even (cityArray[0]) and y-value at odd (cityArray[1]) 1-D locations.
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.I have to read the picture as a binary matrix of the size r c (number of rows times number of columns).The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
-Each element with value 1 is a part of some block
-Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space. Then we have the r lines, where each contains s 0's and 1's.The numbers inside the individual lines are NOT separated by spaces.The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
[code]...
View Replies
View Related
Apr 8, 2014
how you can do array.length, I have an array with [][][][], and what do I do to find the length of one section?
So let's say
Java Code: int array[][] = {
{5, 5, 5, 5},
{4, 4, 4, 4},
{3, 3, 3, 3}
} mh_sh_highlight_all('java');
How do I find out the length of the {5, 5, 5, 5} which is 4. Because there are 4 place-holders.Is there a way?
View Replies
View Related
Feb 18, 2014
I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).
I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?
View Replies
View Related
Sep 10, 2014
I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimensional array. nothing graphical just plain co ordinates.
View Replies
View Related
Mar 22, 2015
import java.util.*;
import java.text.*;
public class Linearize {
public static void main(String[] args) {
// Create new Scanner and Random and Decimal objects
Scanner s = new Scanner (System.in);
Random g = new Random ();
DecimalFormat oneplaces = new DecimalFormat (".00");
[code]....
I am really close to finishing this program and my output is almost there, except it's only printing out the first half or so of the array. I have two for loops and two counters to determine the size of the array and then copy the multidimensional array's values into the 1D array, but it only prints out the first half of the array as such:
How many rows in the array? 4
How many columns in the array? 2
This 2D array contains:
35.23, 26.94,
99.48, 66.69,
7.31, 25.18,
64.53, 21.25,
Converted to a 1D array:
35.23, 26.94, 99.48, 66.69,
And yes, I realize that I should be formatting my Print statements with % but my instructor doesn't seem to care about it (he never taught it to us) so for the time being I am being stubborn and using .
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
Apr 7, 2014
I have been asked to write a library program that will keep record of books and the year it was published. The program should ask the user how many rows he wants accept the string input from the user and display them in rows and columns. This is how i code it
package multidimension;
import java.util.Scanner;
public class bookrecords {
public static void main(String[]args){
//declaring a scanner variable
Scanner input =new Scanner(System.in);
[code]....
View Replies
View Related
Apr 19, 2013
I just started in java programming and into Arrays multidimensional. I started a simple 2 dimensional array with 5 names with genders. I 'm having issues because it does not want to print out the names with correct genders Male/ Female. The simple program should print out the 5 names with gender type.
example:
Jack - Male
Sally - Female
Dave - Male
Sue - Female
Brian - Male
Here is what I came up with so far:
public class Multiname
{
public static void main(String[] args)
{
//String[][] multiname;
String [][] multiname =
[Code] .....
This is the result:
multiname : Jack
multiname : sally
multiname : Dave
multiname : Brian
multiname : Sue
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MultiD.main(MultiD.java:29)
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.
I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.
The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();
[Code] ....
View Replies
View Related
Feb 13, 2015
I am writing a program that has to do with ciphers and cipher shifts. The program itself works fine, but I am having trouble printing out the answers to JOptionPane. I know the basics of JOptionPane, but my problem is this:
Majority of my program takes place in a for loop, and resolves the cipher (it is a basic cipher program) 1 digit at a time. So, only the last DIGIT (I don't know how to convert a digit to a CHAR in JOptionPane) is printed to JOptionPane. Here is my code:
public static void main(String[] args) {
String cipher = JOptionPane.showInputDialog(null,
"Please enter a sentence or word that you wish to encode or decode. This program uses " + " a basic cipher shift.");
int answer = 0;
String upperCase = cipher.toUpperCase();
char[] cipherArray = cipher.toCharArray();
[Code] .......
View Replies
View Related
Feb 13, 2014
I don't know how to write a program based on for loop like pattern printing.etc
View Replies
View Related
Jan 19, 2015
I built a java code to print the following pattern using nested loop:
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
And here is my code
public static void main(String[] args) {
for(int i=1; i<=8;i++)
{
for(int k=1;k<=i;k++)
{
System.out.print(" ");
[Code] .....
which gives the partially true output:
123456781
123456721
123456321
123454321
123454321
123654321
127654321
187654321
I couldn't figure out how to place the spaces.I have made some changes on the code, but it didn't work.
View Replies
View Related
Jan 13, 2015
I was trying to read an article from here : [URL] ..... I was doing the same example but loop is not ending. Thread is struck at CalculateThreadDemo run method. If i mentioned System.out.println then it got terminated else infinite loop continues. I don't understand this.
code:
BlockedThreadDemo
public class BlockedThreadDemo extends Thread {
private boolean isFinished = false;
BlockedThreadDemo(String name) {
super(name);
[Code] ....
Without commenting the statement:
597222
597223
597224
597225
isFinished: true
main thread is dead
597226
CalculateThread is finished.
i: = 49i: = 13i: = 10
BlockedThreadDemo is finished.
View Replies
View Related
Jul 2, 2014
Ask user to enter a name alphabet by alphabet, and print d whole word as output,,,,,, use while loop?Since i am new to JAVA..i have no clue on how to begin?
View Replies
View Related
May 6, 2015
I'm supposed to add characters to a stack and pop them once the adjacent delimiter is read in from a text file. Moreover, program is supposed to print out the incoming text from the file, but stop when the applicable delimiter is not on top of the stack. As in, a '[' doesn't have a ']'.
I've got the program so it can pop and add to the stack correctly, and stops adding at the correct time, but I cant seem to get it to stop printing. I know a switch statement method in another class seems obvious, but I was trying to practice nested loops.
This is the main program:
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.util.Stack;
public class DelimiterChecker {
public static void main(String [] args) throws IOException {
[Code] ......
Moreover, the file I'm reading for is simply:
{
System.out.println(1)
System.out.println(2)
System.out.println(6 <-------------missing delimiter
System.out.println(7)
}
View Replies
View Related
Nov 13, 2014
My issue is that when I run my search, it does find a goal. However, when I try and print the route using my print route method, it just gets stuck in a loop of printing the same two nodes. What is wrong with My A* implementation?
package search;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
[code]....
View Replies
View Related
Apr 13, 2014
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Hours Traveled ");
int hoursTraveled = input.nextInt();
System.out.println("Enter MPH ");
int mph = input.nextInt();
[Code] .....
View Replies
View Related
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related