Home > database >  I want to print the first character of the first string from an array of Strings and the last charac
I want to print the first character of the first string from an array of Strings and the last charac

Time:08-25

I don't know what is the issue, I tried debugging but I couldn't understand why it's not working

class Solution {
    public void sol(String s) {
        String [] parts = s.split(" ");

        int left = 0;
        int right = parts.length - 1;

        System.out.println(parts[left].charAt(left)); // ->this is correct
        System.out.println("_____________");
        System.out.println(parts[right].charAt(right));// wrong output
    }
 }
class Main{
    public static void main(String[] args) {
        Solution s = new Solution();
        s.sol("Don't read this");
    }
}

CodePudding user response:

You need to get the last character of the last string. So first find the length of the last string parts[right].length() then get the index of the last character by reducing one from it.

System.out.println(parts[right].charAt(parts[right].length()-1));

CodePudding user response:

You're double solving the problem you need to either do this

        System.out.println(s.charAt(left));
        System.out.println("_____________");
        System.out.println(s.charAt(right));

or this

        System.out.println(parts[left]);
        System.out.println("_____________");
        System.out.println(parts[right]);

As it is, what you are doing is taking the (lets say) 10th character in the string, and then finding the char at 10 within that single character. Which just doesn't work or make sense.

I misread the question. Should be

        System.out.println(parts[left].charAt(0));
        System.out.println("_____________");
        System.out.println(parts[right].charAt(parts[right].length()-1));

Okay, so for System.out.println(parts[left].charAt(0)) lets break it down. Let's say the string is "sample string". That means:

parts = ["sample", "string"]

So we want to get the letter "s" from "sample". So we need to get parts[0] first, that gets us "sample." Then we want to get the first letter (or rather zeroth letter). So that's parts[0].charAt(0). That should be the letter "s"

Next we want the last letter of the last word. That means we want the "g" from "string". So first we need to get the last word. There are 2 items in the list, so the index of the last word will be 2 - 1 = 1 Where 2 is the length of the list. Thus:

int right = parts.length - 1;

So now we can access the word string (or whatever the last word may happen to be) like this:

string lastWord = parts[right]

To get the last character, we do almost the same thing. We need to know first how long the string is, then we need to subtract one and get the character at that index. Thus:

int rightIndexOfRightWord = lastWord.length() - 1;
string lastChar = lastWord.charAt(rightIndexOfRightWord);

Put it all together and we get this little block of code:

int right = parts.length - 1;
string lastWord = parts[right]
int rightIndexOfRightWord = lastWord.length() - 1;
string lastChar = lastWord.charAt(rightIndexOfRightWord);

We can then combine all of that into one line:

int right = parts.length - 1;

string lastWord = parts[parts.length - 1]
string lastChar = lastWord.charAt(lastWord.length() - 1);
int right = parts.length - 1;
string lastChar = parts[parts.length - 1].charAt(parts[parts.length - 1].length() - 1);

done!

CodePudding user response:

System.out.println(str.charAt(0));//to print first character of the first string
System.out.println(str.charAt(str.length()-1));// to print last character of the last string
  •  Tags:  
  • java
  • Related