0

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:

example sessions - why not working the second time?

The ONLY thing I changed is length-1 -> length - 1

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Epic Coder
  • 19
  • 6
  • 1
    Just image you were someone who never read your code and you would get this question. Would you know what "length - 1"? Is that user input, or a variable? What kind of variable? Would you know which error is meant? And now think about what kind of mind reading you expect from us with the few information bits you've given us :( – Tom Dec 30 '22 at 21:43
  • 2
    They are the same. Coding conventions say to use spaces, but the semantics are the same. – Ole V.V. Dec 30 '22 at 21:43
  • 2
    A [mre], please. Also because I can't make sense of how your sparse code snippets fit together. – Ole V.V. Dec 30 '22 at 21:45
  • I wrote "using operation here: for (int i = length - 1; i >= 0; i--)," its part of a for loop. Length is supposed to be parsing one line that only contains an integer. – Epic Coder Dec 30 '22 at 21:45
  • Thanks for the added precision. Please edit it into your question. The next users reading the question are unlikely to go through the comments, so you will want to keep everything together in the question itself. – Ole V.V. Dec 30 '22 at 21:48
  • 2
    @EpicCoder What is the exact error message you are getting? – OH GOD SPIDERS Dec 30 '22 at 21:48
  • And welcome to Stack Overflow. Please don't worry if you think we're critical, this is normal here. And it does take a bit to learn to use the site in a good way. Thanks for responding above. Please keep on. – Ole V.V. Dec 30 '22 at 21:52
  • Given code works fine with the input: https://ideone.com/RSoWAi – Tom Dec 30 '22 at 21:53
  • 3
    Your error message reads like you didn't actually input "2" or "6" first, thus "1 3 6 2 7 8" is read as your second input for line `int length = Integer.parseInt(scan.nextLine())` (which is line 13). – Tom Dec 30 '22 at 21:56
  • 1
    That error message has nothing to with spaces in `length -1`. You are trying to parse an entire input line as an integer. You can't do that. You need to parse each number separately. – Ole V.V. Dec 30 '22 at 21:58
  • 1
    @OleV.V. yes my code is trying to parse input line as an int, but that is not what I am telling it to do. In my code, I scan the first line independently, then lines after that in pairs of 2. So, sometimes it works, sometimes it doesn't. – Epic Coder Dec 30 '22 at 22:02
  • I don’t know jGrasp and start to wonder whether it has got a bug where it doesn’t always pick up the first line of input when pasting multiple lines. A search did not confirm, though. Are there three spaces before the first number in the string that Java is complaining about in your image? Fishy. Cannot rule out that it’s related. Your sorting doesn’t work for negative input, I don’t know whether it would be a requirement. Also your median calculation is incorrect, but you will get around to that. Thanks for the credit in your answer (happy to help). – Ole V.V. Dec 31 '22 at 07:23

2 Answers2

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.

Epic Coder
  • 19
  • 6
0

This line is your problem:

     int length = Integer.parseInt(scan.nextLine()), med = 0;
    

If the input line is "1 3 6 2 7 8", then you attempt to parse that line as an integer, which it manifestly is not. An exception is thrown indicating you're applying parseInt to invalid data.

We can tell this with absolute certainty - the exception message says so. You were trying to apply parseInt to the string "1 3 6 2 7 8". This is not in doubt.

It looks like that particular line of input was intended for the next line of code, which will split it on spaces, but in fact you never get there because of the previous error.

Given your statement that the input was supposed to be

2
6
1 3 6 2 7 8
7
1 3 6 2 7 8 5

then it looks like you didn't actually type one of the "2" or the "6".