Hi guys I am currently learning Java and I'm trying to make a program that you can use too book motel rooms. I'm trying to create a method to search the amount of days a guest needs and then check the days and units that are free. I'm using a 2D array of an array of an array.
public void doSearch() {
this.redisplay();
int daysWanted = UI.askInt("Number of days required");
int days = 0;
for(int i = 0; i < NUM_UNITS; i++){
for(int j = 0; j < NUM_DAYS; j++){
if(bookings[i][j] == null){
days++;
if(daysWanted >= days && this.bookings[i] == this.bookings[i]){
this.displayCell (i, j, Color.red);
}
}
}
}
}
here is what the program looks like currently, and what i have shown above is the method i am trying to do. What my problem is, when i type in how many days, it checks if the available unit is free for that many days, but i want it to be consequtive. How would i go about this? any help is appreciated, thanks :)
change this :
for(int j = 0; j < NUM_DAYS; j++){
if(bookings[i][j] == null){
days++;
if(daysWanted >= days && this.bookings[i] == this.bookings[i]){
this.displayCell (i, j, Color.red);
}
}
}
to something like this:
days = 0;
int[] daysWeWant = new int[daysWanted];
for(int j = 0; j < NUM_DAYS; j++){
if(bookings[i][j] == null){
daysWeWant[days] = j;
days++;
if(daysWanted == days){
break;
}
}
else{
days = 0;
int[] daysWeWant = new int[daysWanted];
}
}
if(days== daysWanted ){
for(int j = 0 ; j< daysWanted; j++){
int day = daysWeWant [j];
this.displayCell (i, day , Color.red);
}
}
If you do this it makes days zero if one day is not avaiable and continue to search again.
I suspect Lrrr was on the right path - you need to reset your days counter for each unit that you are searching. Likely you can change:
int days = 0;
for(int i = 0; i < NUM_UNITS; i++){
for(int j = 0; j < NUM_DAYS; j++){
to
for(int i = 0; i < NUM_UNITS; i++){
int days = 0;
for(int j = 0; j < NUM_DAYS; j++){
But without sample data, it would be a bit of work to check for certain.
You might find that writing some unit tests would help you a lot in working on this.
Related
I have been working an a good size but single threaded java application. In it I have a function which loops through a matrix of about size[300][10]. I have done a lot of things above this code snippet and have 3 other matrices of similar sizes as local variables. I was having problems with the loop not going through the first value (table[0][0]) when I noticed that the code:
System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
prints out:
looped through 0 0
but the code:
//System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
does not print anything.
Why would this be? Have I run out of Java heap space? Have I overflowed? Is this a compiler error?
I found what was wrong. Above the loop I had another loop and had left a printf statement in there. I am still not sure why this occurs but I was able to reproduce it in the following class.
public class TestJavaPrintfError {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.printf(" ");
}
String[][] table = new String[300][6];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if (i == 0 && j == 0) {
System.out.println("looped through 0 0");
}
}
}
}
}
I am on developing on eclipse neon. Anyone know why this happens?
Is that a joke?
Just scroll the console to the rightmost, you will see the print.
You are using System.out.printf(" "); 100 times, it output alot of spaces and push your pritnt to the right.
I'm trying to evaluate a hand to see if they have a pair and I feel like this is right but i keep getting errors. Any idea on what im doing wrong?
public boolean isPair() {
String[] values = new String[5];
int counter = 0;
//Put each cards numeric value into array
for(int i = 0; i < cards.length; i++){
values[i] = cards[i].toString();
}
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(values[j].equals(cards[k].toString()))
counter++;
if(counter == 2)
return true;
}
counter = 0;
}
return false;
}
The first obvious error I can see, is that you re comparing the first card with itself here :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
Your index k should not be verified if it equals j.
Secondly, why are you using a your variable "hand" in the comparison when you bothered to create a array of String containing your hand ?
values[j].equals(cards[k].toString())
You can write :
values[j].equals(values[k])
I dont think it is responsible for any errors, but it s much easier to understand.
And finally, your counter is false. A pair, is by definition two cards of the same value. So, you have to check if a value is present only two times(that means 1 equality) in your hand.
So you'll have :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(k!=j){
if(values[j].equals(values[k]))
counter ++;
}
}
if (counter ==1 ) //Counter==1 means the a value matched with an other value in your hand only once, involving one pair.
return true;
else counter = 0;
}
return false;
String[] values = new String[5];
should be
String[] values = new String[cards.length];
However, even better would be to not use values anyway, since it's almost a copy of cards.
Didn't know how to call my Thread.
public NaturalNumberTuple(int[] numbers) {
int [] thisTuple = new int[numbers.length];
int count = 0;
for(int j = 0; j < numbers.length; j++){
if(numbers[j] > 0){
thisTuple[j] = numbers[j];
count++;
}
}
int[] newTuple = new int[count];
for(int i = 0; i < newTuple.length; i++){
int k = i;
while(thisTuple[k] <= 0){
k++;
}
newTuple[i] = thisTuple[k];
}
this.tuple = newTuple;
}
This is my code snippet to create a new NaturalNumberTuple.
So this is the Array I want to use: int[] tT2 = {1,2,4,-4,5,4,4};
I only want to use natural numbers greater than 0 and my problem isn't to cut out the negative number but it is that my console is giving me this: Tuple(Numbers:1,2,4,5,5,4).
The problem is if I jump over that value which is negative with my while loop to get the higher (k) I will have to pass the same (k) in my for loop which I don't want to because I already got it in my Array. I hope you understand my problem.
Sorry for the bad english..
Edit: Can't use any methods from java itself like System.arrayCopy
You have an error in the first loop. Fixing it makes the second loop much simpler :
public NaturalNumberTuple(int[] numbers) {
int [] thisTuple = new int[numbers.length];
int count = 0;
for(int j = 0; j < numbers.length; j++){
if(numbers[j] > 0){
thisTuple[count] = numbers[j]; // changed thisTuple[j] to thisTuple[count]
count++;
}
}
int[] newTuple = new int[count];
for(int i = 0; i < newTuple.length; i++) {
newTuple[i] = thisTuple[i];
}
this.tuple = newTuple;
}
Of course, the second loop can be replaced with a call to System.arrayCopy.
I would change your while loop to an if that simply restarts the for loop. Say from this:
while(thisTuple[k] <= 0){
k++;
}
To something like this:
if (thisTuple[k] <= 0)
continue;
This stops you from adding the same number twice when you encounter a negative or zero number.
This code will solve you issue. The code is checked in the following link Tuple Exampple
int [] thisTuple = new int[numbers.length];
int count = 0;
for(int j = 0; j < numbers.length; j++){
if(numbers[j] > 0){
thisTuple[count] = numbers[j]; //Change to thisTuple[count]
count++;
}
}
int[] newTuple = new int[count];
for(int i = 0; i < count; i++){
newTuple[i] = thisTuple[i];
}
I apologize if I'm not clear. I'm new to programming. So lets say I have a char[10][10]. And there are two+ chars I want to assign at intervals for example i[0][0] to i[5][7] have Y and the rest have N. How would I do that if its possible? I've been trying to figure it out for 6+ hours.
One possible solution would be to have a 'for' block that goes through the rows and another 'for' block that goes through the columns. it could be something like
char[] arr= {'Y','N'};
int counter = 0; // <- these are optional depending on what you choose below
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
// i[j][k]= here you should assign the value
counter++;
}
}
The way to assign the value depends on what you want to do. If you want to have it to generate randomly you can do something like i[j][k]= arr[(int)(Math.random()*2)] or if you want to have it alternate between Y and N you could have a counter variable and assign i[j][k]= arr[counter%2] . If you want to assign the first half to 'Y' and the other half to 'N' i[j][k]= (counter<=50)?'Y':'N';. And the particular case you ask would be i[j][k]= (j<=5 && k<=7)?'Y':'N';It really depends much on what you want to do
This can be done with loops.
for(int i = 0; i < 5; i++){
for(int j = 0; j < 7; j++){
i[i][j] = 'N';
}
for(int j = 7; j < 10; j++){
i[i][j] = 'Y';
}
}
for(int i = 5; i < 10; i++){
for(int j = 0; j < 10; j++){
i[i][j] = 'Y';
}
}
char[][] theArray = new char[10][10]
upToX = 5; // limit for rows
upToY = 7; // limit for columns
for(int i = 0; i < 10; i++ ){
for(int j = 0; j< 10; j++ ){
if((i+1)*(j+1) <= (upToX+1)*(upToY+1)){
theArray[i][j] = 'Y';
}
else{
theArray[i][j] = 'N';
}
}
}
Try using for-loops and if-else. Since you are looking for yes/no type values, I just used boolean type in my example
boolean[][] arr = new boolean[10][10];
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
if(i < 6 && j < 8)
arr[i][j] = true;
else
arr[i][j] = false;
}
}
I have implemented a code in java which has a for loop which varies from 0 to 0x10000 times.
Now i am importing this code into android and running the same code.
Problem if that:
In Core java loop executes with in 2 seconds at max.
But when same loop is being executed in android it is taking 4 minutes (Disgusting)
Please can any one help me out from this, i am pasting my loop for your understanding:
for (int r = 0; r < 0x10000; r++) {
for (int j = 0; j < password.length; j += 4) {
long[] key = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
if (i + j < password.length) {
//do something
}
}
//calling one method.
}
}
Basically main loop varies upto 64000 times. Please suggest.
May be this can help a little:
int maxR = 0x10000;
int passwordLength = password.length;
for (int r = 0; r < maxR; r++) {
for (int j = 0; j < passwordLength; j += 4) {
long[] key = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
if (i + j < passwordLength) {
//do something
}
}
//calling one method.
}
}
But I think that the thing to optimized is probably the content of your last for loop and the method you call after it...
[EDIT] If you don't access to other items of the "key" array than the one at index i, you can do what Joop Eggen suggested:
int maxR = 0x10000;
int passwordLength = password.length;
long[] key = {0, 0, 0, 0};
for (int r = 0; r < maxR; r++) {
for (int j = 0; j < passwordLength; j += 4) {
for (int i = 0; i < 4; i++) {
key[i] = 0;
if (i + j < passwordLength) {
//do something
}
}
//calling one method.
}
}
long[] key = new long[4];
int incompleteFourer = password.length % 4;
int n = password.length - incompleteFourer ;
for (int r = 0; r < 0x10000; r++) {
for (int j = 0; j < n; j += 4) {
for (int i = 0; i < 4; i++) {
key[i] = 0;
//do something
}
//calling one method.
}
if (incompleteFourer != 0) {
int j = n;;
//calling one method.
}
}
Memory allocation of key only once; assume "do something" only considers key[i].
Index of j to always have full 4 elements; "calling one method" might not handle some of the three last elements.
The last 1 to 3 elements are handled separately.
NetBeans IDE has a nice profiler, and maybe the same bottleneck concerns both loops. At least measure times of indiivual calls on Android; maybe it is a slow Dalvik implementation of some basic function.