Move element of array to specific position [duplicate] - java

This question already has answers here:
Java: moving items in array
(6 answers)
Closed 6 years ago.
How to move element of array to specific position on Android JAVA.
We have
int oldPosition, int newPosition
and some like
JSONObject[] tmp = new JSONObject[999];
array of JSONObjects

If you want to just move
tmp[newPosition]=tmp[oldPosition];
Swap
JSONObject jo= tmp[oldPosition];
tmp[oldPosition]=tmp[newPosition];
tmp[newPosition]=jo;

EDIT: There are other ways but you can go through this as well to get your result :)
Exercise : You can understand this logic and use JSONObject type and do necessary changes, Watch out for NullPointerExceptions , handle everything i am lazy to do them
let's say you have int array --> private int array[];
array = new int[]{10,20,30,40,50,60,70,80,90,100};
call this method if you want to swap elements,
swapNumbers(array,9,1);
.
public int[] swapNumbers(int [] arr, int possition1, int possition2){
int temp = arr[possition2];
arr[possition2] = arr[possition1];
arr[possition1] = temp;
System.out.println("array -->" + Arrays.toString(array));
return arr;
}
out put : array[10, 100, 30, 40, 50, 60, 70, 80, 90, 20]
But that doesn't satisfy you?
you need out put to be like this : array[10, 100, 20, 30, 40, 50, 60, 70, 80, 90]
you can use below method
resetUpMyArray(array, array[9],1);
System.out.println("array Finally changed-->" + Arrays.toString(array));
enjoy,
public int[] resetUpMyArray(int[] inputArray, int deleteMeValue,int addMePosition) {
List resultLinkedList = new LinkedList();
for (int itemValue : inputArray)
if (deleteMeValue == itemValue) {
System.out.println("Do not add this value"+itemValue);
} else {
System.out.println("Do add this value "+itemValue +"position-"+deleteMeValue);
resultLinkedList.add(itemValue);
}
System.out.println("array -as new L.L->" + resultLinkedList);
resultLinkedList.add(addMePosition,deleteMeValue);
System.out.println("array -as new L.L all set->" + resultLinkedList);
array = new int[resultLinkedList.size()];
for (int i = 0; i < resultLinkedList.size(); i++) {
array[i] = (int) resultLinkedList.get(i); // Watch out for NullPointerExceptions!
}
return array;
}

Here is two simple algorithm.
Switch values :
switch(array, from, to)
tmp = array[to]
array[to] = array[from]
array[from] = tmp
That will give something like
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|--<---->--|
[10, 20, 60, 40, 50, 30, 70, 80, 90, 100]
This will simply store the values that will be replace at the index to to be place at index from
Move and shift values:
This one will move one values a shift the values next.
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|------------^
[10, 20, , 40, 50, 60, 30, 70, 80, 90, 100]
<-----------
[10, 20, 40, 50, 60, 30, 70, 80, 90, 100]
For this, the solution is quite the same, store the values in tmp but shift every value to fill the gap. This code will only work if from < to
tmp = array[to]
i = from
while(i < to)
array[i] = array[i+1]; --Shift the value
i = i + 1
array[to] = tmp;

Related

Getting frequencies and magnitudes from audio sample using FFT

