eanna.macken Posted March 3, 2017 Posted March 3, 2017 // 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; } }
Sensei Posted March 3, 2017 Posted March 3, 2017 (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 March 3, 2017 by Sensei 1
fiveworlds Posted March 3, 2017 Posted March 3, 2017 (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 March 3, 2017 by fiveworlds
Sensei Posted March 4, 2017 Posted March 4, 2017 (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 March 4, 2017 by Sensei
eanna.macken Posted March 4, 2017 Author Posted March 4, 2017 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!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now