0

I have been set a task to print out the 2nd, 3rd and 4th letter of a name within a String variable.

So for example the name is John and I want to print out the letters 'o, h and n only'

I was wondering if there is a specific method which I can use to carry out this task, if not what is the best approach to take?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Consider the .charAt() method of the String class, and use a loop initialized appropriately. The substring() method of String is also a possibility. Read the javadocs in the String class. – KevinO Mar 29 '16 at 23:50
  • java has a lot of documentation [javadocs](https://docs.oracle.com/javase/8/docs/api/) You can usually find what you want with little effort. [substring](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)) – BevynQ Mar 29 '16 at 23:56
  • `System.out.println(variable.substring(1));` – Elliott Frisch Mar 29 '16 at 23:57
  • You need to try this first by yourself and post your code. – Tacocat Mar 30 '16 at 00:00

3 Answers3

2

Assuming the name always has at least 4 letters in it, this code will work:

public static void main(String[] args) {
    String name = "JOHN";
    System.out.println(name.substring(1,4));
}
Aaron
  • 992
  • 3
  • 15
  • 33
2

EDIT as @KevinO pointed out, determining whether the name length or 4 is smaller solves issues that would cause exceptions. I updated to incorporate this input.

Depends on how you're trying to print it. You can use a for loop and iterate starting at the 1st index of your String name like so:

String name = "John";
for(int i = 1; i < Math.min(name.length(), 4); i++){
    System.out.print(name.charAt(i));
}

Sample Run:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

You could print out theCharacter one at a time like:

System.out.print(name.charAt(1)); //print character at index 1
System.out.print(name.charAt(2)); //print character at index 2
System.out.print(name.charAt(3)); //print character at index 3

This might be unsafe because you're not sure if the name will in be in fact at least 4 Characters long. Sample run:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

Or perhaps the easiest way which is also safe, you could print it out using String.substring() which takes in a range, like so:

    System.out.println(name.substring(1, Math.min(name.length(), 4)));

This results in:

run:
ohn
BUILD SUCCESSFUL (total time: 0 seconds)

robotlos
  • 536
  • 2
  • 10
  • .substring(1, Min(name.length(), 4)) avoids the last problem of String length. The first solution doesn't stop at the 3rd printed character (it has name.length() but the question was character 2,3,4 only) if I read the question correctly. – KevinO Mar 30 '16 at 00:13
  • @KevinO, right, incorporated your input into my answer. Thanks! – robotlos Mar 30 '16 at 00:18
0

You can do that by using charAt function on String . The following version of code should do what you are asking for.

public static void main(String[] args) {
    String input = "John";
    if (input != null && input.length() > 1) { 
               System.out.println(input.substring(1, Math.min(input.length(), 4)));     
    }
}
Sajeev
  • 818
  • 7
  • 23
  • This can work, but it is verbose and hard to maintain. Furthermore, it will print in a stacked column rather than on the same line. (Meed to use print not println). – KevinO Mar 30 '16 at 00:01
  • @KevinO code edited to meet your concerns. We have to check length to avoid StringIndexOutOfBoundsException – Sajeev Mar 30 '16 at 00:08
  • 1
    yes, the StringIndexOutOfBounds is a concern. But consider `if (input != null && input.length() > 1) { System.out.println(input.substring(1, Math.min(input.length(), 4))); }`. One only needs to ensure the length is greater than one. – KevinO Mar 30 '16 at 00:10
  • @KevinO good one. I liked you solution. – Sajeev Mar 30 '16 at 00:11
  • @KevinO I have applied your suggestion to the answer thanks. – Sajeev Apr 19 '16 at 04:54