In this program below:
public class medianTemp {
public static void main(String[] args){
int length = args.length;
int[] n = new int[length];
n[0] = Integer.parseInt(args[0]);
System.out.print(n[0] + " ");
for (int i = 1; i < length; i++ ){
String c = args[i];
if (c.equals(".")){
n[i] = n[i-1] + 0;
System.out.print(n[i] + " ");
}
if (c.equals("+")){
n[i] = n[i-1] + 1;
System.out.print(n[i] + " ");
}
if (c.equals("-")){
n[i] = n[i-1] - 1;
System.out.print(n[i] + " ");
}
Inside the for
loop and inside the if
statements. If I use for example args[i] == "."
(instead of converting args[i]
to string
), the code above doesn't work and only the initial integer is displayed. Can someone please tell me why this happens?