Results 1 to 9 of 9
- 03-17-2011, 10:04 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
How do i delete a section of a user inputed string that i input
import java.util.Scanner;
public class StringRemoval {
/**
* @param args
*/
public static void main(String[] args) {
Scanner eyes = new Scanner(System.in);
Scanner pwn = new Scanner(System.in);
String word;
String word2;
System.out.println("Enter Word");
word = eyes.next();
System.out.println("Type what you want to go away");
word2 = pwn.next();
System.out.println(word.substring(word2.length(),w ord.length()));;
}
}
Example----
user types in mountain
user types in mount
output is ain
so basically i want to subtract the 2 inputed strings using substring
- 03-17-2011, 10:11 PM #2
Strings are immutable so what you need to do is make a new String with only the bits you want. You can use indexOf to find the location of the unwanted segment and then use that in the call to substring.
- 03-17-2011, 10:12 PM #3
BTW it is pointless having 2 Scanner objects. Just use the same one for both inputs.
- 03-17-2011, 10:19 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
so what would my code be after
word2 = pwn.next()
- 03-17-2011, 11:01 PM #5
I already told you. Use indexOf to find the location of the small word in the large word. Then use that in substring.
If you are waiting for the actual code, don't hold your breath.
- 03-17-2011, 11:03 PM #6
You might want to make your code crash proof by adding an if statement to make sure that the small word exists in the large word.
- 03-18-2011, 01:57 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
okay so i tryed ur way with the indexOf however i kept getting an error that i cant convert a string to an int.
so it tryed coding a different way and developed this
import java.util.Scanner;
public class StringRemoval {
/**
* @param args
*/
public static void main(String[] args) {
Scanner eyes = new Scanner(System.in);
Scanner pwn = new Scanner(System.in);
String word;
String word2;
System.out.println("Enter Word");
word = eyes.next();
System.out.println("Type what you want to keep");
word2 = pwn.next();
String end = word.substring(0, word2.length());
System.out.println(end);
}
}
- 03-18-2011, 02:04 AM #8
And it doesn't work. Why bother asking for advice if you are going to ignore it?
If you get an error it doesn't mean that the approach is wrong, it means that you made a mistake in doing it that way. Since you didn't post your attempted code or the error message how are we supposed to help?
- 03-18-2011, 04:02 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
drawing the cross section of a pool using user input
By reddy in forum New To JavaReplies: 2Last Post: 10-11-2010, 06:58 AM -
How to draw and delete according to user input?
By ProspectiveDeveloper in forum AWT / SwingReplies: 8Last Post: 04-22-2010, 11:58 PM -
Averages of user inputed values (Need Help)
By Zebra in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:51 PM -
Prompting user input of a string.
By apfroggy0408 in forum New To JavaReplies: 3Last Post: 03-09-2008, 06:23 PM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks