Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

One last problem  RSS feed

 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one final problem to solve with my program.

I'm trying to build an array from a string. I have to ignore blank spaces in the string. Here's my code that I've got so far.




It gives me the error: char cannot be dereferenced. What the heck does that error mean? What did I do wrong?
 
Martin Richard
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charAt returns a char data type not the Character class.

You can either do checks with (s.charAt(i) ==' ' ) or generate a new Character object like so..


PS: Why do you increment i in your if bracket?
[ March 30, 2005: Message edited by: Martin Richard ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting this error because, you are trying to call a method on a primitive variable.

isWhitespace(char ch) is a static method available in Character class. So you can check whether a character is a whitespace or not by writing
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that incrementing "i" inside the body of the loop is wrong here; just not doing an assignment results in the character being skipped, and so there's no need (nor is it correct) to increment i again.

There's also no need to create a Character object. The Character.isWhitespace() method is a static method that takes the character as an argument. You use it like this:

if (Character.isWhitespace(s.charAt(i))) ...
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I see about the incrimenting bit. I didn't take that into account. Oops.

However, I tried the way that you suggested and found that I had a different error when compiling. (Not dealing with the incriment but on how to use isWhitespace.) Now it can't find symbol method isWhitespace. There must be something else that I'm missing. Either in code or understanding. (Probably understanding but maybe it's both )
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you remember to import java.lang.Character?
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Homer Simpson would say DOOH!!! Nope I didn't.

So after some red facing I added it. It compiles but does not do what I want it to. The loop ends at the blankspace and does not continue.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the updated version of your code so we wan have a look.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it.

Here it is:



Hey everyone I just have to say that I really appreciate your help with this.
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason:
Just want to encourage you on your effort to learn java; there wont be a better approach than the one you 've adopted. bug every one the more; we are all here to help.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'm finding that Java is not the easiest language I've ever learned and this forum is really helping me. I'm recommending it to all the people I know who are trying to learn also to learn Java.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't show where cstrl, maxLength, s and j are defined. Your problem almost definately lies with one of the initialisation of one of these variables.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I was trying to save some space by posting only the code that didn't work but if you want here's the whole program.

 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, try this.

If you replace your Scanner stuff with a simple string:

i.e. s = "This is a sentence"

You can debug the main part of your code.

Your problem is to do with the way you are using Scanner, but I'll leave it up to you to read the API and work that out.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Horatio Westock:
Did you remember to import java.lang.Character?


By definition, java.lang.* is implicitly imported into every compilation unit. This should absolutely not be necessary. If you're using an environment where it actually is, I'd be truly interested to hear about it.

It's more likely that you've created your own class named "Character" and have it sitting on your CLASSPATH somewhere. Naming a class the same as any class in java.lang is a double plus ungood idea.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


By definition, java.lang.* is implicitly imported into every compilation unit. This should absolutely not be necessary. If you're using an environment where it actually is, I'd be truly interested to hear about it.


Interesting - I didn't even think about that, I just read that there was a compile error straight after using Character and assumed it was an import problem just like any other. I'm curious now too!
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I did, I did it without ever writing the code. I don't really know what's going on. As for the scanner class, I really don't think that's where my problem is but since I'm new to Java it could be. If I block out the code that checks for a blank space, which I did in the code I posted, everything works. (Except for when you enter in a space that is.)
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try adding the line above and see what it says.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just my two cents:
Jason,
class names should be capitalized: write class Tester instead
[ March 30, 2005: Message edited by: miguel lisboa ]
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that it's bad form for a lower case letter begining a class name, however tester is not the final class. I just put things in there to get them to work. Usually no one see this class.
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I finally got the string correct. I should have been using nextLine() instead of next(), kudos for you for figuring that one out. So now that that was correct I ran the program again. It picked up the blank space, printed the message, then included the space into the array. ???

All was going fine until that point.

Also my check to see if the two arrays are equal no longer works even if there is not a space in the string. Is it just me or did I just do a trade to get one thing working only to lose something that did work. I even set it up as seperate vars. The cleaned version of the array is not even being used in the checks and my checks still don't work unless this section of code is blocked out.

???

So what's going on?

I'm getting more and more confused by the minute and I hate to keep bugging you guys about this.
 
Alex Prawira
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't use try StringTokenizer to extract each character from the String ?
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I never heard of it and don't know how it works.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


By definition, java.lang.* is implicitly imported into every compilation unit. This should absolutely not be necessary. If you're using an environment where it actually is, I'd be truly interested to hear about it.

It's more likely that you've created your own class named "Character" and have it sitting on your CLASSPATH somewhere. Naming a class the same as any class in java.lang is a double plus ungood idea.


There is another possibility - he might be using version 1.0 of the JDK. I hope very much he isn't...
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just letting everyone know I got the program up and running and everything works. Thanks for your help everyone!
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason, how do you do it. For some reason I always have problems throughout my life, and it's one after another. How do you get to it being the one last problem?

Sorry

Mark
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really very simple. One problem at a time.

Since I can only handle one problem at a time, that's exactly how I do it.

This way every problem is just "one last one"
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

Please do share with us as to what got it running?

Coz I was looking the code and apart from the String objects misbehaving, I couldn't think of anything else that could have cuased ur program yo fail.

Thanks

-Kiennjal
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Batchelder:
I know that it's bad form for a lower case letter begining a class name, however tester is not the final class. I just put things in there to get them to work. Usually no one see this class.


It's still a good idea to follow standard conventions, even when you just write "toy code". This will keep you in the habit of following these conventions. I have seen such an attitued leading to laziness when coders write programs that other's WILL see. In larger homework projects (and even more so in commercial projects), this leads to unmaintainable code and wastes more time in the long run than it ever saved initially.

For example, I am a senior in college and sometimes advertise to do private tutoring sessions for CS and math classes. Recently, I am helping a student in the compiler course, which includes a semester long project to develop to a fully functional, if limited, compiler. This student has thought to save time by not bothering with formatting and indentating his code. Most of our sessions have been devoted to chasing down compiler errors, many of which have been related to missing curly braces. These problems would have been completely avoidable (or at least obvious) if he had adopted and followed some kind of consistent indentation practice.

Enough of my soapbox. I just want to encourage you to follow coding conventions even when it doesn't seem to matter. As I said, if you practice doing this all the time, it will just be a habit when you write code where it DOES matter.

Keep Coding!

Layne
[ March 31, 2005: Message edited by: Layne Lund ]
 
Jason Batchelder
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was mainly in the for loops. They were not inserting the elements properly. Thus were causing the program to fail. The first for loop I was using the wrong counter if I remember right.

The second one I changed it to the final section of code.


It also helped that I changed the checks to reflect that I was using cstr1 instead of str1. Oops. Oh well live and learn and I'm learning so I must be living.

Oh, before I forget, tester without the capital letter was a mistake I made almost the first week of class. I made the class to test something that I thought would work, (hence the name). Since it was such a great name, describing exactly what the class is for, I never remade it but just keep using it week after week. I just tranfer what works into my real class when I'm done.

Have fun all and keep programming.

"We don't know one millionth of one percent of anything." -Thomas Alva Edison
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!