0

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&lt;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.

6
  • 3
    Please put more effort into formatting your code. This is horrible to read - always review what a post is going to look like before submitting it. Additionally "I keep getting errors" doesn't provide nearly enough information. Please read tinyurl.com/stack-checklist Commented Aug 1, 2014 at 9:20
  • 1
    Post code you have along with it's errors. People will not merge these programs for you. Commented Aug 1, 2014 at 9:21
  • 1
    you said that you're getting an error. where is the error? Commented Aug 1, 2014 at 9:22
  • have a exit string like -99 or something and loop while your input not equal to that magic number Commented Aug 1, 2014 at 9:23
  • 1
    what do you mean by when I try to merge the two I keep getting errors - where is the merged code? What is the error? Commented Aug 1, 2014 at 9:24

1 Answer 1

2

You have not clarified how do you actually merged your code and what kinds of errors ar eyou getting but your while loop is not good. You need to wait for user input INSIDE an "infinite" loop.

while(true){ //Infinite loop
    Scanner in = new Scanner(System.in);
    while(in.hasNextLine())//Check for input
        System.out.println(in.nextLine()); //Process input
}

This way every time the user pressed 'Enter' you process his input.Of course you can do anything you like with in.nextLine()' orin.next()` and your while loop could have an ending condition cause i doubt you want an 100% infinite loop, but the structure should be the same. Hope this helps.

EDIT: Try the following code:

while(true){

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

} 

Although its quite unlikely you need a true infinite loop, so consider putting some stopping condition in your while loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, and I'm very sorry for this confusion and the lack of information within my post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.