Published:
Updated:

https://programmers.co.kr/learn/courses/30/lessons/12918

charAt()Permalink

public char charAt(int index)

Returns the char value at the specified index.
An index ranges from 0 to length() - 1.
The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
If the char value specified by the index is a surrogate, the surrogate value is returned.

Specified by:
charAt in interface CharSequence
Parameters:
index - the index of the char value.
Returns:
the char value at the specified index of this string. The first char value is at index 0.
Throws:
IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

Source


SolutionPermalink

class Solution {
    public static boolean solution(String s) {
        boolean answer = true;

        if (s.length() >= 1 && s.length() <= 8) {
            if (s.length() == 4 || s.length() == 6) {
                for (int i = 0; i < s.length(); i++) {
                    if (!Character.isDigit(s.charAt(i))) { // is not a number
                        answer = false;
                    }
                }
            } else { // **
                return false;
            }
        } else { // **
            return false;
        }

        return answer;
    }

    public static void main(String[] args) {
        System.out.println(solution("a234"));
        System.out.println(solution("1234"));
    }
}


Another SolutionPermalink

class Solution {
  public boolean solution(String s) {
      if(s.length() == 4 || s.length() == 6){
          try{
              int x = Integer.parseInt(s);
              return true;
          } catch(NumberFormatException e){
              return false;
          }
      }
      else return false;
  }
}

Leave a comment