I am creating a shadow of createTriangle() in the method createPathingTriangle(). createTriangle worked just fine, but when I created the identically-dimensioned createPathingTriangle that used String instead of int, the new triangle started throwing NPE's.
The line that's throwing it is-
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
i.e. the first line that populates it. I looked it up and have liberally sprinkled "new" around the code in createPathingTriangle, but it didn't seem to solve the problem.. I am assuming that it has something to do with the fact that int is a primitive but String is not, but hours of fiddling and nothing gives.
private int[][] createTriangle() {
triangle = new int[triangleSize][];
for (int y = 0; y < triangle.length; y++){
int [] xAxis = new int[triangle.length - y];
for (int x = 0; x < xAxis.length; x++){
xAxis[x] = (int) (Math.random() * 100);
}
triangle[y] = xAxis;
}
printTriangle(triangle);
return triangle;
}
private String[][] createPathingTriangle() {
pathingTriangle = new String[triangleSize][];
for (int y = 0; y < pathingTriangle.length; y++){
for (int x = 0; x < pathingTriangle.length - y; x++){
if (x < 10){
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
}
else if (x < 100){
pathingTriangle[y][x] = new String("0" + String.valueOf(x));
}
else{
pathingTriangle[y][x] = new String(String.valueOf(x));
}
}
}
return pathingTriangle;
}
You never initialize the second dimension of the array pathingTriangle = new String[triangleSize][];
which is done in the first funnction here int [] xAxis = new int[triangle.length - y];
Related
So i set out to make a little "encoding" program that uses a simple algorithm and so far it all works. i came up with the algorithm, and then found the inverse of it to "decode" the given String.
How it works is that in command line you do "java Diver lock message password". It then takes the ascii values and runs it through the algorithm Z_n = (X_n + Y_n) / 2, giving you an "encoded" string that can then be used in the program arguments on start as "java Driver unlock code password". It takes these values and runs them through X_n = 2Z_n - y_n.
These algorithms work when simply using the lock portion, and i've put the same process at the end of lock that happens in the unlocking process, yet when trying only the locking process, the output is incorrect.
Here's a little snippet as to how i believe it is working
lock Oliver Chipper
unlock ĤƨƤnjƪƮä Chipper
x = message, y = password, z = code
x & y = z
z & y = x
I have a feeling that it has to do with the command line not taking in the symbols that are used as output, but a thorough explanation of what i've done would be great... Thank you!
public class Driver {
private static int[] x; //Message or code
private static int[] y; //Password
public static void main(String[] args) {
int SIZE = args[1].length() + args[2].length();
if (args[0].equals("lock")) {
lock(args, SIZE);
} else if (args[0].equals("unlock")) {
unlock(args, SIZE);
}
}
private static void lock(String[] args, int size) {
x = new int[size];
for (int i = 0; i < args[1].length(); i++) {
x[i] = args[1].charAt(i); //Message to ints
}
y = new int[size];
for (int i = 0; i < args[2].length(); i++) {
y[i] = args[2].charAt(i); //Password to ints
}
//code
int[] z = new int[size];
for (int i = 0; i < size; i++) {
z[i] = ((x[i] + y[i]) * 2); //Locking algorithm
System.out.print((char)z[i]);
}
System.out.println("\n");
for (int i = 0; i < size; i++) {
System.out.print((char)((z[i] / 2) - y[i])); //Unlocking algorithm
}
}
private static void unlock(String[] args, int size) {
x = new int[size];
for (int i = 0; i < args[1].length(); i++) {
x[i] = args[1].charAt(i); //Code to ints
}
y = new int[size];
for (int i = 0; i < args[2].length(); i++) {
y[i] = args[2].charAt(i); //Password to ints
}
for (int i = 0; i < size; i++) {
System.out.print((char)((x[i] / 2) - y[i])); //Unlocking algorithm
}
}}
I'm trying to create two classes, one to define a point and another for array operations. I'm trying to create a method to sort an array of coordinates in ascending order based on y coordinates. I've tried following examples, but I keep running into a runtime error where the array is only partially sorted.
public class Point
{
private double x;
private double y;
public Point(double x_coord, double y_coord)
{
x = x_coord;
y = y_coord;
}
public boolean lessThan(Point anotherPoint)
{
if(y < anotherPoint.y)
{
if(x < anotherPoint.x)
{
return true;
}
}
return false;
}
}
public class PointArray
{
private Point[] points = new Point[count];
public PointArray(double[] doubleArray)
{
if(doubleArray.length % 2 == 0)
{
for(int i = 0, j = 0; i < 3; i++, j += 2)
{
double x = doubleArray[j];
double y = doubleArray[j + 1];
points[i] = new Point(x, y);
}
}
else
{
System.out.println("Error: The given array must be even.");
System.exit(0);
}
}
public void sort()
{
double x = 0;
double y = 0;
Point newPoint = new Point(x, y);
Point temp = new Point(x, y);
for (int i = 0; i < points.length - 1; i++)
{
for(int j = i + 1; j < points.length; j++)
{
int minIndex = i;
if(points[minIndex].lessThan(points[j]) == false)
{
temp = points[minIndex];
points[minIndex] = points[j];
points[j] = temp;
}
}
}
}
This code causes the array {5.6, 7.1, 4.9, 13.17, 9.3, 2.9} to first be stored as ordered pairs {(5.6, 7.1), (4.9, 13.17), (9.3, 2.9)}. but it does not sort them properly. After the first and third points are swapped, the second and third are not, even though the y coordinate of the third is smaller.
[(9.3, 2.9), (4.9, 13.17), (5.6, 7.1)]
EDIT: Another issue appeared related to the same assignment. This method is supposed to take two PointArray objects and compare them for equality by the x and y components. My idea was to sort both arrays and then compare the components using a method in the Point class, but I'm not sure how to define each PointArray in terms of an (x, y) point.
public boolean equals(Point anotherPoint)
{
if(x == anotherPoint.x && y == anotherPoint.y)
{
return true;
}
return false;
}
public boolean equals(PointArray anotherPointArray)
{
double x = 0;
double y = 0;
double xAnother = 0;
double yAnother = 0;
Point newPoint = new Point(x, y);
Point newAnotherPoint = new Point(xAnother, yAnother);
anotherPointArray.sort();
for(int i = 0; i < points.length; i++)
{
for(int j = 0; i < points.length; j++)
{
if(newPoint.equals(newAnotherPoint))
{
return true;
}
}
}
return false;
}
Your current lessThan method will give true only if both x and y are smaller. To sort by y alone use
public boolean lessThan(Point anotherPoint)
{
return y < anotherPoint.y;
}
I have an array with some numbers in it, that are procedurally generated through the Code and I want to remove the part of the array that is not necessary.
So until now I tried my best to create my own code, but it didn't work quite well (Thats the reason why im here).
This was my try:
private void cutPlacementMap() {
int firstNeededX = Integer.MAX_VALUE;
int firstNeededZ = Integer.MAX_VALUE;
int lastNeededX = -1;
int lastNeededZ = -1;
for (int x = 0; x < placementMap.length; x++) {
for (int z = 0; z < placementMap[0].length; z++) {
if (placementMap[x][z] != 0) {
if (x < firstNeededX)
firstNeededX = x;
if (z < firstNeededZ)
firstNeededZ = z;
if (x > lastNeededX)
lastNeededX = x;
if (z > lastNeededZ)
lastNeededZ = z;
}
}
}
int lengthX = lastNeededX - firstNeededX;
int lengthZ = lastNeededZ - firstNeededZ;
int[][] newPlacementMap = new int[lengthX + 1][lengthZ + 1];
System.out.println("lengthX: " + lengthX);
System.out.println("lengthZ: " + lengthZ);
int conX = 0;
int conZ = 0;
for (int x = firstNeededX; x <= lastNeededX; x++) {
for (int z = firstNeededZ; x <= lastNeededZ; z++) {
newPlacementMap[conX][conZ] = placementMap[x][z];
conZ++;
}
conZ = 0;
conX++;
}
placementMap = newPlacementMap;
}
This Code throws an ArrayIndexOutOfBoundException. That is pointing to the line newPlacementMap[conX][conZ] = placementMap[x][z]; as reason for the exception.
Every help is appreciated. Thanks.
Debugging this yourself should be pretty easy.
Take a look at your loop:
for (int z = firstNeededZ; x <= lastNeededZ; z++)
This will loop forever (or until you get the ArrayIndexOutOfBoundsException), because your stop condition contains a typo.
x <= lastNeededZ must of course be z <= lastNeededZ.
I'm trying to fill a matrix with stars (*) to draw Bresenham's line, but when i'm printing out the thing the matrix is only filled with one star i don't know whats wrong. Language is Java
public class PtLine extends VectorObject{ // inherites from another class
private int bx;
private int by;
private int delX;
private int delY;
public PtLine(int id,int x,int y,int bx,int by){
super(id,x,y);
this.bx = bx;
this.by = by;
this.delX = this.bx-x;
this.delY = this.by-y;
}
public void draw ( char [][] matrix ){ // filling the martic with stars
int D = 2*delY - delX;
matrix[x][y] = '*';
int j = y;
for (int i=x+1;i==bx;i++){
if(D > 0){
j+=1;
matrix[i][j]='*';
D = D + (2*delY-2*delX);
}
else{
matrix[i][j]='*';
D = D + (2*delY);
}
}
}
}
The following code is when i'm trying to print out the matrix
class Question3{
public static void main ( String args [] ){
char[][] matrix = new char[20][20];
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
matrix[y][x] = ' ';
}
}
PtLine n = new PtLine(6,6,6,13,13);
n.draw(matrix);
for (int y = 0; y < 20; y++) {
for (int x = 0; x < 20; x++) {
System.out.print(matrix[x][y]);
}
System.out.println();
}
}
}
It's likely that you have to change i==bx with i!=bx:
public void draw ( char [][] matrix ){ // filling the martic with stars
int D = 2*delY - delX;
matrix[x][y] = '*';
int j = y;
for (int i=x+1;i!=bx;i++){ // here
...
}
}
The for loop continues while this condition is true. In your code loop finishes immediately at start, before the first iteration, because this condition is false.
package net.gfx;
public class TileSet {
public final int TILES = 627;
class Tiles {
int x = 0, y = 0;
int w = 0, h = 0;
}
public Tiles tiles[] = new Tiles[TILES];
public TileSet() {
for (int i = 0, y = 0; i < TILES; i++) {
for (int x = 0; x < 1280; x =+ 25) {
if (x > 1280) {
x = 0;
y += 40;
}
else {
tiles[i].x = x; //ERROR CAUSED HERE
tiles[i].y = y; //TO HERE *Unknown reason*
tiles[i].w = 40;
tiles[i].h = 40;
}
}
}
}
}
The Error im getting:
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.jump.Jump.<clinit>(Jump.java:8)
Caused by: java.lang.NullPointerException
at net.gfx.TileSet.<init>(TileSet.java:24)
at net.gfx.Graphics.<clinit>(Graphics.java:10)
... 1 more
What I'm trying to do is basically create an array of tiles on a screen. Everyting else works besides the setting the Object array Values.
I've searched almost everywhere and haven't found anything. I bet its probaly some simple thing i missed.
You have to create an instance of Tiles before you can do any operation on it.
for (int i = 0, y = 0; i < TILES; i++) {
for (int x = 0; x < 1280; x =+ 25) {
if (x > 1280) {
x = 0;
y += 40;
}
else {
tiles[i] = new Tiles(); //instance created here.
tiles[i].x = x;
tiles[i].y = y;
tiles[i].w = 40;
tiles[i].h = 40;
}
}
}
You aren't initializing each Tiles in your array, as the default value is null.
Perhaps you should try initializing each Tiles:
for(int i = 0; i < tiles.length; i++){
tiles[i] = new Tiles();
}
After that, you could perform operations with each Tiles element in the array.