How can I read multiple line inputs? I want to create a program that will ask a user for a program code and it will count all the reserve words used in the program. Every time I copy and paste a code for the input, it only reads the first line. Help!

import java.util.*;
import java.lang.*;
import java.io.*;
public class Exer6
{
	public static void main(String args[])
	{
		String[] dummy;
		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 input;
		int a=0, x=0, z=0, y=0;
		int ReserveWords=0;
	
		dummy = new String[1000000];
		
		List rw = new ArrayList();
		Collections.addAll(rw, reservew);
    	
    	Scanner myObj = new Scanner(System.in);
    	
    	
		System.out.println("Enter your program: ");
		input=myObj.nextLine();
		
		StringTokenizer inputF = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");

		while(inputF.hasMoreTokens())
		{
			dummy[a]=inputF.nextToken();
			
			if(rw.contains(dummy[a]))
				ReserveWords++;
					
			a++;				
		}
		
		System.out.println("\n\nReserve Words: " + ReserveWords);
	}
}

Edited 5 Years Ago by pmark019: n/a

You need another loop so that line 23 input=myObj.nextLine(); is inside a loop that iterates until the last line has been entered.

You need another loop so that line 23 input=myObj.nextLine(); is inside a loop that iterates until the last line has been entered.

I've done it already but the loop won't stop. Here's my code.

do
		{
			
		StringTokenizer inputF = new StringTokenizer(input, " \\/1234567890!@~$%^&*()_+:;|>.?<,[]{}-=`");

		while(inputF.hasMoreTokens())
		{
			dummy[a]=inputF.nextToken();
			
			if(rw.contains(dummy[a]))
				ReserveWords++;
					
			a++;				
		}
		
		input=myObj.nextLine();
		
		}while(myObj.hasNext();

pmark, James said you need ANOTHER loop. not ONE loop. and he is right. you are reading one single line of your input file on line 23, and then iterating through that line with your loop, and that's it. if you want to read each line, then do as James suggested.

The do/while is a perfectly good way to go - the problem now is terminating the loop.
You can define some "end of file" thing that the user enters when the input is finished (eg "*END*"), then break out of the loop when you see it... eg

input=myObj.nextLine();
   if (input.equals("*END*")) break;
}while(myObj.hasNext();

The do/while is a perfectly good way to go - the problem now is terminating the loop.
You can define some "end of file" thing that the user enters when the input is finished (eg "*END*"), then break out of the loop when you see it... eg

input=myObj.nextLine();
   if (input.equals("*END*")) break;
}while(myObj.hasNext();

thanks!...It's working now...my error was instead of .equals, I used ==...

This article has been dead for over six months. Start a new discussion instead.