• Post Reply Bookmark Topic Watch Topic
  • New Topic

Evaluating the contents of an entire array  RSS feed

 
Andrew Humphries
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an IF loop, I want to evaluate the entire contents of a char[] array to see if any of the characters in it match and take action accordingly. I'll post what I have so far. Any help on this would be extremely appreciated, and please note I cannot use regular expressions in this (yes it is an assignment, I'm not asking for someone to code for me, just to let me know how I can evaluate an entire array).



Its at times like these I wish for the Perl [..] operator.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things.

First your for loop doesn't work. A For loop is basically as follows


Next I would suggest looking at the indexOf method in the String class.

Try this, post back with your results, and we can go from there.

P.S. On second thought you might want to look at the replaceAll method of the String class. I can think of a way with replace all that would not require a for loop or the char array, but I'm not sure what the requirments of the assignment are.
[ April 14, 2005: Message edited by: Steven Bell ]
 
Andrew Humphries
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're trying to say about my for loop, I've tried shifting it around a little and evaluating up to length but it simply does not run the loop in that case and skips over it. If I leave it how I first presented the loop, then it spews errors at me as soon as it hits a blank line (because I'm evaluating to 0 and doing i++ each pass, but it does the same with -1).

I've looked into indexOf a little, and it looks like it might take a lot of coding to use it in order to search a string for each one of those characters in my array. I have no idea how I'd place an array into it either.

I'm afraid I can't use replaceAll.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First String.length returns the length of the String which is one more than the last index. So if you say

You will get an ArrayOutOfBounds Exception. This is why you are getting errors when you get an empty line. The reason you are getting errors only on an empty line is because that is the only time the for loop is run as your condition is that i==0.

What I would suggest for the for loop is

or


They end up being the same thing, but I think the first one is a little more readable.

Also the line

should not compile as char[] is incorrect. You would either use 'char' to reference the array or 'char[int]' to reference an element of the array.

My guess is that you will need to have a for loop inside your for loop for what you are doing to work.
 
Andrew Humphries
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for the quick reply again. I'm taking on the stuff concerning the loop and making changes, and I'll have a play with a nested for loop too.

In regards with the change to my if loop, which was formerly:



(even though I put it as 'char[]' instead of 'notLetters[]' here.) I changed it to the follow so that it referenced the whole array as you said:



However now I have the problem that I can't compare a char with a char[]. Is this what you meant for the second for loop? That it would be used to dump each char into a char[], so that the if loop could assess it?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the purpose of the second/inner for loop would be to walk down each char in the 'notLetters' array.
 
Andrew Humphries
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I can do this now, many thanks for all your help, it has been inspirational.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hardly think I'm inspirational, but I'm glad I could help.

I'll show you what it would look like if you could use replaceAll.


Hope I got the regex right on that one.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!