0

I am new to Java and I am currently learning about String Manipulation with Java in school.

I have the below program which displays the string entered, the length of the string a user-entered, and where the first space in our string is. (No exception handler)

    Scanner in = new Scanner(System.in);
    
    System.out.println("Please enter your string of words, sentences, or both!");
    
    String entrypoint = in.nextLine();
    
    System.out.println(entrypoint);
    
    int manippointone = entrypoint.length();
    
    System.out.println("Your string is " + manippointone + " characters long.");
    
    int maniptwo = entrypoint.indexOf(" ");
    
    System.out.print("Found the first space in your string at character number: " + (maniptwo + 1));

I have compiled and run the program and it works fine. The issue is on to the more complicated part which needs me to do the following:

Display the word after the 3rd space in your initial string Display the string from index 12 to the end of the string

I understand indexOf() along with charAt() but I am having trouble with those two parts of this assignment. Any help would be greatly appreciated.

  • 2
    Have a look at the javadocs for the class `String` - it will become apparent very quickly what you need to do https://docs.oracle.com/javase/8/docs/api/java/lang/String.html – Scary Wombat Oct 25 '21 at 01:30
  • 1
    Also the method `String::split` may help you. – Scary Wombat Oct 25 '21 at 01:31
  • 1
    Look at these questions: [how-to-split-a-string-in-java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) and [how-to-get-substring-from-string](https://stackoverflow.com/questions/31960752/how-to-get-substring-from-string) – op_ Oct 25 '21 at 01:33
  • @ScaryWombat I looked through the docs and forgive me if I am wrong but I didn't see a method that could return a string after a specified index. Specifically after a number of that index like a word after the 4th "t" in a given string. – hello_world Oct 25 '21 at 01:39
  • 1
    *I didn't see a method that could return a string after a specified index* - At the top of the page there are examples how to use. – Scary Wombat Oct 25 '21 at 01:41
  • *Specifically after a number of that index like a word after the 4th "t" in a given string* - you question does not mention anything like this. – Scary Wombat Oct 25 '21 at 01:41
  • @ScaryWombat I asked about how I display the word after the 3rd space in a given string. – hello_world Oct 25 '21 at 01:43
  • The second and third comments tell you. Plus this into your favorite search engine like *java split string* and you will get thousands of hits. – Scary Wombat Oct 25 '21 at 01:45
  • @ScaryWombat Oh so I could split the string at a given index. The thing is, the string changes as it is inputted. – hello_world Oct 25 '21 at 01:47
  • No, there are two different requirements one it to return a String from the 12th char. The top of the javadcos shows you how to use substring to do this. The second one could use `split` to split up the string based upon a specified char (i.e. space). This returns an array of Strings - you will want the third element (0,1,2 <- this one). Try actually doing some coding, and then come back when you have a problem. – Scary Wombat Oct 25 '21 at 01:55

1 Answers1

0

There are comments about splitting a string; though you could solve your problem that way, I think it's not a direct solution to your problem.

The method I think you're looking for is String.substring() there is a version that takes two integer parameters, the start and end of the desired substring, and a version that takes one integer parameter, and returns the substring from that index to the end of the string. Be careful with the parameters, they are 0-based, and it is therefore easy to get them one off from where you want them.

Find the index you want, including use of the String.indexOf() version that has a starting index, then substring to get the part you want.

EDIT: be sure your code handles the case where the user does not put 3 spaces in their string.

this
  • 94
  • 1
  • 6
arcy
  • 12,845
  • 12
  • 58
  • 103