Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

unreachable code  RSS feed

 
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hell guys! This is my first post in this forum.The following code on compilation shows  :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable code
             at A.main(A.java:60)

Can anyone help me out what is the problem?
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't mind me asking, but where do all the nextInt's come from ?
Without any context of what you're trying to do it's really gonna be hard to help.
Also why do you start an infinite loop, just in the hope of breaking out of it?
You also should have a look at The Java Naming Conventions
 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inputs will come form my machine. Sir,if you think i write something terrible in my code please help me out as i am new to programming.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inputs normally do come from your machine, but at no point you ask the user to put something in
 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the above code is for a  deciding a game winner.
Where , t=no of test cases
           N=sequence of integers
           a=no selected by Bob
           b=no selected by Alice
Game will start from Bob and then Alice, alternate turns will occur in which both have to remove multiples of  no selected by them one by one . if  one is  not able to remove  multiple of their selected no the other will win.
 
Sheriff
Posts: 21641
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The outer loop of this pair is an infinite loop. It can only end by using a break, return or throw statement. You do have some break statements, but these are all located inside the inner loop. As a result, these break out of the inner loop. That means the outer loop will still continue forever.

If you want to break out of the outer loop using a break statement in the inner loop, you need to use a labeled break.
 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:The following code on compilation shows  :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable code
             at A.main(A.java:60)


The compiler is complaining because for(;;) is an infinite loop and the code execution can not progress past it.

 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You ! you solve my problem .I found my mistake and problem resolved.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:
Also why do you start an infinite loop, just in the HOPE of breaking out of it?


I actually already maded that remark
 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well  after one player's turn i have to start iterating the arrayList again from 0 index to check whether any multiple of no selected by the current player is present or not in list. and so on this process will repeat as many times, until winner will not be decided .
To overcome this problem i think let's use the infinite loop and when winner will be decided ,loop will be broke by break.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an while or do whileloop using a boolean would be cleaner...
 
Yup, yup, yup. Tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!