Home > front end >  Adding spaces between operation marks
Adding spaces between operation marks

Time:12-31

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:

https://i.stack.imgur.com/sraZc.png

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

CodePudding user response:

"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.

CodePudding user response:

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".

  • Related