Main Str
passing a string class to main and evetually printing to system?
i am trying to pass result of class to main and finally print it to system
mainly i am trying to find error in class by printing what i am doing...
am i missing something here
public class test{
String str="java";
int index=3;
public String twoChar(String str, int index) {
int j=str.length();
int i=index+2;
return new String(str.substring(index,i));
}
public static void main(String [] args){
System.out.println(twoChar);
}
}
This line is incorrect and will give you a compile time error. This is an error when you try to complie it.
System.out.println(twoChar);
You can change it to:
System.out.println(twoChar(str, index));
But you will have a problem.
int index = 3;
should be:
int index = 1;
or
int index = 0.
If you have 3, and then inside the twoChars method, you increase the index by 2, you will end up with 5.
Now, the length of str = "java" is 4, so 5 is bigger than 4 and will give you an error when you run the program. This is called a runtime error.
OK, above I said you could have index equal 1 or 0. The reason for this is, again, the length of "java" is 4. The indices start at 0 and stop at 3.
Put another way.
The character at position 0 is a "j";
The character at position 1 is an "a';
The character at position 2 is a "v";
The characer at position 3 is an "a";
So you need int index to be either 1 or 0.
There is really only one other error, but it is not an error per se.
The line:
return new String(str.substring(index,i));
returns a string as you want, but you could get the same result with:
return str.substring(index,i);
as the substring method already returns a string, that then can be returned from the twoChar method.
On another note, you can put the lines:
String str="java";
int index=3;
inside the main method and remove them from where they are. This is recommended.
On yet another note, you are not using the int j in the twoChars method. So you could get rid of it.
There are other things to consider, but I won't get into that now, as it should not bother the program as it is.
![]() |
![]() Conners Pottery Barn Main Str Orangeville PA Columbia $4.08 Time Remaining: 1d 3h 46m Buy It Now for only: $4.08 |
![]() Maine Coast STR Alice no 2958 Boat Ship BW Early 1900sPhoto Postcard $5.99 Time Remaining: 3d 7h 40m Buy It Now for only: $5.99 |
![]() Springfield MA Main Str Near Johnsons Bookstore $4.25 Time Remaining: 4d 8h 57m |
![]() F1973 USA WOONSOCKET RI MAIN STRTRAM TROLLEY POSTCAR $12.00 Time Remaining: 4d 21h 13m Buy It Now for only: $12.00 |
![]() F2814 USA MAINE ME PORTLAND MIDDLE STRFIDELITY BUILDIN $12.00 Time Remaining: 5d 14h 54m Buy It Now for only: $12.00 |
![]() WHITE STR COR MAIN DANBURY CONN CT CONNECTICUT $8.95 Time Remaining: 6d 9h 27m Buy It Now for only: $8.95 |
![]() TUS USA WINSTED CONN WEST MAIN STR LOOKING EAST PC $13.00 Time Remaining: 7d 2h 28m Buy It Now for only: $13.00 |
![]() TUS USA GLOVERSVILLE NY NORTH MAIN STR TRAM TROLLEY $13.00 Time Remaining: 7d 2h 29m Buy It Now for only: $13.00 |
![]() E2373 USA NY PEEKSKILL MAIN STR FROM NELSON AVE $14.00 Time Remaining: 7d 19h 42m Buy It Now for only: $14.00 |
![]() Arch Bridge Main Str Athol Massachusetts Postcard 1924 $8.50 Time Remaining: 8d 1h 58m Buy It Now for only: $8.50 |
![]() EL DORADO SPRINGS MISSOURI DOWNTOWN STREET SCENE VINTAGE POSTCARD MAIN STR MO $16.99 Time Remaining: 11d 22h 6m Buy It Now for only: $16.99 |
![]() Spencer MA Main Str $4.00 Time Remaining: 20d 16h 50m Buy It Now for only: $4.00 |
![]() SIMSBURY CT MAIN STR OPPOSITE WOODS FARMHOUSE c 1910s $14.95 Time Remaining: 21d 2h 43m Buy It Now for only: $14.95 |
![]() RPPC Main Str Hartwick NY New York Cooperstown Oneonta $6.50 Time Remaining: 21d 5h 9m Buy It Now for only: $6.50 |
![]() 1906 View of main Str Bridgeport Connecticut Postcard $3.99 Time Remaining: 26d 21h 9m Buy It Now for only: $3.99 |















