I'm trying to implement a infinite while loop into my word frequency java code. The goal of this code was to create a program using main method java to analysis a piece text which has been entered from a user.
They do this by entering the text into a scanner which is then analysed by the program.
import java.util.Scanner;
public class WordFreq {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter text: ");
String s;
s = input.nextLine();
System.out.println("" + s);
String[] strings = s.split(" ");
int[] counts = new int[6];
for(String str : strings)
if(str.length() < counts.length) counts[str.length()] += 1;
for(int i = 1; i < counts.length; i++)
System.out.println(i + " letter words: " + counts[i]);
input.close();}
}
The user then can re enter a piece of text to be analysed over and over again. I have this piece of code, which has a infinite while already in, but when I try to merge the two I keep getting errors.
import java.util.Scanner;
public class WhileLoop {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String line="";
while((line=scan.nextLine())!=null)
{
String tokens [] = line.split(" ");
for(int i=0;i<tokens.length;i++)
{
System.out.println(tokens[i]);
}
}
}
I'm not the Best at java and I'm not expecting anyone to just hand me code, but if someone could give me a hint in what I should do or just point me the right direction it would be greatly appreciated.
when I try to merge the two I keep getting errors- where is the merged code? What is the error?