last java prob from me for a while

mdchesne

Banned
Feb 27, 2005
2,810
1
0
Ello, all java programmers

------------
I have a String.

I want to check each char in the string. one after the other (n, n+1, n+2, etc.)

is there a way I can do this simply? like String.netChar? (i know this doens't work, but something similar, and would I need to create a new class defining a string checker?)
-----------
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
Yea, i always hated looking up things at the java site cause i never understood how to get it down on paper (or screen, w/e the system.in would be). no for those methods, I tried writing the code but it got wayyy to complicated the way i was trying to write it. Will I need to write a seperate string class so my methods of length and charAt have something to implement?
 

Tea Bag

Golden Member
Sep 11, 2004
1,575
3
0
Originally posted by: mdchesne
Yea, i always hated looking up things at the java site cause i never understood how to get it down on paper (or screen, w/e the system.in would be). no for those methods, I tried writing the code but it got wayyy to complicated the way i was trying to write it. Will I need to write a seperate string class so my methods of length and charAt have something to implement?

See my previous statement about thinking too hard :p

Obtain the string's value by whatever method (command line args, using the Standard Input Stream, initalize it yourself, whatever) - actually, that link will technically do what you want judging from your explination of the problem - but in this instance, say you declare a String and assign it a value like so in the program:

String myString = "value";

now, you can get the length of the string by using myString.length() - and use a for loop and check each character at the current value of the iteration - just use

myString.charAt(i);

where i is the value in the for loop's current iteration.

This is one easy way to handle it, and if you don't use that standard in, you should be able to do it with very few lines of code not counting your logic to handle the char that is returned.


EDIT: String IS a Class. Don't reinvent the wheel - use Java's implementation so you don't have to write code for something that already exists.. And the Java API is your friend. If you don't use it as a refrence, you are going to have a tough time (massive understatement) in learning or writing efficient code.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
forgot in a for loop if you only have "if" statements, and none fit, it'll just pass over the "if"s like normal and increase i at the end right? no if no ( or ) are found, it'll go to i + 1, right?

if so, i think i got it, thanks

(note): the tokenizer is setup and works properly. passing it the String s, from the tokenized string.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
oh yea, does this require importing? Can't use java.util, unfortunately, for the purpose of this project
 

Tea Bag

Golden Member
Sep 11, 2004
1,575
3
0
correct, if it it does not meet the conditonal(s) in

if (s.charAt(i) == ')') { do this; }
if (s.charAt(i) == '(') { do this; }

it will jump back up to the for loop and increment 'i' by the value specified.

EDIT: NO, it's part of Java.lang - and that code will give you a StringIndexOutOfBounds Exception. Arrays are zero based.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
so... i < size? not equal as well? that'll solve an out of bounds exception if you're saying that's where it'll be coming from
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
C'mon, it's super simple, there is nothing that confusing about the API. Once you learn inheritance in Pop's CS1122 it all fits together a lot better.

The easiest way is probably using the toCharArray() method, so something like char[] c = yourStringName.toCharArray();. Now you have a character array of the proper size that can easily be traversed one character at a time with a for loop.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
nah, i got it using the s.length()

working on javdoc and getting it to work now. getting a blank screen when i console it.

java ParenthesisMatch < Input.txt --> blank....
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
If you're not getting any output then it's not making it into the if() statements where you have a System.out.println()...
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
i know, still trying to figure out why. I'm thinking it's not accepting
if (tType == StreamTokenizer.TT_Word)

where the tType should be word after the intial tokenized stream got rid of the quotations. but maybe it's not TT_Word. maybe it's not reset. Like, I put a System.out.println("This si the tType: " + tType); right after i tokenized the initial string and it read 32... so it works on that respect... but maybe the string left isn't being tokenized or read as a TT_word. :confused:

he're my code if anyone wants to take a stab at it:
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
When tType is found to be a ' " ', then you automatically can get the whole string that is inbetween those quotes by using String str = input.sval.

So since he told us that our input will simply look like:

"dfsdf(dfs09df0sDF9)"

Then the very first time nextToken() is called, tType will be a quote (cast the int tType to a char if you wanna see what is really is), and so you can pull the whole string right then. So it's not even really necessary to have a while loop since it is pretty much guaranteed that the very first token is the quote/string delimiter.


edit: So in your code where you have

if ( tType == StreamTokenizer.TT_WORD ) {}

Well of course it won't get into that if statement since tType is a quote. So I think you might want something like

if ( tType == ' " ' ) then
do all your crap

But again, you can PROBABLY assume that the very first token it finds WILL BE a quote, but I did a check just to make sure that it is, otherwise keep calling nextToken() until the quote is found (because if the input was: dsfsdfsd"sdfsdf(dsfsdf)" then you want to ignore everything except the string in between the quotes.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
yea, now that I think about it, that does make sense. tType will be a quote.

but the thing is, even if java didn't get into my screwed up "if" statement, it should still print "good" (at the <if all stacks are empty, do this:>System.out.println("good")part) That was out of the while loop and if statements. So now it's backed up to the "try {" or somewhere up thar
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Yeah that is kinda messed up. I had the same problem before discovering what I just told you (no output even tho I still should've had something). If you need to ctrl-c your program to stop execution then it is just stuck in the while loop. So fix the tType business and you might solve other problems at the same time.


Also, your algorithm isn't set to handle if the nesting of the parenthesis are correct. Basically, whatever type is at the top of your stack ( parenthesis, bracket, or curly brace) must match the next closing type. This ensures the nesting is right. Not that just counting them up and comparing sizes is that bad, but I'm sure there will be some input that has equal numbers but wrong nesting. Read the emails from the cs2321-l for a better explanation.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
i have that already set. check out my Pop method in the Stack class towards the bottom. basically, if you try to pop (where there is a closing bracket before an opening of the same type), it'll system print "bad" automatically. I have an "if" statement in there to look for that. otherwise, it'll pop as usual. thanks for the concern though. :beer:
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
oh, and i'm beginning to loath the Eclipse compiler. my prog compiles fine at the workstations at tech, but i have 50+ errors here at home. lol. i think eclipse is trying too hard
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Those probably aren't errors, just warnings. Eclipse can be a bit picky when it comes to coding guidelines. For instance, it will notify you if imported classes aren't actually used, instance variables unused, etc. The program should still compile identically on either machine, given that you aren't using any external libraries.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
http://mchesney.blogspot.com/ they are errors. says right in the tab bar above the long list. I posted it on my blog i HAD to make for a class. only place i have currently that'll upload a pic. cya, off to compile at school. *trudges along pissed off he cant do it at home*
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Right... but you're only checking if the stack is empty then don't pop (which is a good idea as well). But if you're stack currently has

[
(
(

in it and you encounter a ')', it'll pop as usual EVEN THOUGH it is bad because you are popping bracket to "close out" with a parenthesis, hence the nesting is bad.
 

Tea Bag

Golden Member
Sep 11, 2004
1,575
3
0
If you look at your errors, notice that most of the end with "only available with Java source level 5.0".

Your home PC doesn't have Java 5. Do a java -version on the conslole to find out what version of the SDK you are running.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
It has nothing to do with linux/windows... java is multi-platform, just upgrade it obviously. You are using generic typecasting all over so of course Java 1.4 has errors with that.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
ah, ok. i'll do that once i get back.

as for the popping agian. you're saing:
Right... but you're only checking if the stack is empty then don't pop (which is a good idea as well). But if you're stack currently has

[
(
(

in it and you encounter a ')', it'll pop as usual EVEN THOUGH it is bad because you are popping bracket to "close out" with a parenthesis, hence the nesting is bad.

but it'll only pop the parenthesis stack (parens). there will still be a bracket...oooooooooooooooo
while i was talking out loud i know what you mean now

( [ [ { ) } ] ] will return "good" even though the nesting is horrific

sh1t crackers!

more work. oh, but i did replace the TT_Word with ' " ' and it works fine now, thanks for that :)

EDIT:
I'm setting a boolean in the ParenthesisMatch class method: main called Well. When it's true, system.out.println("good") and false for bad. This solves a little problem i found while testing, but in order for the Pop method in the NodeStack class to use it, i have to pass it something right? I forgot how to pass it "Well". any suggestions?

 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
At whever point you call pop(), then call pop( well ) instead. Then in your method declaration of pop(), have it set to receive a boolean, so public ... pop( boolean Well ) {} That is the basics of passing variables or references; the signatures must match.

Alternatively you could declare it an instance variable, but that could bring up problems with a static context, as well as that not being as much of a object-oriented/information hiding design.
 

mdchesne

Banned
Feb 27, 2005
2,810
1
0
I think I'm setting it up wrong. I got it to compile alright, but I don't think the
Well = false
in the Pop method is setting the Well back up in the Main method to false. Still gives me "good" for everthing