I'm writing a very simple method for my programming class and ran in to an issue I can't find the resolution for in my notes.
You see I'm supposed to create a method that generates an array of an arbitrary length that has each consecutive value be a multiple of the last (interest rates). The problem is I can't find why my code doesn't work, it compiles but doesn't print what I'd like.
Instead of printing an array with something like (made up values):
[1, 5, 25, 125]
It prints out obscure text like:
[D#64bd4e3c or [D#7041a12f
Can someone please help? Below is a link to an image of my code:
My Code
System.out.print(Statements)
is essentially System.out.println(Statements.toString()) ;
It prints the address that Statements points to.
[D#64bd4e3c" or "[D#7041a12f" As you may have observed changes because the location of the array in memory changes. Hence the address is different or may be the same if reused.
You need to iterate through Statements.
In pseudocode:
for i to Statements.length
print Statements[i]
Here is a nice link to help you.
For printing arrays in java, use:
System.out.println(java.util.Arrays.toString(Statements));
Hope this helps!
Related
I'm French so excuse my English not necessarily correct.
I explain the context, I currently have a String array list named "tempCustomerDrugsIdsList" (var1) and another string array list named "tempDrugsTableList"(var2).
When I make a loop "For" on "var1" then another one in "var2","var2" loses its format, i. e. upper case is replaced by lower case and spaces are deleted.
I tested with another loop with the same type of variables (but empty), the result being the same I think the problem comes from my way of using java. Being on vb. net before, I must have taken some bad habits !
I don't know how to solve this problem, I've only been working in java for 2 weeks.
Thank you for helping me.
[EDIT]
My problem was:
List<String[]> tempDrugsTableList = otherList;
But this code doesn't duplicate the list.
AxelH gave me the following solution:
List<String[]> tempDrugsTableList = new ArrayList<String[]>(otherList);
Well, you are not doing a "copy" of the list
tempDrugsTableListCopy = tempDrugsTableList; // Get copy of original tempDrugsTableList for comparate
but sharing the reference, every update done in the tempDrugsTableListCopy will be done in the original list (same reference, same adress in memory). Since you are updating that copy in the following loops ... you update the original list too. What you want is to clone the list.
You could do it simply with copyList = new ArrayList(originalList); or for a deep clone, you need to iterate each element to duplicate those. (array need to be duplicated too if you change the value in those)
"String[]" tmpCustomerIds means you are getting a string array from a string array, which you would be using in a 2d array. Try it with just "String" in the for each loops. I am assuming you are using 1d arrays in this case.
I know if I run a program, it will likely express one way or another: what it is expecting from each variable. But I would like to determine on my own when I read over each page of Android code etc. e.g:
How could I determine what size or length an android program is expecting a string array to be?
Whether an integer or double, is expected to be positive or negative?
etc.
Help in this regard would be much appreciated.
You can set breakpoints in your code and examine all of the variables when the program pauses. This would give you a general idea of whether the integers were positive or negative, the length and content of the strings, etc. It could be useful if the code was poorly documented.
Assuming you are using Android Studio you can follow this guide:
https://developer.android.com/tools/debugging/debugging-studio.html
I have been thinking to solve any problem like 1+2*4-5 with user entering it and program to solve it. I've read some questions on this site about storing arithmetic operator and the solution says to check by using switch which can't be applied here. I would be thankful if anybody could suggest any idea of how to make it.
I had a similar exercise not long ago, but in the question it was stated that the seperation is a space. So the user input would be 1 + 2 * 4 - 5, and i solved it that way. I will give you some tips but not paste the whole code.
-you read the input as a String
-you can use the String.split() method to devide the String into the pieces you need and they will be put in an array.(in this case: strArray[0]='1',strArray[1]='+', etc)
-you will need a for-loop to go trough every String in the array:
-the decimals will need to be converted to integers with the Integer.parseInt() method.
-The + - * / will need to be put in switch-statement.
(be careful how you construct your loop, think about how many times you want to go trough it and what you need in each loop)
I hope these tips helped.
For a project that I'm currently working on I am dealing with a list of lists of integers, something of the form:
{[1,2];[5];[3,6,7]}
The idea here is that I'm trying to resolve an n-dimensional array into a list of the local maxima that occur in whatever particular axis I happen to be looking at. My question is this: I would like to get out a list of what would essentially be points in this n-dimensional space that contains every possible combination of entries of this list. For example, I would want the above to return:
{[1,5,3];[1,5,6];[1,5,7];[2,5,3];[2,5,6];[2,5,7]}
With the ordering not actually mattering to me. My first idea in how to approach this would be to boil this down to a tree where each path represents a possible combination and outputting every possible path, but I'm really not sure if this is the best way of going about it, and I am unfamiliar enough with Java's tree classes to be unsure if this would actually be straightforward to implement or not. Ideas?
Ah, my mistake, totally a duplicate.
Here is the location of the source code (using Dropbox).
The problem is in the fact that it doesn't evaluate zeros properly.
For example: x^2-2x-8 should equal the zeros of {-4, 2}, but instead I get a long exponential value like -4+34534....E-25<i>i</i>.
It does work for polynomials with single roots (such as x<sup>2</sup>+4x+4, root = {-2})
Can anyone spot the problem, it's been frustrating me for weeks. This is NOT a homework assignment, this is something I work on in my free time.
I've run into problems like this before and decided to switch to a different (math-oriented) language. You could try using floats instead of doubles, which may do the trick, but would probably bear problems of their own. Or you could write a method that filters out anything smaller than 1E-10 or something along those lines. Another alternative (which may or may not be relevant here) would be to use JLink.