Jump to content

Whitespace Hw


Guest olyvaha

Recommended Posts

Guest olyvaha

no idea how to even begin, its been along time since i touched java and this is like trying to climb a glass wall...some pointers, someone said something about using Character.isWhitespace() but java has become foreign to methanks for any help you can offer

public class Lex{private String s; // input stringpublic String[] tokens = new String[20]; //scanned tokensprivate final char nullChar = '\u0000';private int currentCharLocation = 0;private char c='1'; //character under examinationpublic Lex(String s){this.s = s;scan();}private void scan(){String token="";int currentToken=0; //count of tokensc = getChar()skip white space at start of linewhile (c /= nullchar){if not whitespace{append c to token and get next character}else //found white space, add token to list{add token to token listadvance token counterclear tokenskip whitespace}}//if}//whileif (token != "") //add last token to listtokens[currentToken]=token;}private char getChar(){return a character from the stringif end of string return null character}private void skipWhitespace(){skip spaces and tabsmake use of getChar}public static void main(String[] args){//add more test casesLex l = new Lex(" aaa bbbb ccccc ddddd ");for(int i = 0; i < l.tokens.length; i++){System.out.println(i + "\t" + l.tokens[i]);}}// main}//Lex

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...