I have a problem with counting the number of input lines and the number of operands. Can someone please tell me what is wrong?...

When my input is :
System.out.println("\n\nReserved Words: " + ReserveWords);
System.out.println("Unique Reserved Words: " + UReserveWords);

The number of lines is 11 and I don't know why. The number of lines is supposed to be 2.

import java.util.*;
import java.lang.*;
import java.io.*;
public class Exer6
{
	public static void main(String args[])
	{
		String[] dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, dummy7;
		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[] doubles = {"++", "--", "+=", "-="};
		String input, awaw;
		int a=0, b=0, c=0, d=0, e=0, f=0, g=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];
		dummy6 = new String[1000000];
		dummy7 = 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();
		List dx = new ArrayList();
		
		Collections.addAll(rw, reservew);
		Collections.addAll(op, operators);
		Collections.addAll(dx, doubles);
    	
    	Scanner myObj = new Scanner(System.in);
    	
		System.out.println("Enter your program: ");
		input=myObj.next();
			
		do
		{
			if(input.equals("*END*"))
				break;

			StringTokenizer resword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer uresword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer oper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer uoper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer rand = new StringTokenizer(input, " ;()[].,");
			StringTokenizer urandt = 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]) && !dx.contains(dummy5[e]))
					Operands = Operators + 1;
				
				else
					Operands++;
					
				e++;
			}
			
			while(urandt.hasMoreTokens())
			{
				dummy6[f]=urandt.nextToken();
				
				if(op.contains(dummy6[f]) && !dx.contains(dummy6[f]))
					UOperands = Operators+1;

				
				else if(f!=0 && op.contains(dummy6[f]) && !dx.contains(dummy6[f]) && !urand.contains(dummy6[f-1]))
				{
					UOperands++;
					urand.add(dummy6[f-1]);
				}
				
				f++;
			}
			
			input=myObj.next();
			
			if(input.equals("*END*"))
				break;
				
                        lines++;

		}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

Here is correction, instead of next you should use nextLine

import java.util.*;
import java.lang.*;
import java.io.*;
public class Exer6
{
	public static void main(String args[])
	{
		String[] dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, dummy7;
		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[] doubles = {"++", "--", "+=", "-="};
		String input, awaw;
		int a=0, b=0, c=0, d=0, e=0, f=0, g=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];
		dummy6 = new String[1000000];
		dummy7 = 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();
		List dx = new ArrayList();

		Collections.addAll(rw, reservew);
		Collections.addAll(op, operators);
		Collections.addAll(dx, doubles);

    	Scanner myObj = new Scanner(System.in);

		System.out.println("Enter your program: ");

		do
		{
			input=myObj.nextLine();
			if(input.equals("*END*")){
				break;
			}

			else{
			StringTokenizer resword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer uresword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer oper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer uoper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer rand = new StringTokenizer(input, " ;()[].,");
			StringTokenizer urandt = new StringTokenizer(input, " ;()[].,");

			while(resword.hasMoreTokens())
			{
				String test = resword.nextToken();
				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]) && !dx.contains(dummy5[e]))
					Operands = Operators + 1;

				else
					Operands++;

				e++;
			}

			while(urandt.hasMoreTokens())
			{
				dummy6[f]=urandt.nextToken();

				if(op.contains(dummy6[f]) && !dx.contains(dummy6[f]))
					UOperands = Operators+1;


				else if(f!=0 && op.contains(dummy6[f]) && !dx.contains(dummy6[f]) && !urand.contains(dummy6[f-1]))
				{
					UOperands++;
					urand.add(dummy6[f-1]);
				}

				f++;
			}

			/*input=myObj.next();

			if(input.equals("*END*"))
				break;*/

                        lines++;
			}

		}while(myObj.hasNextLine()); //want to check for whole line, not just for token



		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);
	}
}

Here is correction, instead of next you should use nextLine

import java.util.*;
import java.lang.*;
import java.io.*;
public class Exer6
{
	public static void main(String args[])
	{
		String[] dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, dummy7;
		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[] doubles = {"++", "--", "+=", "-="};
		String input, awaw;
		int a=0, b=0, c=0, d=0, e=0, f=0, g=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];
		dummy6 = new String[1000000];
		dummy7 = 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();
		List dx = new ArrayList();

		Collections.addAll(rw, reservew);
		Collections.addAll(op, operators);
		Collections.addAll(dx, doubles);

    	Scanner myObj = new Scanner(System.in);

		System.out.println("Enter your program: ");

		do
		{
			input=myObj.nextLine();
			if(input.equals("*END*")){
				break;
			}

			else{
			StringTokenizer resword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer uresword = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");
			StringTokenizer oper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer uoper = new StringTokenizer(input, " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@[]{}();:");
			StringTokenizer rand = new StringTokenizer(input, " ;()[].,");
			StringTokenizer urandt = new StringTokenizer(input, " ;()[].,");

			while(resword.hasMoreTokens())
			{
				String test = resword.nextToken();
				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]) && !dx.contains(dummy5[e]))
					Operands = Operators + 1;

				else
					Operands++;

				e++;
			}

			while(urandt.hasMoreTokens())
			{
				dummy6[f]=urandt.nextToken();

				if(op.contains(dummy6[f]) && !dx.contains(dummy6[f]))
					UOperands = Operators+1;


				else if(f!=0 && op.contains(dummy6[f]) && !dx.contains(dummy6[f]) && !urand.contains(dummy6[f-1]))
				{
					UOperands++;
					urand.add(dummy6[f-1]);
				}

				f++;
			}

			/*input=myObj.next();

			if(input.equals("*END*"))
				break;*/

                        lines++;
			}

		}while(myObj.hasNextLine()); //want to check for whole line, not just for token



		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);
	}
}

Thanks. My only problem now are the rules in counting operands.

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.