I am currently working on a music visualization LED strip display (similar to this) but got stuck at the process of extracting audio frequency / magnitude from the sample data.
The entire process takes up quite a few steps, and I am not sure at which point I am failing exactly, so please bear with me. I start with reading audio from a TargetDataLine configured with AudioFormat.
AudioSystem.getTargetDataLine(new AudioFormat(8000.0f, 16, 1, true, true));
Next, I read the data as described in this tutorial. Only difference is that I am using a smaller buffer, effectively reading 64 bytes at a time.
Here is an example data read in this way from a 500Hz test tone.
final byte[] buffer = { // 32 16-bit samples
30, 111, 43, -19, 50, -74, 49, -54,
41, 70, 26, 121, 7, -90, -13, -89,
-31, -118, -44, 17, -51, 75, -50, 60,
-42, -62, -27, -115, -8, 91, 12, 85,
30, 106, 43, -34, 50, -93, 49, -76,
41, 52, 26, 108, 7, -93, -13, -84,
-31, -104, -44, 38, -51, 98, -50, 84,
-42, -42, -27, -98, -8, 99, 12, 83
};
Next, I transform the sampled bytes into an array of double values in range [-1, 1]. I took inspiration from the aforementioned tutorial and this code.
final double[] samples = new double[buffer.length];
int sampleIndex = 0;
int byteIndex = 0;
while (byteIndex < buffer.length) {
int low = buffer[byteIndex];
++byteIndex;
int high = buffer[byteIndex];
++byteIndex;
int sampleIntValue = ((high << 8) + (low & 0x00ff));
double maxSampleValue = 32768;
samples[2 * sampleIndex] = ((double) sampleIntValue) / maxSampleValue; // Transforming to [-1, 1]
samples[(2 * sampleIndex) + 1] = 0; // Imaginary part
++sampleIndex;
}
Next, I perform a Fast Fourier Transform on the data using the edu.emory.mathcs:JTransforms:2.4 library.
final DoubleFFT_1D fft = new DoubleFFT_1D(samples.length / 2);
fft.complexForward(samples);
Then, I calculate frequencies and magnitudes as described in this code.
final float sampleRate = 8000f;
final Map<Double, Double> frequenciesToMagnitudes = IntStream.range(0, samples.length / 2)
.boxed()
.collect(Collectors.toMap(
index -> 2 * ((double) index / (double) samples.length) * sampleRate,
index -> Math.log10(
Math.sqrt(
Math.pow(samples[2 * index], 2) // real part
+ Math.pow(samples[(2 * index) + 1], 2) // imaginary part
)
)
));
I find the maximum magnitude so I can scale the displayed values accordingly.
final double maximumMagnitude = frequenciesToMagnitudes.values().stream().max(Double::compare).orElse(0d);
And finally, I display the results (taller block represents a brighter LED).
final char[] magnitudeDisplay = {'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'};
frequenciesToMagnitudes.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(frequencyToMagnitude -> {
final double magnitudePercentage = frequencyToMagnitude.getValue() / maximumMagnitude;
final int characterToDisplayIndex = Math.min(magnitudeDisplay.length - 1, Math.max(0, (int) (magnitudePercentage * magnitudeDisplay.length)));
final char characterToDisplay = magnitudeDisplay[characterToDisplayIndex];
System.out.print(characterToDisplay);
});
System.out.println();
From all this, I would expect to see two distinct spikes (my frequency + alias region), but instead there is 8.
▁▁▅▁▁▁█▁▁▁▄▁▁▁▅▁▁▁▅▁▁▁▄▁▁▁█▁▁▁▅▁
The number of spikes changes depending on frequency of audio playback (sometimes it's two, sometimes it's four), and the lower the frequency the more spikes I see.
My question is: how do I extract frequency / magnitude pairs from audio data?
After a bit of digging I have found the OpenIMAJ library which does exactly what I need. Here are the differences in my code and their code.
Transforming from bytes to floating point is incorrect. All examples use big endian audio format, but take the bytes in the wrong order. It should be:
int high = buffer[byteIndex];
++byteIndex;
int low = buffer[byteIndex];
++byteIndex;
Size of FFT output is padded to next power of 2. This was not a problem since I always chose a buffer of size that was a power of 2, but in case if your buffer is not, this will be necessary.
final int fftSize = (int) Math.pow(2, 32 - Integer.numberOfLeadingZeros(numberOfSamples - 1));
The values are transformed to floating point a bit differently.
samples[2 * sampleIndex] = (float) sampleIntValue * Integer.MAX_VALUE / Short.MAX_VALUE;
Real parts are normalized.
for (int i = 0; i < samples.length; i += 2) samples[i] /= fftSize;
No logarithms are used for fetching magnitudes.
final Map<Float, Float> frequenciesToMagnitudes = IntStream.range(0, samples.length / 4)
.boxed()
.collect(Collectors.toMap(
index -> (((float) (index * 2)) * (sampleRate / (float) numberOfSamples)) / 2,
index -> {
final float realPart = samples[index * 2];
final float imaginaryPart = samples[index * 2 + 1];
return (float) Math.sqrt(realPart * realPart + imaginaryPart * imaginaryPart);
}
));
After all the changes, the output appears to be much more reasonable:
▁▁▂▁▁▁█▁▁▁▂▁▁▁▂▁
I am still not entirely sure if the frequency calculation is correct, though.

search 200 k array of arrays of integers?? java

