File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Simple Program not working. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Program not working." Watch "Simple Program not working." New topic
Author

Simple Program not working.

richard gallagher
Greenhorn

Joined: Mar 15, 2008
Posts: 9
Hello,

Ive written a small program that reads a text file (whose text is "one" or "two". An instance of a class "number" is updated, and the program is meant to print the text, the line number, and wether the number is odd or even. However the "odd or even" never works, it is always "false", so please help me find where i am going wrong

main class



number class



Thankyou,

RG
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Originally posted by richard gallagher:


When your number object is created, the isOdd variable gets a default value of false.

You create your number object with the string representation of the StringTokenizer. That's something like 'java.util.StringTokenizer@XXXXXX' where the XXXXXX part is the hexidecimal representation of the hash code.

You need to create a new number object inside your loop with the result of st.nextToken() instead:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
richard gallagher
Greenhorn

Joined: Mar 15, 2008
Posts: 9
Hello,

Thanks very much Rob Prime, it works now and i understand how now
Out of curiosity, is there a more up-to-date method to achieve the same results (ie to improve speed, accessibility from other classes etc), as im not sure if im using an outdated method. ( No need to rewrite the code if there is a better method, just a "keyname" would be good).

Thankyou,

RG
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
It's worth mentioning that StringTokenizer is a legacy class, so use String.split() or regex intead.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
richard gallagher
Greenhorn

Joined: Mar 15, 2008
Posts: 9
Hello,

Same output using Sting.split()

Jake Maruk
Greenhorn

Joined: Mar 16, 2008
Posts: 3
Hi all!
Mine is more simple (I hope) than the one posted here by Richard.

I am trying out the code I got from a book:



I tried to compile it, using javac BeerSong.java. I am getting errors like:


I tried to search the web for some explanation and still searching.
I hope that by posting it here, I can get some tips/answers on what I did wrong, the quicker way...

Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Look at your class path.
Jim Lafs
Greenhorn

Joined: Mar 26, 2008
Posts: 1
Its BeerSong not Beersong
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20


That's not how Java works - you have to give the class name, excluding the location. Instead, you should use the classpath:

The %CLASSPATH% part is to include any previous classpath you had set.
 
GeeCON Prague 2014
 
subject: Simple Program not working.