Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Program not working.

 
richard gallagher
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
richard gallagher
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's worth mentioning that StringTokenizer is a legacy class, so use String.split() or regex intead.
 
richard gallagher
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Same output using Sting.split()

 
Jake Maruk
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your class path.
 
Jim Lafs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its BeerSong not Beersong
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic