• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

grabing letter

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok what i have to do is change any paraethesis I grab from input to a string instead of a charcter and if it is a letter I am suppose to grab the rest of the letter until I hit whit space. How do i do the letter thing? this is what i have so far

Any help woulb be great
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.lang.Character class has all kinds of methods to check a particular character. You're probably looking for Character.isLetter(char).
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest a regex search pattern. Have a look at the one below.




Here is the input and output string.



If you dont want digits or underscores, then you can specify a range in the search pattern instead of the 'd' meta character. Have a look at the Pattern and Matcher classes if you need more information.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay now I know how to check if it is a letter or a number but I do not know how to grab the rest of the letters...lets say i had something that looked like the following

(add 4 (mul 4 6))

I want to grab the entire word add or mul...anyone know how
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still need help.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A word would end at the next space character, correct? (Or possibly at the next parentheses in certain circumstances.) Since you know the index of where it starts, you could use the String.indexOf(String str, int fromIndex) method to search for the next character that would terminate the word.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ December 05, 2006: Message edited by: Joanne Neal ]
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay first I would like to say thank you for all the help.....second thank you Joanne but I can't use StringBuffer. Okay this is what I have what am I doing wrong
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello!

the given code suggests that you are trying to write a parser for simple equations and the example (add 4 (mul 4 6)) suggests that the equations are in prefix notation

if this is the case than its very simple split your string use the braces as delimiters because in prefix and postfix notations braces are of no use

String [] tokens = input.split("(|)");

this will break your string like this
tokens[0] = add
tokens[1] = 4
tokens[2] = mul
tokens[3] = 4
tokens[4] = 6

but if you really want to write a parser for all kind of equations you need to implement context free grammars.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to write a parser, per teacher instructions. I am unsure what you mean by "need to implement context free grammars".
 
Amir Kamran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well carry on as the teacher instruct you
but if you are interested you can find material on parsing and context free grammars easily on net

here i can give you some idea how things works

any equation follows a particular syntax for example if I say in my system all equations are of type
"operand operator operand"
and lets say operator means + and *, and operand is any number
now for example the input comes 2 + 2 it follows the above rule
and if input comes 3 * 5 it follows the above rule

in short the above rule defines a grammar and all equations of above type follows that grammar

now what i need to do is write a program that can parse the grammar "operand operator operand" and I am able to parse all equations of this type weather its 10 + 9 or its 100 * 300 whatever

its a really interesting subject building grammars, parsers, compilers ...
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay I have all new code now. I want to test it but I cannot get the test code to compile. Any help would be great. Here is the new code


Here is the test code.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I cannot get the test code to compile


What does this mean? If there are any errors, tell us what they are, and in which line they occur.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem I can see is that you are not creating a StringBuilder instance. Change (in two places)to

If you are still getting compilation errors when you have fixed that, post the error messages and I'm sure someone will explain them to you.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the errors I recieve. And I was wrong I receive them when I run the program

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the Javadoc for InputMismatchException you will see that it is thrown by the Scanner class. So you now need to look at the Javadoc for the Scanner class to see which of its methods throws this exception. Once you know this, you need to see which of these methods are called in your code and then try and work out why the exception is being thrown.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have already done this and so has my tutors at school and they have know idea what is going on. They could not fiqure it out. I was hoping you all could help. This is what I found on the error or exeception that I keep on getting

this is the line where it is messing up at
[ December 07, 2006: Message edited by: Victoria Preston ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried running your code thru a debugger or adding some print statements, to find out what the Scanner is trying to read at the point it fails ?
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I have...the scanner is not scanning anything...the error is thrown before anything is read
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the first time I've used Scanner, but after some repeated reading of the Javadoc, I can see your problem.
nextByte() does not read the next byte of the input. It reads a numerical String, which it converts to an integer and if this integer value is within the valid range of a byte it reurns that byte.
Try the following code


It prints 'A' because 65 is a valid value for a byte and is the ASCII value of the letter 'A'.

(add 4 5) is not a valid integer value so it throws an exception.
[ December 07, 2006: Message edited by: Joanne Neal ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic