Hi, been reading up head first java. there's a bit about creating a battleship game and i just jumped ahead and tried doing it myself. so far i managed to create a grid which populates ships denoted by 'x' in 3 cells. i'm having trouble getting the checks right if it tries to populate it in the same cells. here is ... |
import java.io.*; import java.util.*; //Print all the elements below the leading diagonal as zeros(The leading diagnol is where all the row index equal the column index. class Printsqu{ public static void main(String[] args){ int [][]array = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; for(int i = 0; i < array.length; i++){ for(int j = 0 ; j |
|
4. 2D array go4expert.comHello, Could someone help me out, I am very new to programming( as one could tell by the Question I am about to ask.) I am trying to assign a value to random spots in a 2D array. This is what I have so far: Code: /** * Generates random coordinates, puts them into a new CoordinatePair, * returns CoordinatePair - ... |
import java.awt.*; import java.applet.*; import java.awt.event.* public class TwoDDrray extends Applet implements MouseListener{ int[] lmb[10,10]; int d1 = 0 int d2 = 0 public TwoDDrray() { addMouseListener ( this ) ; setBackground (java.awt.Color.blue ) ; } public void mouseClicked( MouseEvent ev ) { do{ if (d2<9) d2++; eles ( set d2=0 d1++; ) }while (counter < 9); } if (d1<9) { ... |
Displaying and calculating 2D arrays I need some help with a Netbeans GUI with 3 buttons and 1 text area. First button will collect data from text field a and b and this is stored in a 2D array. The second button will process 3 caluclations from the array data. The third button works fine. The text area will display the ... |
Read the attached file. 1.start with X in array[0][0] 2.Move to Right if end is reach and still Right, X appears on left side. 3.Move to Left if end is reach and still Left, X appears on Right side. 4.Move to Up if end is reach and still Up, X appears from bottom. 5.Move to Down if end is reach and ... |
|
From 1,2.3,4 5,6,7,8 9,10,11,12 13,14,15,16 To 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 Is it more easy to use 1D array than 2D array to transpose? I know that i use for loop as below it will display every row from 0 to 3, but how to put in the expression in ... |
I have a function that draws a stack of rounded rectangles ("disks") in a particular column. As the stack increases the disk size decreases. I would like to be able to move these shapes between different columns. This is done buy the user clicking on a "disk" then clicking on a destination column. (I have already written a method that will ... |
|
Hello everyone. I'm familiar with Java's syntax, but I've only been using it for console applications. I would like to start some graphics programming, although, I would like to do everything on my own with pixel control. If possible, please help me generate some code that does the following: - Creates an empty window with certain dimension. That dimension should be ... |
Hi all. I have an table like [][]tablo and it includes 3 value x,y,z . and there is about 600 lines . How can I draw a table from this values? I saw on that on java has 2d graph with x,y but how can i draw 3d? x,y,z ? 0.0000 0.0000 55.0000 208.9600 -99.9630 55.0000 208.9600 -99.9630 14.0520 208.9600 -100.3960 ... |
Hi, I am new to java programming, but i have this problem: I open an image, i put ir in array 2 dimensional, teh idea is like this array[count][0]=x; array[count][1]=y array[count][2]=red; array[count][3]=green; array[count][4]=blue; array[count][5]=alpha; I do so becaude i need to work with colors, but later on i want to save all changes in image. Can you please help me?! Some ... |
I wanted to draw a graph(just points) reading from array. the numbers of array have to be doubles because of accuracy. In the main class, I attached two files and saved into 2D arrays and draw graph in another class. the x axis will be CorrectData[i][0] and y axis is CorrectData[i][1]. Can someone tell me how to read from array and ... |
|
I need some help with a Netbeans GUI with 3 buttons and 1 text area. First button will collect data from text field a and b and this is stored in a 2D array. The second button will process 3 caluclations from the array data. The third button works fine. The text area will display the data in the array when ... |
The program below I made draws a field with trees randomly placed. The trees are triangles drawn by lines, and the stumps are filled rectangles drawn. I want to make the triangles drawn by the lines, filled too. I think the best way is fillPolygon but i cant create an array of polygons. Can someone help me fill my trees? import ... |
Hey, im having some problems with making a 2d canvas. I have made a canvas that adds images to it when i click. I want to store the x and y coordinates of each image i add into an array when they are added to the canvas. The problem i am having is that if i resize the window and left ... |
Is there a way to Stop repeating random numbers in a 2d array? I have a 2d array 5x3 and I need to prevent a random number from repeating in the middle column which is array[r][1] Here's what I have... public static void main(String[] args) { int MyArray[][] = new int [5][3]; fillArray(MyArray); OutputArray(MyArray); //method to display array }//end of main ... |
I need to plot multiple lines on one graph axis in java, the data for each line is stored in an Integer Array. I found a useful example in a thread from a couple of years ago, code posted by 'hardwire'. This was the code- Code: Java Code: import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class GraphingData extends JPanel ... |
This code: Java Code: for (int row = 0; row < processes.length; row++) { for (int column = 0; column < processes[row].length; column++) { // if //System.out.print(processes[column] + " "); } } is almost there. First, consider the first row of the array to be filled with constants, and the other two rows will tell you how to act on these ... |
2d array help- Game of Life so I'm having problems, the boards completely erases on generation 2 and I have no clue as to why. Java Code: import java.util.Scanner; import java.util.Random; public class Life { // the size of the grid (GRIDSIZE x GRIDSIZE) final private static int GRIDSIZE = 18; /********************************************************************************/ public static void main ( String args[] ... |
Learning - 2D array and method question. Hi Im new to Java and enjoying it so far. I have come to my first complex bit of code and am having some trouble understanding how methods work within a class. Below is my code thus far (and detailed description of what each method is to do), it is to based ... |
it compiles and runs but when i put in say the position 12, 9 it sees that there is nothing there so it returns a line length of 0, that works. but if there is say a B (black) toke at 12, 9 the method maxLength is supposed to look in each vertical, horizontal and both diagonals to find out of ... |
:confused: I had a 1 dimensional array using the array class() to search a list of strings which are numbers, everything works. Java Code: System.out.println("After Search"); String[] nums={"21.675", "24.798", "16.567", "25.014", "32.907", "31.456", "41.876", "22.123", "20.945","20.679", "18.398", "17.075", "40.025", "26.564", "30.712", "29.897", "23.397", "35.548", "22.123", "31.398" }; Arrays.sort(mbrnum); txtResult.setText(mbrnum[0][1]); // sends 1st (lowest) number to textfield // send number to textfield ... |
Hi all, i was hoping one of you guys could help me out. I have a class which contains an 2D array called raster[10][16] Each "postition in the array contains either a 1 or a -1 now i want to make a for loop in my paintComponent so that when the loop reads a 1 it creates a certain drawing and ... |
Hello, I'm making a Pacman java program. For the maze i'm using a 2d array. There are different kinds of integers in the 2D array. -1 for the wall 0 for nothinh 1 for a pill and a number for a bonus now I want to draw all those different things, like the wall and the pills. Only I dont know ... |
good day, i am a rookie when it comes to java and im just basicaly messing around for fun. Im trying to make a program where you have 4 teams and using 2D array you enter there wins and losses, it calculates the win pct and sorts them in win pct order. just like the NFL standings. my code so far ... |
can any1 help me solve my assignment?please i only studied java for a month and the lecture throw this kinda question to us i really got no idea how to do A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given a six cards in the deck, ... |
You may help us help you by explaining just what you are trying to achieve in non-programming terms. Why do this? What's the goal? This article may help you to get help here: smart questions For instance, what is going to be held by this array? What purpose does the class that holds it have? What are the other classes doing? ... |
Im going to have two seprate 2D arrays. I know how to add the rows of a 2d array and i know how to add the columns of a 2d array, but i dont know how to do this. There are TWO 2D Arrays. I need to add the index of the first 2d array with the same index of the ... |
|
Im trying to make code where there are 2, 2D arrays, that the user can choose to add together or subtract. Right now, im jsut trying to get the user input to work, but im getting a lot of errors. What am i doing wrong? Java Code: import java.util.Scanner; public class Lab3 { public static void main(String [] args) { Scanner ... |
|
public class Cmap { private int col,row; char[][] room= new char[5][5]; public Cmap(){ for(row=0;row<5;row++){ for(col=0;col<5;col++){ room[row][col]='#'; } } } public void Msqre(){ room[1][1]=' '; room[1][2]=' '; room[1][3]=' '; room[2][1]=' '; room[2][2]=' '; room[2][3]=' '; room[3][1]=' '; room[3][2]=' '; room[3][3]=' '; } public void prntRm(){ for(row=0;row<5;row++){ if(row>0){ System.out.printf("\n"); } for(col=0;col<5;col++){ System.out.printf("%c",room[row][col]); } } } } |
|
public class CustomerDisplay { JFrame frame = new JFrame(); JPanel panel = new JPanel(); private String [] columnNames = null; private String[][] rowData = null; public CustomerDisplay(String rowData[][], String [] columnNames ) { this.columnNames = columnNames; this.rowData = rowData; JTable table = new JTable(rowData, columnNames); JScrollPane sp = new JScrollPane(table); panel.add(sp); add(panel); } } |
All, I want to read a file and use to create a maze. The file is as follows: 1111111111 1000000001 1000000001 1000000001 1000000001 1111100111 1000000001 1000000001 1000000001 1111111111 Where 1 is supposed to be a wall, and 0 free space. I solved the issue with a simple scannermethod, but now I want to use a filereader. The code : Java Code: ... |
39. 2D array java-forums.orgHello guys.. I am new to jave and looking for some help... // here is the application import javax.swing.JOptionPane; public class rainfall { public static void main(String args[]) { //the first array 4 weeks int a[][]=new int[4][]; //the user should determine the second array which is the reading of rainfall amount per week. int rainfall_per_week, reading,total_rain_month=0, avg_rainfall_per_week; int i,j,k; //loops for(i=0;i |
So, Im new to java and I have an assignment to enter Student Scores in a 2D array. I need to then add each students scores and compute the average. My question is how do I add the elements in each row of the array. Here's the code I have so far that simply prints the scores in a table public ... |
This is a little project I'm just doing myself, and I'm stuck. I'm basically trying to create a program, that, some time later, when I learn GUIs properly, will allow the user to play chess. For now, I'm just trying to set up the board and everything, but I'm having trouble with static (error I'm getting is non-static method cannot be ... |
program for asking the user to enter the size of the array(row).The size of the column is fixed (it should be 3). Next the user has to enter the roll number which starts with IAD example IAD123 The roll number 123 should be stored and then ask user to enter the marks and also the name of the person and store ... |
import java.io.*; import java.util.*; public class Map{ public static void main(String[] args){ File map = new File("map1.txt"); try{ String line; ArrayList list = new ArrayList(); Scanner scan = new Scanner(map); for(int i = 0;scan.hasNextLine();i++){ line = scan.nextLine(); list.add(line); } System.out.println(list);//demonstrative, to check working, will be removed } catch(Exception e){ System.out.println("invalid file"); } char[][] mArray = new char[//width][//height]; } } |
I think this can also be done by using the toCharArray() method of the String class. This will return an array of char's for each line of text in your text-document. Then you add the given array to your own predefined 2dementional array and go the the next line of text and the next place in your array. Repeat. |
|
|
47. 2D Arrays java-forums.orgIn the following program for the two dimensional arrays I want to get the same output as the one dimensional arrays, but how do I get "biome" and "precipitation" every time? I don't think that I can split it up the way I have where I have secdim[j] and secdim[i], because when splitting it up I end up with strange symbols ... |
I know this is probably a very simple solution, but im very new to Java... im just switching over from C My problem is i get a "Null" exception on my gameOn 2D array in the display method... can anyone help? The display is a method that supposed to print a 6 by 7 grid of X's with numbers 1 through ... |
I have to make a program and I'm not familiar with 2D arrays. I need to take a 1D array convert it to a 2D array. I'm taking a string whose length is a square, and initialize the characters into a 1D array, then initialize them into a 2D array, like a square. So this is what I need to do: ... |
Battleship 2D Array Program here is basically the instructions for the full program i have to write so that it is clear. up to this point, it seems to work except: 1. when i run it, the ship should take up 4 values/spots on the board so in end, there should be 4 '*''s, but i run it fully ... |
I am having trouble with my assignment and got totally lost with 2-D array. I'm not that good with java, but still trying my best to learn it for class. I have to have users input their answers in this problem. Here is the question: Requirements: Write a program to allow the user to input sales data for several sales representatives ... |
I have a project due thursday and it involves creating a 2d array that displays the output 123 456 789 Here is what i have so far... public class twodarray { public static void main(String[] args) { int [][]T={{1,2,3},{4,5,6},{7,8,9}}; list (T); public class list (int[]x) { for (int R=0; R<3;R++) for (int C=0; C<3;C++) System.out.print(x[R][C] + " "); } } } ... |
|
Hi, I have a question about this little code example. Its always these little nested for loop tricks that mess me up! LOL. Hopefully I will get used to them. But below, I have the following questions about it. The code outputs a table of 0-9 in the top row, then the next row 10-19, etc, there are 5 rows total. ... |
hi there, i'm doing an assignment for college and have been asked to make a battleship like game. i have managed to make a grid using a 2d array which gives an out put as follows: xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx i want to put numbers on the x and y axis as reference so that it would ... |
This method should return a new matrix with each of its dimensions doubled in size. The way to do this is that every single value in the original matrix should be duplicated 3 additional times so that it is present 4 times in the new matrix. The header should look something like this: public static int[][] doubleSize(int[][] m) |
This method should return a new matrix with each of its dimensions halved in size. The way to do this is to compress the matrix by taking each 2 by 2 block of the original matrix, averaging the 4 values, and replacing it by the 1 average. You may assume that the matrix has even dimensions in both directions. The header ... |
This post concerns doubling the size of a matrix. The method takes an int[][] as imput and outputs a new int[][] double in size. Some of the code I have managed to write: public static int[][] doubleSize(int[][] m) { int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length]; for (int i=0, i < m.length; i++) { for (int j=0; j < ... |
hello, i have a java homework to make a game called "game of life" its suppose to be popular game where you select a generation one creature and the program just keeps making new generations of it. im suppose to create a generation zero using 25x25 2d arrays, the walls are suppose to be left blank, and the program is suppose ... |
60. 2D array java-forums.orgWhat data type is the array? What goes in the empty elements? To define a two dim array: [][] twoDim = new [rows][columns]; Then go thru the rows and columns to set the values. Or another way is to only define the rows initially and define the columns later: [][] twoDim = new [rows][]; twoDim[i] = new [columns for this row]; ... |
Hello, I'm new here but I need some help with some homework. I'm required to make 2 classes, a maze class and a mazeDriver class. The mazeDriver class is a simple UI which i've already done and it works fine. The maze class how ever, I've followed the instructions given and generated the maze the way it says to, but the ... |
My assignment is to create a battleship board (6x6) and ask the user to guess a position and the output will display hit or miss. right now my ship is just 1x1 so far, but my teacher also wants a text based board to be displayed with occuring misses on them. I need help configuring the 2d array code. Here's what ... |
Well, looks like this my first post. Need a little help with my homework. Anyway, here's what I have so far. Both #'s 2 and 3 are somewhat similar, but I'm stumped. The main method is given by default. Please && Thank you. Java Code: /* Step 2: Write a method with the header public static int sumRow(int[][] arr, int m) ... |
Checking 2d Arrays to Determine if they are Magic Squares (all sums are the same) Got most of the methods done, and just need help the main and first method because they are the only ones I am unsure about in which I am getting incorrect output. Here are the directions for the first and main methods. "Write a ... |
class Temperature { private String name; private double[][] temp; public Temperature(String name, double[][] temp2) { this.name = name; int day = temp2.length; int hour = temp2[0].length; temp =new double[day][hour]; for(int i=0;i |
|
67. 2D Arrays java-forums.orgHi could anyone give an example of how to search a sorted square 2d array with binary search (i assume that is the quickest if not i would love to hear other ideas) 2D array is sorted in both rows and collumns eg. 2 4 5 6 8 3 6 7 8 10 5 8 9 12 13 6 10 11 ... |
I need to make a 2D array out of the 0's and 1's in the file. The file is 25X25 in total so contains 625 characters in all, either 0 or 1. This is the code I have so far: import london.*; public class Picture { public static void main (String[] arg) { EasyReader fileInput = new EasyReader("picture.txt"); EasyGraphics g = ... |
I have this code which makes a 2D array out of a .txt file which contains 25X25 0's and 1's (625 characters in all) to make a picture. I have a graphics class calledEasyGraphics (which is a part of the "london" package), which makes a graphics window of custom size. I need the picture to be displayed in the graphics window ... |
boolean flag = true; String temp; while (flag) { flag = false; for (int j = 0; j < Player.player.length - 1; j++) { System.out.println(""+Player.player[j][3]+" "+Player.player[j+1][3]); double currentScore = Double.parseDouble(Player.player[j][3]); double nextScore = Double.parseDouble(Player.player[j+1][3]); if (currentScore < nextScore && currentScore != 0) // '>' for ascending sort { temp = Player.player[j][4]; Player.player[j][4] = Player.player[j+1][4]; Player.player[j+1][4] = temp; flag = true; System.out.println("A ... |
|
Hey everyone, I'm new to this forum. This is my first semester of programming and I'm working on a project. I was hoping someone could take a look at my code and help me figure out what I'm doing wrong. Note: The program compiles just fine, however, one of the operations isn't performing correctly on output. I have a program that ... |
Hello, I'm working on a project and I need to write a program that asks the user for the 2 dimensions, and then the program asks the user to enter all the elements by row and column which I've successfully done but I can't figure out how to print out the finished matrix. Here's what I have so far: import java.util.Scanner; ... |
Buffered is really the way to go, as people have said. A buffered image is already pretty much a 2d array with color values, hence it's MASSIVE memory requirement. If the only thing you need are the pixel color values, you can grab those directly from the buffered image. Really no need to copy the color data into an actual array, ... |
you would need to use a thread if you wanted it to update constantly. I would recommend using images and just using a Gridlayout. Then just updating the images everytime someone makes a move. your way works, but it over complicates the problem. Also if you us buttons or images you can just add a mouse listener to it. |
|
I thought I would have to parse the XML file and then write that into the array I needed, but I was told it should be easier to just reverse the XMLEncoder with XMLDecoder. I thought the XMLDecoder read it back as an object and that is why I am having this problem. |
My question is fairly simple I just want to make sure I am doing this correctly. I wrote a 2D array out to an XML file. I want to put it back into a 2D Array. I believe I have to parse out the information I want and then insert it into the array correct? Should I be using SAX or ... |
Hello, I am having trouble taking a 2D array (0 and 1's at this point, for unoccupied and occupied) and creating an image that shows occupied cells as one color (say red) and the unoccupied cells as another color (say white). Eventually, I would like to be able to update the image with a new distribution, showing the motion of the ... |
|
|
|
83. 2D array ? forums.oracle.comYou're creating two one dimensional arrays there. Every element of an array has to have the same type as the declared array type or a subtype thereof, so int and String won't work. Better use a little class with an int and a String and define an array of that type. kind regards, Jos |
|
85. 2d array forums.oracle.comhello, i am making a 2d array of integers, but the numbers that it contains are very specific so i have to state exactly what every single value within it should be (and they are all different). i know i could do it like: array[0][0] = 12; array[0][1] = 3; etc etc but it is a 13 by 13 array and ... |
I am very new to java and extremely new to using arrays. Lets say I have 3 sales people and need to enter their sales from monday to friday. How would I go about populating the 2D array. Should I use a [3][5] (3 by 5) Should I use a [4][6] (4 by 6) Lets say I want the user to ... |
|
|
I would not use arrays of Strings at all - create a Book class that holds a book's various attributes. You can then create an array of Books instead. I just read your requirements more closely and what I suggested is exactly what you are supposed to do. Edited by: jbish on Mar 4, 2008 8:18 PM |
Hi all, How can I split a 2D array of strings into 2 , 1D arrays? I'm implementing a page rank algorithm. webpage->link->link String[][] data= { { "0 2", "1 0", "2 0 1" }, {"0","2","1"} ,{ "0", "1 0", "2 1 0", "3 2 1 0", "4 3 2 1 0"}, {"0","1","2","3","4"} }; String[] url should hold the first element ... |
|
You did not assign a value for the "code" field. By default it is 0 and it is not equal to the value of 'A', so your code never gets into the if statement " if (shipCode == code) ". As a result you always get the value 0.0, which is the default value when you do not assign a value ... |
Are multi-dimensional arrays limited to only one type? For example; int[][] array = new int[2][10]; In this example, the array only holds only integers. I would like to construct a 2D array with different types to represent data pairs. In my example, I would like the first 'column' to hold Date objects, and the second 'column' to hold Double objects (the ... |
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if ... |
Alright, so I'm almost done this program but i've been stuck on this last bit of code for about an hour. I have a 2d array called ACL. I want it so that when i enter something like Bob as the Person and File1 as the object, then R as the permission the R goes in the element for bob/File1. Any ... |
I'm writing a program that searches for a sequence of characters within a 2d array. The 2d array is static and all the elements are declared with no user input. The program then asks for what "word" the user would like to search for and converts the string into a 1d array. The program now has to search for the entire ... |
97. 2-D arrays forums.oracle.comHi all, I have to develop a program on wich the user will input a digit(from 0 to 9) and the program will output that digit as a grid of X characters. For instance, if the digit is 2 then the output should be: x x x x x x x x x An array will contain row/column positions for X. ... |
|
|
|