• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Compilers and Methdods

 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Campbell Ritchie
Sheriff
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic