import java.util.*; public class InputString { public String buffer; public int current; public InputString() { buffer = new String(); current = 0; } public InputString(String b) { buffer = b; } public int getCurrent() { return current; } public void setCurrent(int c) { current = c; } public String getBuffer() { return buffer; } public void setBuffer(String b) { buffer = b; } public boolean hasMoreTokens() { return (current < buffer.length()); } public void readInput() { Scanner sc = new Scanner(System.in); buffer = sc.nextLine(); } public Token getID() { Token t = new Token(); String buf = ""; while (current < buffer.length() && (Character.isLetterOrDigit(buffer.charAt(current)))) { buf += buffer.charAt(current); current++; } t.setType(Token.IDENTIFIER); t.setValue(buf); return t; } public Token getInt() { Token t = new Token(); String buf = ""; while (current < buffer.length() && Character.isDigit(buffer.charAt(current))) { buf += buffer.charAt(current); current++; } if ((current == buffer.length()) || !(Character.isLetter(buffer.charAt(current)))) { t.setType(Token.INTEGER); t.setValue(buf); return t; } else { while (current < buffer.length() && Character.isLetter(buffer.charAt(current))) { buf += buffer.charAt(current); current++; } t.setType(Token.UNKNOWN); t.setValue(buf); return t; } } public Token getSymbol() { Token t = new Token(); if (buffer.charAt(current) == '=') { t.setType(Token.EQUALSSIGN); t.setValue("="); } else if (buffer.charAt(current) == '+') { t.setType(Token.PLUSSIGN); t.setValue("+"); } else if (buffer.charAt(current) == '-') { t.setType(Token.MINUSSIGN); t.setValue(";"); } else { t.setType(Token.UNKNOWN); t.setValue(Character.toString(buffer.charAt(current))); } current++; return t; } public Token getNextToken() { while (buffer.charAt(current) == ' ') { current++; } if (Character.isDigit(buffer.charAt(current))) { return getInt(); } else if (Character.isLetter(buffer.charAt(current))) { return getID(); } else { return getSymbol(); } } public static void testGetInt() { boolean done = false; Scanner sc = new Scanner(System.in); String tempbuf, ans; InputString is = new InputString(); Token t; while (!done) { System.out.print("Enter a buffer: "); tempbuf = sc.next(); is.setBuffer(tempbuf); is.setCurrent(0); t = is.getInt(); System.out.println(t); System.out.print("Would you like to keep testing? (y/n)"); ans = sc.next(); if (ans.equals("n") || ans.equals("N")) { done = true; } } } public static void testGetID() { boolean done = false; Scanner sc = new Scanner(System.in); String tempbuf, ans; InputString is = new InputString(); Token t; while (!done) { System.out.print("Enter a buffer: "); tempbuf = sc.next(); is.setBuffer(tempbuf); is.setCurrent(0); t = is.getID(); System.out.println(t); System.out.print("Would you like to keep testing? (y/n)"); ans = sc.next(); if (ans.equals("n") || ans.equals("N")) { done = true; } } } public static void testGetSymbol() { boolean done = false; Scanner sc = new Scanner(System.in); String tempbuf, ans; InputString is = new InputString(); Token t; while (!done) { System.out.print("Enter a buffer: "); tempbuf = sc.next(); is.setBuffer(tempbuf); is.setCurrent(0); t = is.getSymbol(); System.out.println(t); System.out.print("Would you like to keep testing? (y/n)"); ans = sc.next(); if (ans.equals("n") || ans.equals("N")) { done = true; } } } public static void testGetNextToken() { boolean done = false; Scanner sc = new Scanner(System.in); String tempbuf, ans; InputString is = new InputString(); Token t; while (!done) { System.out.print("Enter a buffer: "); tempbuf = sc.next(); is.setBuffer(tempbuf); is.setCurrent(0); t = is.getNextToken(); System.out.println(t); System.out.print("Would you like to keep testing? (y/n)"); ans = sc.next(); if (ans.equals("n") || ans.equals("N")) { done = true; } } } public static void testHasMoreTokens() { boolean done = false; Scanner sc = new Scanner(System.in); String tempbuf, ans; int c; InputString is = new InputString(); while (!done) { System.out.print("Enter a buffer: "); tempbuf = sc.next(); System.out.print("Enter a counter position: "); c = sc.nextInt(); sc.nextLine(); is.setBuffer(tempbuf); is.setCurrent(c); if (is.hasMoreTokens()) { System.out.println("More tokens available"); } else { System.out.println("No more tokens available."); } System.out.print("Would you like to keep testing? (y/n)"); ans = sc.next(); if (ans.equals("n") || ans.equals("N")) { done = true; } } } public static void main(String args[]) { testGetInt(); testGetID(); testGetSymbol(); testGetNextToken(); testHasMoreTokens(); } }