-
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. :D
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);
}
-
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
:)
-
also add another } to close off the class block
;)
-
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);
}
-
Duh? Im an idiot. I forgot to add a } for the class block. Thanks for pointing that out. It would have drove me crazy. :D