The Artima Developer Community
Sponsored Link

Java Answers Forum
Whats a 'unreachable statement'?

1 reply on 1 page. Most recent reply: Jul 19, 2003 1:32 PM by mike

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
goyee

Posts: 1
Nickname: acee
Registered: Jul, 2003

Whats a 'unreachable statement'? Posted: Jul 19, 2003 10:16 AM
Reply to this message Reply
Advertisement
I am making a Treemap in which the user is prompted for a word and then a definition. These are stored in the Treemap. Later the user is prompted to enter a word, and the treemap goes in to look for the word. If found, the definition of the word is returned. Here is what i have so far:


import java.util.*;
 
public class MapTree
{
  public static void main(String[] args)
  {
    Terminal t = new Terminal();
 
    TreeMap theMap;
    theMap = new TreeMap();
 
    String b;
    String c;
    while (true)
    {
      b = t.readLine("Enter key: ");
      c = t.readLine("Enter value: ");
     theMap.put("b", new String(c));
     }
 
     Iterator iter = theMap.keySet().iterator(); //@@@@@@//
 
     while (iter.hasNext()) {
       b = (String) iter.next();
       c = (String) theMap.get(b);
       // do something with key and value.
 
     }
 
 
 
 
 
   }
}


My problem lies at the line: Iterator iter = theMap.keySet().iterator();

When i try to compile, i get: $ javac MapTree.java
MapTree.java:22: unreachable statement
Iterator iter = theMap.keySet().iterator();
^
1 error


How do i fix that?

Thanks.


mike

Posts: 4
Nickname: mikez
Registered: Jul, 2003

Re: Whats a 'unreachable statement'? Posted: Jul 19, 2003 1:32 PM
Reply to this message Reply
> I am making a Treemap in which the user is prompted for a
> word and then a definition. These are stored in the
> Treemap. Later the user is prompted to enter a word, and
> the treemap goes in to look for the word. If found, the
> definition of the word is returned. Here is what i have so
> far:
>
>
>
import java.util.*;
> 
> public class MapTree
> {
> public static void main(String[] args)
> {
> Terminal t = new Terminal();
> 
> TreeMap theMap;
> theMap = new TreeMap();
> 
> String b;
> String c;
> while (true)
> {
> b = t.readLine("Enter key: ");
> c = t.readLine("Enter value: ");
> theMap.put("b", new String(c));
> }
> 
> Iterator iter = theMap.keySet().iterator();
> r(); //@@@@@@//
> 
> while (iter.hasNext()) {
> b = (String) iter.next();
> c = (String) theMap.get(b);
> // do something with key and value.
> 
> }
> 
> 
> 
> 
> 
> }
> }
> 

>
> My problem lies at the line: Iterator iter =
> theMap.keySet().iterator();
>
> When i try to compile, i get: $ javac MapTree.java
> MapTree.java:22: unreachable statement
> Iterator iter = theMap.keySet().iterator();
> ^
> 1 error
>
>
> How do i fix that?
>
> Thanks.

you will never reach the statement after the while loop because
while(true)
is an infinite loop. ie. the while loop will continue on forever and never reach anything after the while loop.

Flat View: This topic has 1 reply on 1 page
Topic: resizing JFrame Previous Topic   Next Topic Topic: using .equals to compare objects of different types

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use