*
The moose likes Beginning Java and the fly likes First program. Tons of errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "First program. Tons of errors" Watch "First program. Tons of errors" New topic
Author

First program. Tons of errors

Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Hey all.
I've decided to create a pet project in order to apply what I'm learning from the Java Head First program. The pet project will be a text-based RPG. Today I attempted to write a portion of the code which would test drive 'flares,' IE magical effects which intermittently accompany the swinging of a weapon.


EDIT: Most problems fixed, but still having a bit of trouble post-compilation. Please scroll down and reference my latest reply.

EDIT: Thanks for the formatting advice. Done.

EDIT: Solved. Thanks a ton, all.

[ May 24, 2008: Message edited by: Alex Birmingham ]

[ May 24, 2008: Message edited by: Alex Birmingham ]
[ May 24, 2008: Message edited by: Alex Birmingham ]

- SCJA 1.0
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Using an IDE like NetBeans or JGRASP will help catch alot of your errors (like String instead of string). I started with JGrasp it is simple and easy to learn. NetBeans will provide more functions.
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Below is your code with the typos removed. In addition, your System.in.read() should be only one char,and it throws an exception, so I commented that out. You can fix that.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Don't write huge segments of code without compiling along the way.

Write a few lines, then compile. Fix the errors as they happen. When those lines compile without error, then write a few more lines, then compile...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

a general tip... don't write this much code before you compile it.

a lot of people will tell you to write as little as you can. I
often try and only write 2-3 lines before i re-compile - some
folks will tell you to only write ONE line.

If you're just learning to code, i would pull out something like
netbeans just yet. i'd stick with something simple - i use one called
context. It does formatting and syntax highlighting, but not much
else. as a beginner, you NEED to learn what's right and wrong. also,
those IDEs will hide much of the detail from you.

just my 2-cents.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Thanks for the help! I now understand pretty well the mistakes I've made, but haven't yet learned how to 'throw' an 'exception.' In fact I've never heard either of those terms. Nonetheless the only remaining error is now:

swing.java:74: unreported exception java.io.IOException; must be caught or declared to be thrown
int entry = System.in.read();

I'm just going to assume that this material is a bit over my head for the moment, and use the pre-made code I've got in Java Head First to handle user input. Thanks again.
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Grrr.... Okay Ive got everything to compile, but for some reason the program itself just loops "Enter 1 to swing, or 2 to quit." regardless of use input. Very frustrating seeing as how I can't actually test the meat of my program! Here's a revised version of the code, complete with the Java Head First pre-packed code for getting user input:



[ May 24, 2008: Message edited by: Alex Birmingham ]
[ May 24, 2008: Message edited by: Alex Birmingham ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

something that would make THIS thread more readable would be if you broke up your statements into shorter lines. for example:


This is identical as far a java is concerned, but is much easier both in this forum AND for anyone reading your code.
Uncle Ed Hatfield
Greenhorn

Joined: May 24, 2008
Posts: 10
Hi,

I'm a newbie myself, but:

You might insert a print statement after you get the user entry
to see what it is.

Also you might want to store all those responses in an object
(class itself) that somehow associates the ranges with the strings
(so you don't have this huge if else statement.

Also there are lots of wonderful debuggers and teaching tools out there
(Dr Java?) so you can step through your program and see what's going on.

Good luck. :-)
Uncle Ed.
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Okay Ive got everything to compile, but for some reason the program itself just loops "Enter 1 to swing, or 2 to quit." regardless of use input.


Okay I made a couple of changes to your code
1) I added boolean playagain this will deal with someone entering something other than "1" or "2"
2) there is a problem with the "if" statement. for this case you need to use "equals" method to compare objects.



I hope this helps, Larry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Originally posted by Larry Frissell:
Using an IDE like NetBeans or JGRASP will help catch alot of your errors (like String instead of string). I started with JGrasp it is simple and easy to learn. NetBeans will provide more functions.
Maybe, but it is not usually a good idea for a beginner to use an IDE. Better to start with a decent text editor, which doesn't mean NotePad. On Linus try Kate or gedit, on Windows google for Notepad2 and Notepad++, which are much better than Notepad.
The advice to got 2-3 lines at a time is also good.

. . . and Uncle Ed, welcome to JavaRanch.
 
wood burning stoves
 
subject: First program. Tons of errors
 
Similar Threads
SCJA Prep
GUI troubles
Conceptual Misunderstanding of Arraylists?
Regexes
Method to build subclass objects