I have array containing 200 000 lines of integers ( containing smaller arrays of integers). Each line contains 1 - 20 members. Integer are 0-200 in value. Looks like this:
...
[42, 66, 54, 145, 183, 198, 104, 24, 22, 125, 127]
[71, 149, 59, 147, 115, 36, 124] // <--lets say i am searching for this line
[27, 141, 19, 75, 101, 149, 36, 7, 12, 108, 69, 149, 1, 39, 55, 87, 178, 76, 133]
[94, 170, 185, 17, 121, 42, 51, 70, 176, 187, 31, 181, 167, 200, 144, 126, 123, 120, 91, 40]
[112, 162, 173, 145, 0, 165, 106, 137]
[141, 198, 32]
...
How to search for particular line in it? Please direct me to right direction, if possible link me to example.
I mean search - when i type in 71, 149, 59 i would get my line (or close to it if there are several similar lines).
Spend days reading around still not sure how to proceed. Please help. (I have list sorted ascending)
which method is better, hashing? binary search? Any good keyword or link appreciated (first time i am doing search)
I suspect you are trying to optimise your search prematurely. Unless you are going to do it many times a second then an exhaustive search should be fine. I'm assuming by 'close to it' your intention is to find all arrays that start with the values you pass in.
Here is an exhaustive search using Java 8:
List<int[]> searchForArrays(int[][] data, int[] value) {
return Arrays.stream(data).parallel()
.filter(line -> {
for (int i = 0; i < value.length; i++)
if (i >= line.length || value[i] != line[i])
return false;
return true;
});
.collect(Collectors.toList());
}
This finds all arrays that match your search term. If you need to find just one array that matches and your array is sorted then you could speed things up with a binary search:
int[] binarySearch(int[][] data, int from, int to, int[] value) {
int trial = (from + to) / 2;
if (from >= to)
return new int[]{};
int compare = compareTo(data[trial], value);
if (compare < 0)
return binarySearch(data, from, trial, value);
else if (compare > 0)
return binarySearch(data, trial, to, value);
else
return data[trial];
}
If you really need to optimise then you'd be better off reorganising your data into a tree with a map from values to nodes. Then searching would be a trivial matter of following the nodes of the tree for the values you are searching for. This could look something like:
class Node {
private final Map<Integer, Node> children;
private boolean terminal;
}
How about something like below, I can flesh it out later if it suits:
package area51;
import java.util.ArrayList;
import java.util.List;
public class Junk {
Node root;
static void main(String[] args) {
}
public Node initialize() {
root = new Node(0);
int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
for (int[] row : matrix) {
Node parent = root;
for (int childValue : row) {
parent = parent.addChild(childValue);
}
}
return root;
}
public List<int[]> find(int[] key, int limit){
//use some recursion
List<int[]> list = new ArrayList<int[]>();
Node node = root;
for (Node child: node.children){
....
}
return list;
}
class Node {
int value;
List<Node> children;
Node(int value) {
super();
this.value = value;
}
Node addChild(int childValue) {
if (children == null) {
children = new ArrayList<Node>();
}
Node child = new Node(childValue);
children.add(child);
return child;
}
}
}

Processing mapping always out of bounds

I'm trying to make a graph in processing, but keep getting the "ArrayIndexOutOfBounds" error. I am having a hard time understanding the concept of the map() syntax, could anyone clear it up for me? I'm not sure what every number is supposed to stand for within map(x,y,z,a,b);
String[] name = {
"1st:",
"5th:",
"10th:",
"15th:",
"20th:",
"25th:",
"30th:"
};
int[] temperature = {
81,
82,
84,
85,
87,
88,
90
};
void setup(){
size(200,200);
}
void draw(){
int x=0;
for(int i=0;i<10;i++){
if(mouseX>x && mouseX<=x+40){
fill(255,40,40);
}else{
fill(50);
}
float h = map(temperature[i], 0, 100, 0, 200);
rect(x+4,height-h,32,h);
x+=40;
}
}
Your arrays have 7 elements, but you iterate 10 times.
for(int i=0;i<10;i++){
...
float h = map(temperature[i], 0, 100, 0, 200);
}
Either you fill up your arrays with additional 3 elements, or better: Instead of the number 10 you should use i < temperature.length() as condition in for-loop.

Parallel sieve of Eratosthenes - Java Multithreading

