• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array without ints

 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on a program and I am trying to figure out how to make sure there are no integers input into a character array.

I do understand that I need to step through each index of the array. I am just not sure how to test to make sure each index does not have an integer value.

Thank you,
Eric
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since an int and a char are both just integral primitives in Java, within the range of a character's valid values, they are indistinguishable... except for the type of the variable. If the array is defined to be a char[], anything that the compiler KNOWS to be an int will give a compile error if you try to assign it directly into one of the char's in the array (without a cast - but a cast seems counter to your stated goal).

Anything the compiler sees as possibly being a char (like a char that has some math operation like multiplication, that would take it out of char range, will just lose precision and have only a min/max value of Character.MIN_VALUE and Character.MAX_VALUE. So once they're in the array, there's no way of knowing they were once int's.

-- Jon
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha. You make a good point. I need to check before they are input into an array. I am using a string when I read the in the input. Then, I am converting the string into a character array with toCharArray(). So I need to check for integers before I make the conversion from the string.

Thank you,
Eric
 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you iterate through your array, extract each element to a string. Then create a patern Matcher and supply the regular expression '[0-9]?'. This should look for occurences of any digit and return a true or false. Just search for tutorials on regular expressions and pattern matchers.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using something like isLetter or isDigit?

Thanks,
Eric
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions are powerful and flexible, but for doing what you describe, isDigit would be fine. Not sure which would be more efficient, but I suspect any difference wouldn't matter for your needs. You'd have to write a loop to iterate through the characters in the string.

-- Jon
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using 1.4 or later, you can use regex on your source Strings.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello David,

I just sent you an e-mail. I was going to see if you can answer a few questions for me.

Thanks,
Eric
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to post my latest code so it makes things a little easier.

 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My main questions are:

How can I make sure the user can only input 80 characters?

Should I change the input error checking:
if(Character.isLetter(input[i]) || Character.isSpaceChar(input[])) ?

What is the best thing to have in the echo input error checking statement:
if(choice == 'n') ?

How do I use the method:
public static boolean palindrome(char[] a, int used) ?

Thank you,
Eric
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, after much time and hard work, I have my program doing most of what I want it to do.

I still have these questions:

If the user inputs a 2 for the menu choice before a string is input, I get a NullPointerException error. How can I fix this?

The boolean palindrome(char, int) is not returning the correct result.

Thanks in advance,
Eric

 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I have gotten rid of the NullPointerException error.

I am getting this error:
ArrayIndexOutOfBoundsException: 9
at PalindromeCheck.palindrome(PalindromeCheck.java:89 )
at PalindromeCheck.checkInput(PalindromeCheck.java:67 )
at PalindromeCheck.mainMenu(PalindromeCheck.java:155)
at PalindromeCheck.main(PalindromeCheck.java:177)

How is my array getting full? I do not see what I am doing wrong.

I cannot figure out why boolean palindrome is not returning the correct results.

Thanks,
Eric

Latest code:
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please help me figure out what I am doing wrong here?
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is my array going out of bounds? I am trying to learn this, but it is not coming easy at times.

Thank you,
Eric
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me figure this out?

Thanks,
Eric
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you don't get a response here at the Ranch, you've got to look over what you've posted and see if you can provide more information. Simply posting again asking for help won't make a difference. There are a lot of posts here, and often instead of replying that we cannot help people will skip your post if it involves too much work to help. We want to help as many people as possible.

So, what have we got? You've given us a couple hundred lines of code and a stack trace. Where exactly is line 89? I see it must be in palindrome(), and most of that is commented out.Since the bolded line is the only one accessing the array, I must assume that used is set larger than a.length. Have you verified that used has the correct value you expect? Put some println()'s in there to verify your assumptions.

I recommend you try out some of these ideas as this is the kind of detective work you need to practice so you can find solutions yourself. I'll let you find the actual problem, but post more information if you cannot find it.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, sorry about that. I am in the process or rewriting it.
When I run it more than once, it is giving me some weird output when I echo back the input to the user the second time.

 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, fixed that part. I forgot to reset count.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I have a few questions.

How do I:

Make sure the input is only letters and blank spaces?
Make sure there is a period at the end of the charString?
If the user inputs the wrong string, how do I allow the user to stay in the program to reinput the correct string?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Character class has several static methods that will analyze an individual char. Check the JavaDocs.

The String class has a static method that will tell you if a String ends with another String. Check the JavaDocs.

For this you'll want to use a loop (e.g. while) that keeps going until the user's input passes your tests. One pattern that doesn't require you to create a boolean to store the result is this:This will loop forever until the break is hit.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I am still having some problems.

I am getting this error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at Palindrome7.checkInput(Palindrome7.java:29)
at Palindrome7.mainMenu(Palindrome7.java:127)
at Palindrome7.main(Palindrome7.java:158)
Exception in thread "main"


[ April 26, 2005: Message edited by: eric elysia ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell which line is 29 (please bold the line in the future or mark it in some other way), but this is a problem:What happens when charString doesn't have a period?
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that I didn't mark line 29.

The user is required to put a period at the end of the input string.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, but what happens when the user doesn't put in a period? Is that line 29, by the way? If not, please edit your previous post and bold line 29.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have bolded line 29 in my previous post.

I am trying to write code that makes sure the user does enter a period.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eric elysia:
I am trying to write code that makes sure the user does enter a period.
Yes, but do you see what happens if the user enters a String without a period? Let's see what happens when you enter "hi" without the quotes.Since 'h' is not a period, the loop body is executed, placing 'h' into the array a.Since 'i' is not a period, the loop body is executed, placing 'i' into the array a.Since i is now outside the bounds of legal values for the String, you get a StringIndexOutOfBoundsException.

I recommend thoroughly reading the java.lang.String JavaDocs as there are several methods in that class that would be very helpful here. Since String is such a core class, it's a very good idea to become familiar with all of its methods.

For example, you want to remove all spaces from the String and test to see if it ends in a period. trim() will remove leading and trailing whitespace (not spaces between words), and endsWith() will tell you if one String ends with another String. Or you could use charString.charAt(charString.length()-1) to check the last char of the String.

Also, if you're using JDK 1.4 or later and you're allowed to use the regular expression methods, you could remove all non-letters with a single line of code using replaceAll().

[ Grrrrrrammar is goooooood! ]
[ April 26, 2005: Message edited by: David Harkness ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic