aspose file tools*
The moose likes Beginning Java and the fly likes Question about Compilers and Methdods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about Compilers and Methdods" Watch "Question about Compilers and Methdods" New topic
Author

Question about Compilers and Methdods

Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
I am suppose to somehow create some text and ask for input in the main method. And then create another void method that is PARAMETERLESS and call it in the main method to reverse the digits that were entered. My question is, how is this possible? How am I suppose to use those inputs and get the new method to read it and do the work without passing the argument since it will be parameterless?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

your class can have a String as a member variable. In your main, create an instance, and set the string to your input.

Then, call your method, where you can access the string using this.String...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Think object-oriented: create an object whose fields include that input, and whose methods include a no-args method which reverses the input.
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
I have to look more into this because whenever I search a little bit more on instance variables I get others. Like all the examples about using instance data always call the method and pass the argument though that method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Tarrell Fletcher wrote: . . . all the examples about using instance data always call the method and pass the argument though that method.
You are looking in the wrong place. Get a book like “Head First” (get the 2nd edition) and read about instance fields. If you have a method which claims to take an instance field as a parameter, and do anything with it, you have either got the wrong website, or have misunderstood what they said.

You should know how to set up an instance field in an object of a class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
fred rosenberger, Yesterday at 18:22:17 wrote:. . .
Have you been taking lessons from Rob? Even he has difficulty beating me by 34 seconds
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Ok yea I understood what instance variables were. I was doing it already but never realized it was called that. But here is my code but now I am getting an error message saying I can't run the public void method in the main driver.

Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Ok got it to work somewhat. I just declared "public static String number; " as my instance variable to cover the scope of all the methods. I just created an Object from the void method I made and had it print out like that. Didn't even know I could do that but I was just playing around with it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Tarrell Fletcher wrote:. . . I just declared "public static String number; " as my instance variable . . .
No, you didn’t. If you called it static, then it isn’t an instance anything. You would have to remove static, give your class a constructor which takes that String as a parameter, and create an instance. You can do that in the main() method. Then you can call the reverse() method on that object.
You say to enter a 5-digit number, but you are taking a String. That means you can pass any String, eg “12345” or “Campbell” and get “54321” or “llebpmaC” back.
A for loop to traverse an array or a String backward should be written like this: for (int i = length - 1; i >= 0; i--) {...}
You can use the insert() method of StringBuilder to reverse a String: similar technique mentioned here.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Tarrell Fletcher wrote:Ok got it to work somewhat. I just declared "public static String number; "...

A really bad habit to get into.

First of all, static variables are NOT instance variables; second, making all your variables static defeats the whole purpose of the language, which is to deal with objects; third, you should make variables private unless you've got a really good reason not to, and doubly so when they're static.

For the moment, a good mantra for you, from 'Animal Farm': private variables good; public variables bad.

If you need to access the variable, provide getters and setters for it (and they can be public), viz:and now your main() might look something like:
HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Compilers and Methdods
 
Similar Threads
Please explain this becuase I dont understand
Overloading constructors with differing intializations
Form Data not being populated in Struts 2
Need Help on UI designing
regarding classes inside interfaces