Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting an infinite loop
    primarykey
    data
    text
    <p>I have just made a program that is supposed to scan a file and count the number of lines, vowels, and many other things but when I run it, it goes into an infinite loop. I'm pretty sure it has to do with the <code>.hasNext()</code> and <code>.hasNextLine()</code> methods which I am not very familiar with</p> <pre><code>import java.util.*; import java.io.*; /** * * */ public class Wordcount1 { public static void main(String[] args) { int vowels=0; int punctuation=0; int sentences=0; int words=0; int lines=0; int alphaNumeric=0; try{ Scanner input = new Scanner(System.in); System.out.println("Enter file name: "); File file = new File(input.nextLine()); Scanner fileReader = new Scanner(file); while(fileReader.hasNextLine()){ lines +=1; } while(fileReader.hasNext()){ { fileReader.next(); words +=1; } String word = fileReader.next(); for (int i=0; i&lt;word.length();i++){ char ch= word.charAt(i); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') vowels +=1; if((ch=='!'||ch=='.'||ch=='?')) sentences +=1; if(Character.isLetterOrDigit(ch)) alphaNumeric +=1; switch(ch){ case ',': punctuation +=1; break; case '[': punctuation +=1; break; case ']': punctuation +=1; break; case ':': punctuation +=1; break; case '`': punctuation +=1; break; case '-': punctuation +=1; break; case '!': punctuation +=1; break; case '_': punctuation +=1; break; case '(': punctuation +=1; break; case ')': punctuation +=1; break; case '.': punctuation +=1; break; case '?': punctuation +=1; break; case '"': punctuation +=1; break; case ';': punctuation +=1; break; } } } System.out.println("The number of words in the file name: " + words); System.out.println("The number of lines in the file name: " + lines); System.out.println("The number of alphanumeric characters: " + "in the file name: " + alphaNumeric); System.out.println("The number of sentences in the file name: " + sentences); System.out.println("The number of vowels in the file name: " + vowels); System.out.println("The number of punctuations in the file name: " + punctuation); }catch(FileNotFoundException e){ e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload