[Solved] substring methods java


String.substring() is a method, not a field, meaning you need to call str.substring() rather than simply str.substring. Further, the substring method takes parameters – you have to tell it the specific substring you want in the form of which indexes within the string. str.substring(0, 2) would print characters 0 and 1 (the upper bound is not included). For your purposes, str.substring(height, height+1) would work…

IF you have to use substring.

I would recommend using str.charAt(height), which accomplishes the same goal.

EDIT (based on your edit) :
1) You have height defined as a String, then you call str.charAt(height). The charAt() method takes an int parameter – if you give it 6, it will give you the charater at index 6 (i.e. the seventh letter) in the string. Knowing that, it doesn’t make any sense to pass a String in as a parameter, does it?

2) You’re still going to need a loop to accomplish this. Something like:

String str = console.nextLine();
     for (int height = 0; height < str.length; height++) {            
         System.out.println(str.charAt(height);   
     }

Does that make sense? Let me know if I need to walk you through what we’re doing in this.

10

solved substring methods java