I wanted to write sieve of Eratosthenes which will work using specific number of threads. I figured out, that it will work in following way:
For 2 threads up to 17.
Thread-1 takes 2, and starts to remove multiple of 2 from List. Parallel Thread-2 takes 3 and does the same. After that Thread-1 takes 5( because there is no 4 in List) and Thread-2 takes 7 and so on until they reach end.
I wrote following piece of code:
private List<Integer> array = new ArrayList<Integer>();
private List<Integer> results = new ArrayList<Integer>();
public synchronized void run(){
while(array.size() > 0){
Integer tmp = array.get(0);
for(int i = 1; i < array.size(); i++){
if( (array.get(i).intValue() % tmp.intValue()) == 0)
array.remove(i);
}
results.add(array.get(0));
array.remove(0);
}
}
public void setArray(int x){
for(int i = 2; i < x; i++)
array.add(Integer.valueOf(i));
}
public void printArray(){
for(Integer i: results){
System.out.println(i);
}
}
This code works, but I added time measurement "tool" to my main class:
ThreadTask task = new ThreadTask();
task.setArray(5000);
Long beg = new Date().getTime();
for(int i = 0; i < 3;i++){
new Thread(task).start();
}
Long sleep = 1000L;
Thread.sleep(sleep);// I am sleeping main thread to wait until other Threads are done
task.printArray();
System.out.println("Time is "+(new Date().getTime()-beg-sleep));
The problem is that running this with 2 threads is slower than running with 1 thread, and 3 threads are slower than 2 threads. Could anyone explain me, why?
EDIT:
There is one important thing about that. I don't need it to be done as fast as it can be. I need it working on Threads for one reason. My Teacher wants to compare runtimes of running same program with 1, 2 .. n threads. Results should look like in this graph.
EDIT2:
I have rewritten code to following
private HashMap<Integer,Boolean> array = new HashMap<Integer,Boolean>();
private int counter = 1;
private int x;
public void run(){
while(counter < x-1){
do{
counter++;
}
while( array.get(counter));
int tmp = counter;
for(int i = tmp; i < array.size(); i+=tmp){
if( i!= tmp)
array.put(i,true);
}
try{
Thread.sleep(0L, 1);
}
catch (Exception e){}
}
}
public void setArray(int x){
this.x = x;
for(int i = 2; i < x; i++)
array.put(i, false);
}
public void printArray(){
for(int i = 2; i < array.size();i++){
if( !array.get(i))
System.out.println(i);
}
}
Now it uses HashMap and this is how it works:
Fill HashMap with keys from 2 to n and false values.
New thread goes into while loop which is based on counter variable. Counter represents current key.
Increment counter on the begging so new threads doesn't operate on counter of earlier started thread.
Put counter value into temporary variable tmp so we can work even when another thread increment counter
Iterate through the HashMap by incrementing i with tmp ( it is actually jumping on the multiplies of i) and set their values to true.
All keys which has true value are ignored in print method. Also counter skips them when incremented.
The problem is that it still works slower with more threads. What's wrong now?
The mistake is simpler than I first thought. All your threads are doing the same thing so each thread does more work. To make a multi-threaded program work faster you have to divide up the work, which has to be performed concurrently.
When you have one thread accessing a data structure, it can be in the fastest cache of one core, use multiple threads and they need to co-ordinate their actions and since most of the work is updating the data structure, a lot of time is lost as overhead. This is the case even though your data structure is not thread safe and is likely to have a corrupted result.
BTW updating an ArrayList is very expensive and using a collection objects is also an overhead.
You will get a much faster result using a BitSet and just one thread.
public class BitSetSieveMain {
private final BitSet set;
private final int size;
public BitSetSieveMain(int x) {
size = x + 1;
set = new BitSet(size);
set.flip(2, size);
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
BitSetSieveMain bitSetSieveMain = new BitSetSieveMain(5000);
bitSetSieveMain.sieve();
long time = System.nanoTime() - start;
System.out.println(time / 1000 + " micro-seconds to perform " + bitSetSieveMain);
}
}
public void sieve() {
int i = 2;
do {
for (int j = i*2; j < size; j += i)
set.clear(j);
i = set.nextSetBit(i+1);
} while (i > 0);
}
public String toString() {
return set.toString();
}
}
finally prints
87 micro-seconds to perform {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999}

Trouble with creating a string array of int arrays

I need to create an array of integers contained within an array of words. I keep getting an error
cannot convert from int[] to java.lang.String[]
where it says
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
This is my code
public class Budget{
///////////////fields////////////////
int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000};
int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.
int month_num;
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
/////////constructors///////////////
public Budget() {}
public Budget(int name) {this.expenseName[i][i] = name;}
public Budget(String name, int clothes, int tuition, int transportation, int food, int housing, int books)
{
this.expenseName[i] = name;
this.clothes[i] = clothes;
this.tuition[i] = tuition;
this.transportation[i] = transportation;
this.food[i] = food;
this.housing[i] = housing;
this.books[i] = books;
this.monthNumber[i] = month_num;
}
/////////////methods///////////
public String getexpenseName() {return expenseName[i][i];}
public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}
public int getmonthNumber() {return monthNumber[i];}//change the i
public void setExpenseName(String name)
{
this.expenseName[i][i] = name;
}
public boolean setexpenseAmount(int num)
{
if (this.expenseAmount[i] == 0)
{this.expenseAmount[i] = num;
return true;
}
else
return false;
You need quotes around your strings:
private String[] expenseName = {"clothes", "tuition", "etc"};
or you need to declare it int[][]
private int[][] expenseName = {clothes, tuition};
As the error message clearly states, you can't put an int[] into an array of String[]s.
Change
private String[][] expenseName = {clothes, tuition, transportation, food, housing, books};
to
private int[][] expenseName = {clothes, tuition, transportation, food, housing, books};
In Java, you cannot use an array of int as an array of String.
That will just not work, like the error says.
I guess that you must be familiar with some language where there is no type checking and you can do whatever you want. But this is Java.
You have to convert the int[] to String[], for example like this.
public class ArrayIntToString {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3 };
String[] numbersAsStrings = new String[numbers.length];
int i = 0;
for (Integer number : numbers) {
numbersAsStrings[i++] = number.toString();
}
}
}

Categories