Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    BTW it is pointless having 2 Scanner objects. Just use the same one for both inputs.

  4. #4
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    so what would my code be after
    word2 = pwn.next()

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  7. #7
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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);

    }



    }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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?

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can also use a StringBuilder too, however; you need to read carefully what junky said and figure it out from there, think about it.

Similar Threads

  1. Replies: 2
    Last Post: 10-11-2010, 07:58 AM
  2. How to draw and delete according to user input?
    By ProspectiveDeveloper in forum AWT / Swing
    Replies: 8
    Last Post: 04-23-2010, 12:58 AM
  3. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:51 PM
  4. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 07:23 PM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 08:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •