Jump to content

Recommended Posts

Posted

// the question is about methodOverloading in java. I am having trouble with this method, i only included this one method as the others are working fine

// i test this code with the string "the cat sat on the mat" "1" "12" "a" "m"


//the method should remove all characters beteen the given characters(exclusive) from the string. this should happen between the first instance of the first character and the last instance of the second character.

// with the string "the cat sat on the mat" this method should print out "the camat"



import java.util.Scanner;

public class methodOverloading

{

public static void main (String [] args)

{

Scanner scan = new Scanner (System.in);


String s = scan.nextLine();


char c1 = scan.next().charAt(0);

char c2 = scan.next().charAt(0);


System.out.println(manipulation(s,c1,c2));//part 5// not working





public static String manipulation(String s, char c1, char c2)

{

int a =0 ,b =0;


for(int i =0; i<s.length(); i++)

{

if(s.charAt(i)==c1);

{

a=i;

System.out.println(i);

break; // for some reason i doesn't get past 0, i have printed out the value stored in i at this point and is 0

}

}

for(int j = s.length()-1; j>=0;j--)

{

if(s.charAt(j)==c2)

{

b = j;

break;

}

}

System.out.println(a); // this also prints out 0, it should print out 6

System.out.println(b);// this prints out 19 which is expected

String s2 = s.substring(a+1,b-1); // this doesnt print out "the camat" it prints out "t mat", if i hard code the print statement to print out a substring of s from (6 to 19) it prints out "the camat" as expected

s =s.replace(s2, "");

return s;

}

}

Posted (edited)

 

if(s.charAt(i)==c1);
^^^^^^^^^

 

You made here mistake at the end of line, there is character ;

 

BTW, did you think about using string.indexOf() and string.lastIndexOf() instead.. ?

 

You should not use string.replace() like you used for this task. Think what will happen if start index,end index will be very close, just single char, substring could be f.e. 'a'.. and the all occurrences of 'a' will be turned to empty string..

Edited by Sensei
Posted (edited)

Generally instead of using method overloading it is better to use generics.

 

package generics;

public class Box<T> {
	private T t;

	public void add(T t) {
		this.t = t;
	}

	public T get() {
		return t;
	}
	
	public String manipulation(T p, T p1, T p2) {
		int a = 0, b = 0;
		String s = p.toString();
		char c1 = p1.toString().charAt(0);
		char c2 = p2.toString().charAt(0);
		
		for (int i = 0; i < ((String) s).length(); i++) {
			if (s.charAt(i) == c1)
			{
				a = i;
				System.out.println(i);
				break;
			}
		}
		for (int j = s.length() - 1; j >= 0; j--) {
			if (s.charAt(j) == c2) {
				b = j;
				break;
			}
		}
		System.out.println(a);
		System.out.println(b);
		if(a > 0 && b > 0){
			String s2 = s.substring(a + 1, b - 1); 
			s = s.replace(s2, "");
			return s;
		}
		return null;
	}

	public static void main(String[] args) {
		Box<Integer> integerBox = new Box<Integer>();
		Box<String> stringBox = new Box<String>();		
		stringBox.manipulation("Hello World","H","W");
		integerBox.manipulation(123,4,8);
	}
}
Edited by fiveworlds
Posted (edited)

David, did you check your code?

You're using "Hello World" string with "H" letter as first character to find. But you're doing if( a > 0 )... But H is at index 0... Your examples the most likely won't work.

a,b should be initialized to -1 (or use string.indexOf/string.lastIndexOf), and then check whether they are not -1... 0 is valid index.

Edited by Sensei
Posted

 

You made here mistake at the end of line, there is character ;

 

BTW, did you think about using string.indexOf() and string.lastIndexOf() instead.. ?

 

You should not use string.replace() like you used for this task. Think what will happen if start index,end index will be very close, just single char, substring could be f.e. 'a'.. and the all occurrences of 'a' will be turned to empty string..

Hi, thanks that fixed it, There are other methods in this class which use a similar process so i wanted to keep them along the same route. Thanks again!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.