Results 1 to 5 of 5

Thread: missing bracket

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    missing bracket

    Does anyone know why i might be getting an '}'
    expected error at line 60 which is basically
    at the end of the program? I dont see that
    i am mising any brackets.

    Im doing a little program that was
    assigned by one of my friends teachers
    as a project. So i decided to do
    it to just to get some practice. My algorithm
    for calculating the string is off {just to tell you}
    so ill have to work on it.

    Code:
    // create a method that takes a String as an arguement and returns a TreeMap
    // which will map the string and the number of characters that occur only 
    // once within the string. since the counting operation can be 
    // time consuming, the method should cache  
    // the results so that when the method is 
    // given a string previously encountered,it will
    // simply retrieve the stored result.
        
      import java.io.*; 
      import java.util.*;   
       
      public class MultipleChar{
         public static void main(String[] args){
    
         for(;;){
          BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            try{
             System.out.println("Please enter a string");
             String key = buff.readLine();
             TreeMap tm = (TreeMap) checkForDuplicates(key);
          
             System.out.println("Within" + tm.get(key) + "there were" +  "characters that only appeared once");
             System.out.println("Press x if you would like to exit");
             String exit = buff.readLine();
             if(exit.equalsIgnoreCase("x")){System.exit(0);}
           }catch(Exception e){System.err.println(e);}
        } // end for 
         } // end main 
       
       public static Object checkForDuplicates(String key){
            int onlyappearsonce = 0;
            boolean charmatch = false;
            String matchingString;
            Map stringMap = new TreeMap();
            
         
         // check map to see if string is present
         // if it is return key
     
             for(Iterator i = (stringMap.values()).iterator();  i.hasNext();){            
               if(stringMap.containsKey(key)){
                 return stringMap.get(i.next());     
                }
               i.next();
             }
        // if string is not present 
       // calculate characters that only appear once and add to map
           
        for(int index = 0; index <= matchingString.length() - 1; ++index){
              char matchingchar = matchingString.charAt(index);
                           
               for(int x = 1 + index; x <= matchingString.length() - x ; ++x){
                   if(matchingchar == matchingString.charAt(x)){
                      charmatch = true;
                      break;  
               }
               if(charmatch == false){++onlyappearsonce;}     
         } 
         Integer charcount = new Integer(onlyappearsonce);
         return stringMap.put(key,charcount);
       }

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    if(matchingchar == matchingString.charAt(x)){
                      charmatch = true;
                      break;
    I think it is here. You do't appear to have closed off your If block

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    also add another } to close off the class block

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Your right.... I fixed it but i still keep getting '}' expected at line
    63. very weird.

    Code:
    // create a method that takes a String as an arguement and returns a TreeMap
    // which will map the string and the number of characters that occur only 
    // once within the string. since the counting operation can be 
    // time consuming, the method should cache  
    // the results so that when the method is 
    // given a string previously encountered,it will
    // simply retrieve the stored result.
        
      import java.io.*; 
      import java.util.*;   
       
      public class MultipleChar{
         public static void main(String[] args){
    
         for(;;){
          BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            try{
             System.out.println("Please enter a string");
             String key = buff.readLine();
             TreeMap tm = (TreeMap) checkForDuplicates(key);
          
             System.out.println("Within" + tm.get(key) + "there were" +  "characters that only appeared once");
             System.out.println("Press x if you would like to exit");
             String exit = buff.readLine();
             if(exit.equalsIgnoreCase("x")){System.exit(0);}
           }catch(Exception e){System.err.println(e);}
        } // end for 
         } // end main 
       
       public static Object checkForDuplicates(String key){
            int onlyappearsonce = 0;
            boolean charmatch = false;
            String matchingString;
            Map stringMap = new TreeMap();
            
         
         // check map to see if string is present
         // if it is return key
     
             for(Iterator i = (stringMap.values()).iterator();  i.hasNext();){            
               if(stringMap.containsKey(key)){
                 return stringMap.get(i.next());     
                }
               i.next();
             }
        // if string is not present 
       // calculate characters that only appear once and add to map
           
        for(int index = 0; index <= matchingString.length() - 1; ++index){
              char matchingchar = matchingString.charAt(index);
                           
               for(int x = 1 + index; x <= matchingString.length() - x ; ++x){
                   if(matchingchar == matchingString.charAt(x)){
                      charmatch = true;
                      break;
                   }
               }
               if(charmatch == false){++onlyappearsonce;}     
         } 
         Integer charcount = new Integer(onlyappearsonce);
         return stringMap.put(key,charcount);
         
      }

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Duh? Im an idiot. I forgot to add a } for the class block. Thanks for pointing that out. It would have drove me crazy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width