• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Simple Program not working.  RSS feed

 
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
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
 
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()

 
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
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your class path.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its BeerSong not Beersong
 
Rob Spoor
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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
Boost this thread!