Question: Java My program scans tokens from a file. How can I get it to ignore the numbers at the beginning of each line while scanning
Java
My program scans tokens from a file. How can I get it to ignore the numbers at the beginning of each line while scanning the rest of the line?
Example.
//test.txt
05 REM find greatest common divisor (Euclidean Algorithm) 10 HOME : TEXT 20 LET A = 1071 : LET B = 462 30 IF A < B THEN C = A : A=B : B=C 40 PRINT A,B 50 LET C = A - INT(A/B)*B : REM C = A MOD B (A modulo B) 60 LET A = B : B = C 70 IF B > 0 GOTO 40 80 PRINT "GCG is ";A 90 END
//My program
public class ReadTextFiles2 { public static void main(String[] args) throws FileNotFoundException { //loading the file //change the name of the file accordingly Scanner scan = new Scanner(new File("C:\\Users\\leona\\OneDrive\\Desktop\\BWTest.txt")); String line = ""; String store[]; //reading the file while(scan.hasNext()) line = line + scan.nextLine(); int flag = 1; store = line.split(" ");// Separate the tokens using the split function & save them in store array int len = store.length; int no1 = 1; String s[] = new String[100]; s[0] = "%"; int n = 0; System.out.println(" "); for (int k = 0; k < len; k++) { switch (store[k]) { // Switch loop identifies the different tokens case "int": System.out.println(""); break; case "float": System.out.println(""); break; case "char": System.out.println(""); break; case "double": System.out.println(""); break; case ",": System.out.println(""); break; case ";": System.out.println(""); break; case "+": System.out.println(""); break; case "-": System.out.println(""); break; case "/": System.out.println(" "); break; case "%": System.out.println(""); break; case "*": System.out.println(""); break; case "=": System.out.println(""); break; default: String b = store[k]; char ch[] = b.toCharArray(); if (Character.isDigit(ch[0])) { System.out.println(""); } else { if (Character.isLetter(ch[0])) { flag = 1; for (int u = 0; u < no1; u++) { if (s[u].equals(store[k])) { n = u; flag = 0; } } if (flag != 0) { s[no1] = store[k]; n = no1; no1++; } } } } } System.out.println(" "); System.out.println("No of tokens: " + store.length); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
