|
-
Aug 19th, 2001, 02:51 PM
#1
Thread Starter
Dazed Member
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);
}
-
Aug 19th, 2001, 03:04 PM
#2
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
-
Aug 19th, 2001, 03:06 PM
#3
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
-
Aug 19th, 2001, 03:09 PM
#4
Thread Starter
Dazed Member
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);
}
-
Aug 19th, 2001, 03:11 PM
#5
Thread Starter
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|