• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Compilers and Methdods

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic