Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exception code?  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get an exception, the stack trace that is printed on the console tells you exactly in which line of your code the exception happens. So, in what line does the exception happen?

When you know in what line the exception happens, you can probably see which method throws the exception. Lookup that method in the API documentation, it most likely explains under what circumstances and why it will throw that exception.

What values are you entering?
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exception is raised at k=s.next();.when i run the program in my pc no exception is raised but when i compile at hackerrank website it shows nosuchelementexception !help my code to get the ouput!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code the other day and did not suffer such an Exception. Please provide a link to that online JVM. I have seen a few of them and they usually do not compile or run Java® code correctly. One of the things I have seen wrong is Exceptions.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suggest me some other way to handle the string ?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete the whole code and start again.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It throws a exception when i gave input as 5!!!
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code, and although I have no idea what you're trying to achieve with it, it does not throw any exceptions when you enter 5.

As has already been stated, it's most likely a problem with that online compiler you are using.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with TC: use a JVM on your own machine.
That code is so confusing and difficult to use I would start from scratch myself.
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wen i run the code with input as 5 any compiler raises exception i tried most of it even on my pc!!!please try with input as 5!
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to read the replies to your questions. If you had done so then you will have seen that both Campbell and I have taken the time to try out your code on our own machines. Neither of us are observing the Exception condition that you describe, and I most certainly did test it entering 5 as an input, with no exceptions thrown.

There's something going on here that you're not telling us, because previously you said that it ran ok on your local machine, but not on the online compiler, and now you are saying that it doesn't run ok on either your local machine or the online compiler.

In general I would be suspicious of using an online compiler. I have never used one, nor have I plans to. I recommend you stick to your proper JDK installed on your own machine.

I think it's time for you to go back and review your program yourself to determine exactly what behaviour you are trying to achieve, and determine exactly how your program is not achieving it. I really have no idea what your program is trying to do so I cannot offer any further suggestions to make it 'work'.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works fine for me. Here's my run-time console:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!