+ 19

Learning to handle exceptions. Why this thing doesn't compile? Am I missing something or switch can't be used like that?

https://code.sololearn.com/cA2wPPSXVVsa/?ref=app

21st Mar 2019, 6:41 PM
1Lory☕
1Lory☕ - avatar
18 Respuestas
+ 3
1Lory☕ yes as Denise Roßberg suggested line 12 is missing ; as well as non visible characters in your code in lines 15, 31, 40 and 48. clear those lines from the begining and than your code will compile and work ;-)
21st Mar 2019, 9:42 PM
vlada
+ 20
Denise Roßberg, Jenine , thanks a lot for the help 😊 Nice to know that it should work, will try to fix the bugged lines Edit: it is working!😃
21st Mar 2019, 9:38 PM
1Lory☕
1Lory☕ - avatar
+ 19
vlada, thanks, fixed it :) I did save it into my text editor at first, before getting it back into playground to continue writing it, had no idea that it could cause compilation errors. Nice to know for the future
21st Mar 2019, 9:49 PM
1Lory☕
1Lory☕ - avatar
+ 18
Denise Roßberg thank you for your response, but at first I did have a Random generating numbers for switch, then I used int, which didn't help, afterwards I just put the value inside the switch directly to simplify things. I want to test each of those exceptions one by one, I have a general one in the default case as well. I have written this here in the playground, no idea where the invisible chars could be 😐
21st Mar 2019, 8:42 PM
1Lory☕
1Lory☕ - avatar
+ 17
Jenine I've been always doing it this way and it should be totally correct (or at least acceptable). Thank you for trying to help though, I really do appreciate that. Maybe switch just is not the best way to do this kind of thing.
21st Mar 2019, 9:27 PM
1Lory☕
1Lory☕ - avatar
+ 17
Denise Roßberg, I'm writing on my phone, in a text editor or here in the playground (mostly in both at the same time 😄)
21st Mar 2019, 9:51 PM
1Lory☕
1Lory☕ - avatar
+ 16
Jenine I hope I got right what you mean and I made it System.out.println(" " + value) now, but it still doesn't compile 🙁
21st Mar 2019, 9:02 PM
1Lory☕
1Lory☕ - avatar
+ 6
Line 12: Missing ; But I didn't found all bugs. So I wrote a new code which you can copy. Maybe you will find the bug if you compare your code with mine. https://code.sololearn.com/c7N34u5v9oGb/?ref=app
21st Mar 2019, 9:33 PM
Denise Roßberg
Denise Roßberg - avatar
+ 6
vlada Thank you very much! I was really sure that there have to be invisible characters. Can you tell me how did you find them? 1Lory☕ Did you write the code on smartphone or pc?
21st Mar 2019, 9:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 6
vlada I know this issue. But I don't know a way to find such characters (I'm using my smartphone). I think for the next time I have to use my ide on my laptop. 1Lory☕ Maybe you can change the formatting settings of your text editor. If I'm right your problem has to do with utf-8 (unicode).
21st Mar 2019, 10:03 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
* Int number = 1; //user input or random number Switch(number) * I think Exception e is enough. * Compilation error: I think that you copied your code from your ide --> there can be unvisible characters in it (unicode characters). Try to remove them.
21st Mar 2019, 8:31 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Denise Roßberg I am using InteliJ. I had same issues long time ago, and it is first thing I check when my working code doesn't compile on CP.
21st Mar 2019, 9:54 PM
vlada
+ 3
1Lory☕ I just came across this "invisible character" problem myself. Strange bug, lol. Denise Roßberg Linked me to this post.
3rd Apr 2019, 6:58 PM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar
+ 2
Switch statements combined with try-catches should be fine. You’ve got break statements in place and a default case. The point of a switch statement is to do any block of code for a given case which try-catches may be a part of As Denise Roßberg has just demonstrated, it works! (Sorry for the red herring)
21st Mar 2019, 9:35 PM
Jenine
+ 1
I thought it could be declaring int number = 1 too but technically it should still compile and run (as you are evaulating 1 is in the switch statement and then it will match case 1 It’s also best practice to narrow down your Exceptions in try-catches. Yes, you can try{} catch(Exception e) {} but this might mean you catch an exception that you didnt anticipate (so for example I may catch a NullPointerException in some code which I anticipate it could be but if the code throws a different Exception, I as the developer might want to know about it so it can affect how I write my main code) Good spot on the unicode characters though, I was scratching my head trying to figure out what it was 😅
21st Mar 2019, 8:39 PM
Jenine
+ 1
Check that the types you are passing into the print methods are strings. Java is strict with types and will have compilation errors where you have passed Byte and int objects where these methods expect strings 👍🏾
21st Mar 2019, 8:57 PM
Jenine
+ 1
Edit: Ignore! “” + value is fine 😊 That’s because there’s still a type issue there: you are trying to add a String to a non-String type I haven’t worked with Byte objects before (so I’d google how to convert a Byte to a String) but as for the int, you can do Integer.toString(int) and then that will be addable to another string as it is a string itself
21st Mar 2019, 9:18 PM
Jenine
+ 1
Yes, you’re right, I realised afterwards that the “” + value should be completely fine! It could be Bytes are a different case? But if you do want to test your theory on switch statements being the root problem, try translating that to if statements or just consequent blocks of code exploring the different Exceptions Good luck 👍🏾
21st Mar 2019, 9:30 PM
Jenine