• Post Reply Bookmark Topic Watch Topic
  • New Topic

Substring Challenge/Console input error  RSS feed

 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

I'm extremely new to Java, so bear with me, eh?

Next semester in my computer science class we're going to learn Java. Being a relatively experienced programmer (in Python mostly, but a teeny tiny bit of C as well), I decided yesterday that I should learn Java so I could be a bit ahead of the curve.

After having read through most of the basics of Java in the Java tutorial, I thought it'd be a good idea to search for some simple challenges to complete to make sure I had it down. After a bit of searching, I found a post on this forum about another person who was given the task of writing a program that could determine if one string was a substring of another. I thought this would be a cool challenge, so I decided to code it. For the most part it works - I think the general algorithm is solid - however when I try to make it so the user can specify the string through console input, it throws an error at me, which I can't decipher.

So my question is, how do I properly get user input from the console?

Also, I have no idea if this code is "proper" Java.

Here's the code:


If you have any suggestions for any of the code, they're greatly appreciated!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Schultz wrote:If you have any suggestions for any of the code, they're greatly appreciated!

Before that, why don't you tell us what the error is (the exact message please - in full).

Winston
 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure! the error is this:


Edit:
Oh, silly me. I shoulda just looked up the error, but I must have been too tired to realize that the error message wasn't nearly as vague as I thought it was. I've figured out now that the Eclipse console doesn't count as a console, and so c was being assigned to a null. If I compile with javac it works fine
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Schultz wrote:Oh, silly me. I shoulda just looked up the error, but I must have been too tired to realize that the error message wasn't nearly as vague as I thought it was. I've figured out now that the Eclipse console doesn't count as a console, and so c was being assigned to a null. If I compile with javac it works fine

Now that's the kind of problem we like. Funny how running things past someone else often helps you to the solution, isn't it? Remember that if you ever decide to program for a living.

Winston
 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Also, does anybody have any comments on the code? Did I do it right?

Also, why is it that the checkString function has to be static? I've seen other programs where there are functions outside of main() which aren't static, but java throws an error if checkString isn't static.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Schultz wrote:Also, why is it that the checkString function has to be static? I've seen other programs where there are functions outside of main() which aren't static, but java throws an error if checkString isn't static.

Because you haven't created any working objects.

Java (and BTW, it's 'Java'; not 'java' or 'JAVA') programs should be made up of classes that do the work for you. Here's an example of how you might do it with a non-static method:Please note, the above is just an example, there are many other possibilities; but whenever you write a program, think first about the class (or classes) that will do the work.

HIH

Winston
 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the insight! I'll have to remember to think in classes, although it doesn't come naturally to me. I suppose I'll just have to get used to it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Schultz wrote:Thanks very much for the insight! I'll have to remember to think in classes, although it doesn't come naturally to me. I suppose I'll just have to get used to it.

No probs. Please note correction above; contains() should NOT have been static.

Winston
 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question: is it good/bad/proper to define all of your classes in separate .java files? I suppose it would depend on the situation, but if you only use a class in one particular program (like this example), is it better to define them separately or all together in one file?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Schultz wrote:Another question: is it good/bad/proper to define all of your classes in separate .java files? I suppose it would depend on the situation, but if you only use a class in one particular program (like this example), is it better to define them separately or all together in one file?

The answer to your first question is: generally, yes. The trouble you're running into at the moment is that the problems you're being given can probably be solved with a single class, but that won't be the case for long. You may also want to browse the tutorials for "inner classes", but for now, I'd stick to the pattern of 'one class, one file' - and NOT the one that contains your main() method.

Winston
 
Cole Schultz
Greenhorn
Posts: 6
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, cool. Thanks for the help
 
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!