Whats the difference between these two operations?
I imagined:
length - 1
to be the same as
length-1
but I am getting an error with length when using the latter
using operation here: for (int i = length - 1; i >= 0; i--)
int length = Integer.parseInt(scan.nextLine());
Edit: ENTIRE CODE:
import java.util.Scanner;
import java.util.Arrays;
public class continuousmedian
{
public static void main(String args[])
{
Scanner scan = new Scanner (System.in);
int cases = Integer.parseInt(scan.nextLine());
for (int set = 0; set < cases; set++)
{
int length = Integer.parseInt(scan.nextLine()), med = 0;
String[] copy = (scan.nextLine()).split(" ");
int[] test = new int[length];
for (int i = length-1; i >= 0; i--)
{
test [i] = Integer.parseInt(copy[i]);
//System.out.println("4 is " + test[4]);
Arrays.sort(test);
//*
System.out.print(">>");
for (int a = 0; a < length; a++)
System.out.print(test [a]);
System.out.println();
//*/
//System.out.println(i);
int mid = length - 1 - i;
if (i%2 == 0)
med += test[mid];
else
med += Math.floor((test[mid] + test[mid+1])/2);
//System.out.println("TEST AT " + mid + " is " + test[mid]);
}
System.out.println(med);
}
}
}
input case:
2
6
1 3 6 2 7 8
7
1 3 6 2 7 8 5
and finally, the error message:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 3 6 2 7 8"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:784)
at continuousmedian.main(continuousmedian.java:13)
For those of you saying that it is trying to parse a line of input:
The ONLY thing I changed is length-1 -> length - 1

"They are the same. Coding conventions say to use spaces, but the semantics are the same." - Ole
This was correct. Turns out, it didn't matter which of the formats I chose. In the end, it was completely 50/50 when running the program whether it worked or not. I ran the program a few times, copy pasting the same input into the same program. Sometimes it worked, sometimes it didn't. Maybe something with my computer clock or something, Idk but thanks for the help anyways.