permaculture playing cards*
The moose likes Cattle Drive and the fly likes Method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Method question" Watch "Method question" New topic
Author

Method question

Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
My head is THIIIIIIICK! The most I have been able to do so far is make several very small/primitive methods that are pretty useless. In the following, I'm trying to make a method that will take a String array and two ints and get the substring of the int values. What am I doing wrong, or should I say what am I not doing wrong?? I want to avoid the code answer if possible:
<pre>
public class Method
{
static int MakeDigit( String[] input, int b, int e )
{
int num = Integer.parseInt( input.substring( b, e ) );
return num;
}

public static void main( String[] args )
{
System.out.println( Method.MakeDigit( args[0], 0, 1 ) );
}
}

Errors:
java:5: Method substring( int, int ) not found in class java.lang.object.
java:13: Incompatible type for Method. Can't convert java.lang.String to java.lang.String[].
</pre>
Steve
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20647
    ∞

Your MakeDigit method should probably take a string instead of a string array.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Oh, wow! I guess I was closer than I thought. Thanx.
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
O.K. I changed it a little. Basically the same except MakeDigit() accepts only a String:
<pre>
static int MakeDigit( String input )
{
int b = 0;
int e = 1;
int num = Integer.parseInt( input.substring( b, e ) );

return num;
}
</pre>
....and tried to access MakeDigit()'s int variable b in the main() method with the following tries/errors:
System.out.println( MakeDigit().b );
-no method matching MakeDigit() in class
System.out.println( MakeDigit( input ).b );
-undefined variable: input
System.out.println( b );
-undefined variable: b
....How could I access int b in the main() method? The format is pretty much the same as the code in my first post.
Steve
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
I put the incrementing in the method, and just about have the whole thing finished. However, I still need to know how to access a static method's (local?) variable from the main() method. Even if it doesn't fit this assignment (1.4), I'd still like to know if it is possible. I'm not so clueless to methods anymore......small but delicious VICTORY!
Thanks, Steve
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20647
    ∞

You can't access variables that are inside other methods. I think what you want to do is pass in a String and a number to the method. The number would be your b.
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Maybe a little clueless.....I should've known that but it didn't come to mind. I tell you, I was going to ACCESS that variable. I think my will takes over sometimes; other times I just get so confused, that I lose what little conviction I did have.
Thanks, I am almost done with the floundering and desparate code.
[This message has been edited by Steven YaegerII (edited August 19, 2000).]
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
I wanted to maybe get an idea of how many lines of code/methods to use in 1.4. I'd hate to make you wade through it unnecessarily. My whole class has about sixty lines, keeping in mind that curly braces have their own line. This one was a well needed assignment.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20647
    ∞

My solution is 127 lines and it includes the extra credit. It has four methods including the main().
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Thanks. I think I'll try to add a few more methods before I submit.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Only 127 lines - guess I'll have to optimize a little more.
Marilyn
Originally posted by Paul Wheaton:
My solution is 127 lines and it includes the extra credit. It has four methods including the main().


[This message has been edited by Marilyn deQueiroz (edited November 01, 2000).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20647
    ∞

I think line count is not important. Readability is important.
Plus, hardly anybody's solution looks like mine.
For the tiny 1.1 assignemnt there are four to five architectural approaches. And no two have ever been identical.
What I'm looking for is clean, elegant, easy to understand solutions. Focus on that. Not on line count.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method question