Example usage for java.lang String charAt

List of usage examples for java.lang String charAt

Introduction

In this page you can find the example usage for java.lang String charAt.

Prototype

public char charAt(int index) 

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());

    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();

    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);

    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:DateParse2.java

public static void main(String[] args) {

    //+/* w  w  w . j a  v  a  2  s  . c  om*/
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "BD: 1913-10-01 Vancouver, B.C.", "MD: 1948-03-01 Ottawa, ON",
            "DD: 1983-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
        String aLine = input[i];
        String action;
        switch (aLine.charAt(0)) {
        case 'B':
            action = "Born";
            break;
        case 'M':
            action = "Married";
            break;
        case 'D':
            action = "Died";
            break;
        // others...
        default:
            System.err.println("Invalid code in " + aLine);
            continue;
        }
        int p = aLine.indexOf(' ');
        ParsePosition pp = new ParsePosition(p);
        Date d = formatter.parse(aLine, pp);
        if (d == null) {
            System.err.println("Invalid date in " + aLine);
            continue;
        }
        String location = aLine.substring(pp.getIndex());
        System.out.println(action + " on " + d + " in " + location);
    }
    //-
}

From source file:Main.java

public static void main(String[] args) {
    String str = "12345";
    int counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (Character.isLetter(str.charAt(i)))
            counter++;/*from   w  w w .  j  a v a2s. c o m*/
    }
    System.out.println(counter + " letters.");
}

From source file:CountVowels.java

public static void main(String[] args) {
    System.out.print("Enter a string: ");
    String s = sc.nextLine();

    int vowelCount = 0;

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u'))
            vowelCount++;//from w ww .  j  a va  2  s.  c  o m
    }
    System.out.println("That string contains " + vowelCount + " vowels.");
}

From source file:Main.java

public static void main(String args[]) {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();/*from   ww  w.j a  v  a 2 s . c  o m*/
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
}

From source file:Main.java

public static void main(String[] args) {
    String n = "level";
    boolean right = true;
    int f = n.length() - 1;
    for (int i = 0; i < n.length(); i++) {
        if (n.charAt(i) != n.charAt(f - i)) {
            right = false;//from  ww w  . ja  va2 s  .  co  m
        }
    }
    System.out.println("The word is " + right);
}

From source file:MarkVowels.java

public static void main(String[] args) {
    System.out.print("Enter a string: ");
    String s = sc.nextLine();
    String originalString = s;/*w w w.j  a  v  a2 s  . c  o  m*/

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u')) {
            String front = s.substring(0, i);
            String back = s.substring(i + 1);
            s = front + "*" + back;
        }
    }
    System.out.println(originalString);
    System.out.println(s);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea comp = new JTextArea(5, 20);
    comp.setText("this is a test");
    frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);

    String charsToHighlight = "a";
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();//from  w ww .  j  a  va 2 s .  c  o  m
    String text = comp.getText().toUpperCase();

    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (charsToHighlight.indexOf(ch) >= 0)
            h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:TabFilter.java

public static void main(String args[]) throws Exception {
    FileReader fr = new FileReader(args[0]);
    BufferedReader br = new BufferedReader(fr);

    FileWriter fw = new FileWriter(args[1]);
    BufferedWriter bw = new BufferedWriter(fw);

    // Convert tab to space characters
    String s;
    while ((s = br.readLine()) != null) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\t')
                c = ' ';
            bw.write(c);/*w  w w.  ja va2s.c om*/
        }
    }

    bw.flush();
    fr.close();
    fw.close();
}

From source file:Main.java

public static void main(String[] args) {
    String input = "test";
    Stack<Character> stack = new Stack<Character>();
    for (int i = 0; i < input.length(); i++) {
        stack.push(input.charAt(i));
    }//from w  w  w.  ja  v a2s.  co m
    String reverseInput = "";
    while (!stack.isEmpty()) {
        reverseInput += stack.pop();
    }
    if (input.equals(reverseInput)) {
        System.out.println("Yo! that is a palindrome.");
    } else {
        System.out.println("No! that isn't a palindrome.");
    }
}