JAVA 7
Jeremy Guest on 29th March 2022 01:05:06 AM
  1. /*
  2. Below is the description for how my scanner generates an integer where  kind is 'NUM'
  3. The first thing to look at is my next() function, to decide whether the first character in a token being read by the buffered reader is a digit. If this is true, it will run the function that I have named munchNUM(). In this function, we will set our kind to NUM right away. This is because we know that we are looking at a number from before because the first character in the token was a digit(number). I then set a local variable that I named position and set that equal to the global variable that I have named charPos, which represents the character position we are reading on the given line. Code described from the next() method is printed below.
  4. */
  5.  
  6. // if character is a number, run munchNum() method
  7. } else if (Character.isDigit(lineDetails.charAt(charPos))) {
  8.         munchNum();
  9. }
  10.  
  11. /*
  12. Here I implement a while loop that has 3 conditions. It must be "within bounds", all characters being read before there is a space must be a digit(number), and it will stop if the next character is whitespace, because this will signify the end of the token. As the while loop runs it will add the digit being read to the token. Then it will increment the position to the next character in the line, and the program will repeat executing while loop again. Once the token is complete and all of its digit characters have been added to the token, munchNum() will terminate, and the printAll() method will print the current token with its line position, character position, kind (in this case it is NUM because we're working with integers) and will print the integer. My munchNUM() method is printed below showing what was described.
  13. */
  14.  
  15. // want to create a token that is of kind NUM
  16. public static void munchNum() {
  17.         kind = "NUM";        int position = charPos;        while ((inBounds(position)) &&      Character.isDigit(lineDetails.charAt(position)) && (!   Character.isWhitespace(lineDetails.charAt(position)))) {
  18.                 token = token + lineDetails.charAt(position);
  19.                 position++;
  20.         } 
  21. }
  22.  
  23. /*
  24. I mentioned that the character position must be within bounds. This means that the character position must be less than the length of the entire line currently being read. The function inBounds() will ensure that we are not reading past what needs to be read on that line. This is not considered whitespace because there is nothing that comes after the end of the line, so without this, it will attempt to keep reading the empty space in memory causing an exception. I have a boolean function named inBounds that will return true only if the given variable position is less than the length of the lineDetails variable which holds all the characters on the given line being read. If this returns false, then the while loop from my munch function will end. This code is printed below
  25. */
  26.  
  27. // checks to see if character position is less than the line length. if it is, return true
  28. public static boolean inBounds(int position) { 
  29.         return (position < lineDetails.length());
  30. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.