Category Archives: cs_review

CS Review: Strings

Tokenizers:

  1. StringTokenizer tokenizer = new StringTokenizer(“Example String Here”, delimiter (blank if space));
  2. while(tokenizer.hasMoreElements()){
  3.         Integer i = Integer.parseInt(tokenizer.nextElement().toString());
  4. }

Substring:

  1. String full = “Full Text”;
  2. String sub = full.substring(5,9);
  3. System.out.println(“Will print the word ‘Text’:  ” + sub);

Split:

  1. String full = “This is the full text”;
  2. String[] = full.split(” “); //or regular expression
  3. string.trim();

Character At:

  1. char c = string.charAt(7);

Replace:

  1. String s = “Hello”;
  2. s.replace(‘H’, ‘J’);//turns Hello to Jello
  3. s = “Hello”;
  4. s.replace(“Hello”, “Jello”);//also turns to Jello

Character Array:

  1. char[] cha =  new char[str.length()];
  2. for(int i=0;i<str.length();i++){
  3.         cha[i] = str.charAt(i);
  4. }
  5. //toString for printout

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Strings

Tokenizers:

  1. StringTokenizer tokenizer = new StringTokenizer(“Example String Here”, delimiter (blank if space));
  2. while(tokenizer.hasMoreElements()){
  3.         Integer i = Integer.parseInt(tokenizer.nextElement().toString());
  4. }

Substring:

  1. String full = “Full Text”;
  2. String sub = full.substring(5,9);
  3. System.out.println(“Will print the word ‘Text’:  ” + sub);

Split:

  1. String full = “This is the full text”;
  2. String[] = full.split(” “); //or regular expression
  3. string.trim();

Character At:

  1. char c = string.charAt(7);

Replace:

  1. String s = “Hello”;
  2. s.replace(‘H’, ‘J’);//turns Hello to Jello
  3. s = “Hello”;
  4. s.replace(“Hello”, “Jello”);//also turns to Jello

Character Array:

  1. char[] cha =  new char[str.length()];
  2. for(int i=0;i<str.length();i++){
  3.         cha[i] = str.charAt(i);
  4. }
  5. //toString for printout

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Input/Output

There are a few basic types of input/output sources that are usually used.

Buffered File In:

    1. String file = “file.txt”;
    2. FileReader fReader = new FileReader(file);
    3. BufferedReader bReader= new BufferedReader(fReader);
    4. String s = bReader.readLine();
    5. bReader.close();

Buffered File Out:

    1. String fileOut = “fileOut.txt”);
    2. FileWriter fWrite = new FileWriter(fileOut);
    3. BufferedWriter bWrite = new BufferedWriter(fWrite);
    4. bWrite.write(“Anything”, offset, length);
    5. bWrite.close();

Standard In (Usually a Keyboard):

    1.  InputStreamReader cin = new InputStreamReader(Sytem.in);
    2.  char c = (char) cin.read();

Standard Out (Console):

    1. System.out.print(“System.out is already setup”);

 

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Input/Output

There are a few basic types of input/output sources that are usually used.

Buffered File In:

    1. String file = “file.txt”;
    2. FileReader fReader = new FileReader(file);
    3. BufferedReader bReader= new BufferedReader(fReader);
    4. String s = bReader.readLine();
    5. bReader.close();

Buffered File Out:

    1. String fileOut = “fileOut.txt”);
    2. FileWriter fWrite = new FileWriter(fileOut);
    3. BufferedWriter bWrite = new BufferedWriter(fWrite);
    4. bWrite.write(“Anything”, offset, length);
    5. bWrite.close();

Standard In (Usually a Keyboard):

    1.  InputStreamReader cin = new InputStreamReader(Sytem.in);
    2.  char c = (char) cin.read();

Standard Out (Console):

    1. System.out.print(“System.out is already setup”);

Scanners:

  1. Scanner in = new Scanner(System.in);
  2. int i = in.nextInt();

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.