I want to count the number of lines that are inputed in my program but I don't know how. Can someone please help me.

Here's my code:

import java.util.*;
import java.lang.*;
import java.io.*;
public class Exer6
{
	public static void main(String args[])
	{
		String[] dummy1, dummy2, dummy3, dummy4, dummy5;
		String[] reservew = {"abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"};
		String[] operators = {"=", "-", "+", "*", "/", "%", "++", "--", ">", "<", ">=", "<=", "==", "!=", "&&", "||", "&", "|", "!", "^", "&", "|", "^", ">>", ">>>", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "?:"};
		String input;
		int a=0, b=0, c=0, d=0, e=0, f=0, x=0, z=0, h=0, i=0, y=0, lines=0;
		int ReserveWords=0, UReserveWords=0, UOperators=0, Operators=0, Operands=0, UOperands=0;
	
		dummy1 = new String[1000000];
		dummy2 = new String[1000000];
		dummy3 = new String[1000000];
		dummy4 = new String[1000000];
		dummy5 = new String[1000000];
		
		List rw = new ArrayList();
		List op = new ArrayList();
		List urw = new ArrayList();
		List uop = new ArrayList();
		List ra = new ArrayList();
		List urand = new ArrayList();
		
		Collections.addAll(rw, reservew);
		Collections.addAll(op, operators);
    	
    	Scanner myObj = new Scanner(System.in);
    	
		System.out.println("Enter your program: ");
		input=myObj.next();
			
		do
		{
			StringTokenizer resword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer uresword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer oper = new StringTokenizer(input, " abcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer uoper = new StringTokenizer(input, " abcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer rand = new StringTokenizer(input, " ;()[].,");
			
			while(resword.hasMoreTokens())
			{
				dummy1[a]=resword.nextToken();
			
				if(rw.contains(dummy1[a]))
					ReserveWords++;
					
				a++;				
			}
			
			while(uresword.hasMoreTokens())
			{
				dummy2[b]=uresword.nextToken();
				
				if(rw.contains(dummy2[b]))
				{					
					if(!urw.contains(dummy2[b]))
					{
						UReserveWords++;
						urw.add(dummy2[b]);
					}
				}
				
				b++;				
			}
			
			while(oper.hasMoreTokens())
			{
				dummy3[c]=oper.nextToken();
				
				if(op.contains(dummy3[c]))
					Operators++;
				
				c++;
			}
			
			while(uoper.hasMoreTokens())
			{
				dummy4[d]=uoper.nextToken();
				
				if(op.contains(dummy4[d]))
				{					
					if(!uop.contains(dummy4[d]))
					{
						UOperators++;
						uop.add(dummy4[d]);
					}
				}
				
				d++;				
			}
			
			while(rand.hasMoreTokens())
			{
				dummy5[e]=rand.nextToken();
				
				if(op.contains(dummy5[e]) && !dummy5[e++].equals(";"))
					Operands+=2;
				
				e++;
			}
			
			input=myObj.next();
			
			lines++;
			
			if(input.equals("*END*"))
				break;
			
		}while(myObj.hasNext());
		
		System.out.println("\n\nReserved Words: " + ReserveWords);
		System.out.println("Unique Reserved Words: " + UReserveWords);
		System.out.println("Operators: " + Operators);
		System.out.println("Unique Operators: " + UOperators);
		System.out.println("Operands: " + Operands);
		System.out.println("Number of Lines: " + lines);
	}
}

Recommended Answers

All 2 Replies

With an int variable and the ++ operator after every call to next()?

With an int variable and the ++ operator after every call to next()?

I've done that already, but every time my input is System.out.println(" Number of reserve words: " + Reservewords)...the number of lines is 6...I don't know why...If my input is the do...while loop, the number of lines are correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.