I'm a student and currently learning java. From what I know, java reads codes from top to bottom, but my programs behaviour is confusing me. This is not the my complete program, I created a simpler program to emphasize the part that I don't understand.
I had two projects:
Problem.java
- my main project.
Procedures.java
- contain many public void methods, I just create this to reuse a set of codes. Example: GetAllDigits()
- this is to get every digits/char of a String and store inside an ArrayList.
As you can see after I pass variable account
to GeAllDigits()
I immediately parse the values to String AllDigits
(this is to save the first result. I did this because I know I will call the method again and will have an another result).Then I store the second result inside variable ThreeDigits
.
So my expected output should be:
All Digits: [2, 0, 1, 0, 0, 5]
Three Digits: [2, 0, 1]
But instead I get:
All Digits: [2, 0, 1]
Three Digits: [2, 0, 1]
String account = "201005";
ArrayList<String> AllDigits = new ArrayList<>();
ArrayList<String> ThreeDigits = new ArrayList<>();
String num = new String();
Public Problem()
{ int length = account.length();
Procedures proc = new Procedures();
proc.GetAllDigits(account);
AllDigits = proc.digits;
for(int x=0;x < (length-3);x++)
{
num += AllDigits.get(x);
}
proc.GetAllDigits(num);
ThreeDigits = proc.digits;
System.out.println("All Digits: " + AllDigits.toString());
System.out.println("Three Digits: " + ThreeDigits.toString());
}
public static void main(String[] args) {
new Problem();
}
This is what Procedures.java looks like:
public ArryList<String> digits = new ArrayList<>();
public void OddEvenDigits(String number)
{...
}
public void GetAllDigits(String acc){
digits.clear();
for(int i =0; i < acc.length(); i++)
{
int j = Character.digit(acc.charAt(i), 10);
digits.add(Integer.toString(j));
}
}
Sorry for my long post regarding such a simple problem. Any answer would be greatly appreciated, and if there are any sites/books that you can refer me to for learning that would be awesome!