• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method question

 
Steven YaegerII
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21188
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your MakeDigit method should probably take a string instead of a string array.
 
Steven YaegerII
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, wow! I guess I was closer than I thought. Thanx.
 
Steven YaegerII
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21188
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21188
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My solution is 127 lines and it includes the extra credit. It has four methods including the main().
 
Steven YaegerII
Ranch Hand
Posts: 182
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I think I'll try to add a few more methods before I submit.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
paul wheaton
Trailboss
Pie
Posts: 21188
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic