aspose file tools*
The moose likes Java in General and the fly likes grabing letter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "grabing letter" Watch "grabing letter" New topic
Author

grabing letter

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42266
    
  64
The java.lang.Character class has all kinds of methods to check a particular character. You're probably looking for Character.isLetter(char).


Ping & DNS - my free Android networking tools app
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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

Joined: Feb 03, 2006
Posts: 106
I still need help.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42266
    
  64
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15

[ December 05, 2006: Message edited by: Joanne Neal ]

Joanne
Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
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
Amir Kamran
Ranch Hand

Joined: Apr 21, 2002
Posts: 48
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

Joined: Feb 03, 2006
Posts: 106
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

Joined: Apr 21, 2002
Posts: 48
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

Joined: Feb 03, 2006
Posts: 106
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
Marshal

Joined: Mar 22, 2005
Posts: 42266
    
  64
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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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

Joined: Feb 03, 2006
Posts: 106
Here are the errors I recieve. And I was wrong I receive them when I run the program

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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

Joined: Feb 03, 2006
Posts: 106
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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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

Joined: Feb 03, 2006
Posts: 106
yes I have...the scanner is not scanning anything...the error is thrown before anything is read
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: grabing letter