Related
I am creating a seating chart in java that displays the cost of each seat in a two dimensional array:
public class MovieTheater {
public static void main(String[] args) {
final int rows = 10;
final int columns = 10;
int i;
int j;
int[][] seating = {
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 }
};
However, when I attempt to print the array:
for (i = 0; i < rows; i++) {
System.out.print(rows[i]);
for (j = 0; j < columns; j++) {
System.out.print(columns[j]);
}
}
}
}
I receive an error that states: array required, but int found
Is this a problem with my array format or a syntax problem for my print solution?
You do columns[j], but columns is an int, so you can't access it like an array. Same with rows[i]. What you should do is in the inner loop
System.out.println(seating[i][j]);
"columns" and "rows" have been defined as int, not as arrays of type int. the index value of rows and columns can be used to access rows and columns of array(seating). and it can be printed with a single print statement:
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++)
System.out.print(seating[i][j]);
You are not actually accessing your array object in your for loop.
Try this:
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
System.out.print(seating[i][j]);
}
}
You code doesn't work because you're trying to use an integer as an array.
In fact rows and columns are two integers (value 10); your array is seating.
When the compiler compiles your code it sees something like this:
for (i = 0; i < 10; i++) {
System.out.print(10[i]);
for (j = 0; j < 10; j++) {
System.out.print(10[j]);
}
}
which is impossible.
What you really want is:
for (i = 0; i < rows; i++) {
for(j = 0; j < columns; j++) {
System.out.print( seating[i][j] );
}
}
I've got an array with 225 elements and 15 smaller arrays which their length sum is exactly 225.
The point is that I need to fill the larger array with these smaller arrays but in a random way.
private final short datosdeNivel[]= new short[225];
private final short diecinueve[]= {19, 19};
private final short veintiseis[]= {26, 26, 26};
private final short dieciocho[]= {18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18};
private final short veintidos[]= {22, 22};
private final short veintiuno[]={21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
private final short cero[]= {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};
private final short diecisiete[]= {17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
private final short dieciseis[]= {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
private final short veinte[]= {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
private final short veinticuatro[]= {24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
private final short veinticinco[]= {25, 25, 25, 25};
private final short veintiocho[]= {28, 28};
private final short uno[]= {1, 1, 1, 1, 1, 1, 1};
private final short nueve[]= {1};
private final short ocho[]= {9, 9, 9, 9, 9, 9, 9, 9, 9};
How can I establish a random order so every time the program runs the order in which the smaller arrays are placed in the larger array is different?
This would be a way to fill it in order:
int aux;
aux= diecinueve.length;
for(int i=0; i<diecinueve.length; i++)
{
datosdeNivel[i]= diecinueve[i];
}
for(int i=0; i<veintiseis.length; i++)
{
datosdeNivel[aux]= veintiseis[i];
aux++;
}
for(int i=0; i<dieciocho.length; i++)
{
datosdeNivel[aux]= dieciocho[i];
aux++;
}
for(int i=0; i<veintidos.length; i++)
{
datosdeNivel[aux]= veintidos[i];
aux++;
}
for(int i=0; i<veintiuno.length; i++)
{
datosdeNivel[aux]= veintiuno[i];
aux++;
}
for(int i=0; i<cero.length; i++)
{
datosdeNivel[aux]= cero[i];
aux++;
}
for(int i=0; i<diecisiete.length; i++)
{
datosdeNivel[aux]= diecisiete[i];
aux++;
}
for(int i=0; i<dieciseis.length; i++)
{
datosdeNivel[aux]= dieciseis[i];
aux++;
}
for(int i=0; i<veinte.length; i++)
{
datosdeNivel[aux]= veinte[i];
aux++;
}
for(int i=0; i<veinticuatro.length; i++)
{
datosdeNivel[aux]= veinticuatro[i];
aux++;
}
for(int i=0; i<veinticinco.length; i++)
{
datosdeNivel[aux]= veinticinco[i];
aux++;
}
for(int i=0; i<veintiocho.length; i++)
{
datosdeNivel[aux]= veintiocho[i];
aux++;
}
for(int i=0; i<uno.length; i++)
{
datosdeNivel[aux]= uno[i];
aux++;
}
for(int i=0; i<nueve.length; i++)
{
datosdeNivel[aux]= nueve[i];
aux++;
}
for(int i=0; i<ocho.length; i++)
{
datosdeNivel[aux]= ocho[i];
aux++;
}
You could try creating an ArrayList, containing all of your smaller arrays:
ArrayList<short[]> arrays = new ArrayList<>();
arrays.add(ocho);
arrays.add(veintiocho);
// ...
and then access indexes randomly, removing from the list each time you add to your large array:
Random rand = new Random();
while (!arrays.isEmpty()) {
int index = rand.nextInt(array.size());
short[] s = arrays.get(index);
for(int i = 0; i < s.length; i++)
{
datosdeNivel[aux]= s[i];
aux++;
}
arrays.remove(index);
}
This will add each array to the large array in a random order.
In pseudo code:
Put the arrays in a List
Shuffle the list using Collections.shuffle(list)
iterate over the list, filling the final array with elements of each array
If I were you, I'll give each array an index, then make a bucket of these indexes, then randomly pick up an index from the bucket, do your copy job, and remove this used index from your bucket.
having this issue with a decryption program I am writing in Java. Here is the code in question
public static int int_to_int(int input)
{
int[] value_array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26};
int[]bin_array= {00000, 00001, 00010, 00011,
00100, 00101, 00110, 00111,
01000, 01001, 01010, 01011, 01100,
01101, 01110, 01111, 10000, 10001,
10010, 10011, 10100, 10101, 10110,
10111, 11000,11001, 11010, 11011};
for(int i=0; i <27; i++)
{
System.out.println("hello");
if(input==value_array[i])
{
System.out.println("returning: " + bin_array[i] + "at: " + i);
return bin_array[i];
}
}
return -1;
}
And here is the issue highlighted in a line
double temp = 00010;
System.out.println("returning: " + temp);
This will output
returning: 8
but I want to see
returning: 00010
thoughts?
The 00010 is octal number, i.e., 8. Remove all the leading zeroes.
Integers prefixed with 0 are treated as octal, not binary. Prefix with 0b or 0B to indicate binary, like 0B00010. To print as binary, use
System.out.println("returning: " + Integer.toBinaryString(temp));
or,
System.out.println("returning: " + Integer.toString(temp, 2));
That is, assuming temp is an integer, like in your bin_array.
public static int int_to_int(int input)
{
int[] value_array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26};
int[]bin_array= {00000, 00001, 00010, 00011,
00100, 00101, 00110, 00111,
01000, 01001, 01010, 01011, 01100,
01101, 01110, 01111, 10000, 10001,
10010, 10011, 10100, 10101, 10110,
10111, 11000,11001, 11010, 11011};
for(int i=0; i <27; i++)
{
System.out.println("hello");
if(input==value_array[i])
{
System.out.println("returning: " + Integer.toOctalString(bin_array[i]) + "at: " + i);
return bin_array[i];
}
}
return -1;
}
I use Integer.toOctalString
I have a code but I want to avoid using java streams because streams are not supported in android. Here is my code:
import java.util.Arrays;
import static java.util.Arrays.stream;
import java.util.concurrent.*;
public class VogelsApproximationMethod {
final static int[] demand = {30, 20, 70, 30, 60};
final static int[] supply = {50, 60, 50, 50};
final static int[][] costs = {{16, 16, 13, 22, 17}, {14, 14, 13, 19, 15},
{19, 19, 20, 23, 50}, {50, 12, 50, 15, 11}};
final static int nRows = supply.length;
final static int nCols = demand.length;
static boolean[] rowDone = new boolean[nRows];
static boolean[] colDone = new boolean[nCols];
static int[][] result = new int[nRows][nCols];
static ExecutorService es = Executors.newFixedThreadPool(2);
public static void main(String[] args) throws Exception {
int supplyLeft = stream(supply).sum();
int totalCost = 0;
while (supplyLeft > 0) {
int[] cell = nextCell();
int r = cell[0];
int c = cell[1];
int quantity = Math.min(demand[c], supply[r]);
demand[c] -= quantity;
if (demand[c] == 0)
colDone[c] = true;
supply[r] -= quantity;
if (supply[r] == 0)
rowDone[r] = true;
result[r][c] = quantity;
supplyLeft -= quantity;
totalCost += quantity * costs[r][c];
}
stream(result).forEach(a -> System.out.println(Arrays.toString(a)));
System.out.println("Total cost: " + totalCost);
es.shutdown();
}
i would be grateful if anyone can help me with this because i can't understand how stream works.
Well that is quite easy, you can use for loop as Mike M suggested in his comment see examples below:
int supplyLeft = 0;
int[] supply = {50, 60, 50, 50};
for (int i : supply) {
supplyLeft += i;
}
System.out.println(supplyLeft);
to replace
int supplyLeft = stream(supply).sum();
if you don't want foreach either then
int supplyLeft = 0;
for (int i = 0; i < supply.length; i++) {
supplyLeft += supply[i];
}
System.out.println(sum);
As far as iterating through 2D array and replacing the java-8 stream as shown below is concerned
stream(result).forEach(a -> System.out.println(Arrays.toString(a)));
you can use a for loop or Arrays.deepToString() as per your needs see below example:
//assume your array is below, you can replace it with results everywhere
int[][] costs= { { 16, 16, 13, 22, 17 }, { 14, 14, 13, 19, 15 },
{ 19, 19, 20, 23, 50 }, { 50, 12, 50, 15, 11 } };
for (int i = 0; i < costs.length; i++) {
System.out.println(Arrays.toString(costs[i]));
}
Above for loop will print you the array in the following manner
[16, 16, 13, 22, 17]
[14, 14, 13, 19, 15]
[19, 19, 20, 23, 50]
[50, 12, 50, 15, 11]
If you use Arrays.deepToString(costs) then you get an output as shown below:
[[16, 16, 13, 22, 17], [14, 14, 13, 19, 15], [19, 19, 20, 23, 50], [50, 12, 50, 15, 11]]
Above to replace
This is a brute force attempt to solve the problem, but it is not giving the right answer. The program runs, but its not producing desired output. I believe the logic and program is correct.
This is problem of a famous site (don't want to a spoiler)
It asks for the number that produces the longest Collatz chain under one million.
class Euler
{
public static void main (String args[])
{
long len,longLength=0;
for(long i =3;i<=1000000;i++)
{
len = Euler14.numFucs(i);
System.out.println("Ans"+len+"\t"+i);
if(len>longLength)
longLength=len;
}
System.out.println(longLength);
}
public static long numFucs(long num)
{
long count=1,$test=0;
while(num>1)
{
if(num%2==0)
{
num=num/2;
}
else
{
num=3*num+1;
}
count++;
}
//System.out.println("\tEnd");
return count;
}
}
Well, I feel like it'd be cheating to give you the code for the right answer, as a fellow Project Euler fan. You're outputting the length of the longest chain, not the number that obtains it. If you really want, I can show you the one line that needs to change, but, honestly, this is a simple fix, and I challenge you to do it yourself.
If the program is suppose to compute the number of steps in the Collatz Conjecture the implementation looks fine to me.
The sequence of numbers in described by OEIS Sequence A008908.
Here's is your program together with some debug output.
class Test {
public static void main(String args[]) {
long len, longLength = 0;
System.out.println(Test.numFucs(13));
String[] correct = ("1, 1, 2, 8, 3, 6, 9, 17, 4, 20, 7, 15, 10, 10, 18,"
+ " 18, 5, 13, 21, 21, 8, 8, 16, 16, 11, 24, 11, 112, "
+ "19, 19, 19, 107, 6, 27, 14, 14, 22, 22, 22, 35, 9, "
+ "110, 9, 30, 17, 17, 17, 105, 12, 25, 25, 25, 12, "
+ "12, 113, 113, 20, 33, 20, 33, 20, 20, 108, 108, 7,"
+ " 28, 28, 28, 15, 15, 15, 103").split(", ");
for (int i = 0; i <= 70; i++) {
len = Test.numFucs(i);
System.out.printf("i = %2d, Correct %3s, Computed: %3d%n", i,
correct[i], len);
if (len > longLength)
longLength = len;
}
System.out.println(longLength);
}
public static long numFucs(long num) {
long count = 1;
while (num > 1) {
if (num % 2 == 0) {
num = num / 2;
} else {
num = 3 * num + 1;
}
count++;
}
// System.out.println(count);
return count;
}
}
Output:
i = 0, Correct 1, Computed: 1
i = 1, Correct 1, Computed: 1
i = 2, Correct 2, Computed: 2
i = 3, Correct 8, Computed: 8
i = 4, Correct 3, Computed: 3
i = 5, Correct 6, Computed: 6
i = 6, Correct 9, Computed: 9
i = 7, Correct 17, Computed: 17
i = 8, Correct 4, Computed: 4
i = 9, Correct 20, Computed: 20
i = 10, Correct 7, Computed: 7
i = 11, Correct 15, Computed: 15
i = 12, Correct 10, Computed: 10
i = 13, Correct 10, Computed: 10
i = 14, Correct 18, Computed: 18
i = 15, Correct 18, Computed: 18
i = 16, Correct 5, Computed: 5
...
As you can see it follows the OEIS sequence.
The error must be some where else.