Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newbie question...helppp plzz

 
teena abraham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im in the process of learning java. trying to write a small program. i wrote the methods. now i dont know how to invoke the methods in my main
here is the code

CAN ANYONE HELP ME.....THANKS IN ADVANCE
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ December 31, 2002: Message edited by: Dirk Schreckmann ]
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teena,
Method "main()" is a static method (since it is defined using the 'static' keyword). The other methods in your "test" class are instance methods (i.e. not static). In order to invoke an instance method, you need an instance. So, in my opinion, you have two choices:
1. make all the instance methods static methods
2. create an instance of class "test" in method "main()", example:-
test aTest = new test();
Then you can do the following:-
aTest.do0_9();
Hope this has helped you.
Good Luck,
Avi.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kind of off the subject, but your methods also have a bug:

Since your method does not increment i (e.g. i++), i will never reach 100 so your program will print "9" repeatedly - probably not what you intended.
[ December 31, 2002: Message edited by: Blake Minghelli ]
 
teena abraham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Avi and Blake
I made modifications according to ur sugegstions but im getting a runtime error..... :roll:
here is the code

dont know where i went wrong....any help appreciated
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ December 31, 2002: Message edited by: Dirk Schreckmann ]
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teena,
I copied the code you posted here, compiled it and ran it -- and I didn't get a runtime error. I am using JDK 1.4.1_01 on Windows 2000.
However, you did not correct the mistakes that Blake pointed out, so when I execute the "Empty" class and supply a value greater than 9 (nine), I get an infinite loop!
Perhaps you would care to post the runtime error message (and accompanying stack trace) that you are getting?
Good Luck,
Avi.
 
teena abraham
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks avi for spending ur valuable time in teaching me the basics of java. stupid of me wat happened is i didnt give a number and pressed enter button.
actually i have changed the code wat Blake has pointed out. but i cut and pasted the pre version. but even then its going as an infinite loop


(code tags added by Marilyn)
[ January 01, 2003: Message edited by: Marilyn de Queiroz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ January 01, 2003: Message edited by: Marilyn de Queiroz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but even then its going as an infinite loop

I don't see an infinite loop....

Perhaps you need to define what you want your program to do.

The current program does this:

Get input from keyboard
Parse it
if ( x < 10 ), print all numbers from zero to (but not including) 9
else if ( x < 100 ), print all numbers from zero to (but not including) 99
else if ( x < 1000 ), print all numbers from zero to (but not including) 999.

No infinite loop here.
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

--
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
-----------------------------
[ edited to add line breaks to code -ds ]
[ January 05, 2003: Message edited by: Dirk Schreckmann ]
 
Divya Venkatesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the working code.I have commented your mistakes in the code.I dont know if this is what you want.If u need any clarifications do ask.

Hope you've got this clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic