A Java Scanner Exception - java

I am trying to write a program where I take a textfile and copy it to another file. In this other file I want one word on the first line, two words on the second, three on the third and so on.
I am having some trouble with the Scanner class however. In the program below I keep getting a NoSuchElementException for line 14. I thought this was because I closed the Scanner in the while loop or something but even when I left out 'in.close()' I kept getting the same error.
Can anybody help me with this?
Thanks in advance.
import java.io.*;
import java.util.*;
public class WordPyramid {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File(args[0]);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(args[1]);
int s = 1;
int i = 0;
while (in.hasNext()) {
if (s >= i) {
for (i = 1; i <= s; i++) {
out.print(in.next());
out.print(" ");
}
out.println("");
s++;
}
}
in.close();
out.close();
}
}

A NoSuchElementException is thrown when there is no next() element. While you check to see if the file hasNext() at the start of each layer of the pyramid, you also need to check it before you call next() in the for loop. Your exception is thrown in the for loop because the next layer of the pyramid may require a larger number of words than remains in the file causing next() to try and get an element that is not there.
To fix it, wrap the interior of you for loop with if(in.hasNext()).

hasNext() checks if there is one more token in the scanner's stream. You are checking if there is one more token, and then assuming there are even more than one in your for loop. I would modify your for loop to look like this:
for (i = 1; i <= s && in.hasNext(); i++)

I would like to suggest that your loops are perhaps over-complicated.
Here is what I think is an easier answer, and avoids your exception:
File inputFile = new File(args[0]);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(args[1]);
int s = 1;
int i = 0;
while (in.hasNext()) {
out.print(in.next() + " ");
i++;
if (i == s)
{
// Start the next line
out.println();
s++;
i = 0;
}
}

Related

How to scan a file for a specific letter in Java

I need to take a file that a user chooses and scan that file for a letter that a user chooses, and then output how many times the user's letter appeared in the file.
I know how to get the user input and get the user to select a file, as well as scanning the file, but I cannot figure out a way to check each character within a file for a specific letter. The closest I have been able to come is this:
public class FileLetterCounter
{
public static void main(String[] args) throws IOException
{
int count = 0, stringLength;
String file, a = "a";
Scanner fileScanner, letterScan;
ArrayList<String> line = new ArrayList<String>();
fileScanner = new Scanner(new File("lab6.txt"));
while (fileScanner.hasNext())
{
line.add(fileScanner.next());
for (int index = 0; index < line.length(); index ++)
{
if (line.get(index).contains(a));
{
count++;
}
}
}
}
}
This doesn't work because the length() method does not work on an ArrayList, and I am unsure of how to approach the problem. I am asking this question because I found a similar one, but the recommended solution was to use what I have right now in my for loop (line.length()), but this won't work.
Instead of adding it to the list, just scan the text into a string, iterate each character of the string to check if the character matches with the search character, and increase the value of count for each match.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
int count = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the char to search: ");
char searchChar = keyboard.next().charAt(0);
Scanner fileScanner = new Scanner(new File("lab6.txt"));
while (fileScanner.hasNext()) {
String text = fileScanner.next();
for (int index = 0; index < text.length(); index++) {
if (text.charAt(index) == searchChar) {
count++;
}
}
}
System.out.println("The character " + searchChar + " appears " + count + " times in the file.");
fileScanner.close();
}
}
Look at this implementation with Streams. Looks pretty nice to me. Additionally do not forget to provide Charset, otherwise you could get unexpected results.
public static long countCharacterInFile(Path file, char ch, Charset charset) throws IOException {
try (Stream<String> stream = Files.lines(file, charset)) {
return stream.map(String::codePoints)
.flatMap(IntStream::boxed)
.filter(c -> c == ch)
.count();
}
}
Output:
Path file = Paths.get("lab6.txt");
System.out.println(countCharacterInFile(file, 'e', StandardCharsets.UTF_8)); // 666
Assuming you are trying to search a character in the whole file. Modified the code by removing all those unnecessary variables. Also I don't see any use of adding each line to a list of strings.
Idea is to scan through each line and increment count if the current character character matches your character
public class FileLetterCounter
{
public static void main(String[] args) throws IOException
{
int count = 0;
char targetLetter = 'a'; //define whatever you want or take it from user input
Scanner fileScanner = new Scanner(new File("lab6.txt"));
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
for(int i=0; i<line.length(); i++) {
if(line.charAt(i) == targetLetter) {
count++;
}
}
}
System.out.println(count);
}
}

Java Scanner's nextLine() method gives no line found

I have wasted hours with this simple problem but I could not figure out why nextLine() cannot find the next line. Please help me, thank you!
I tried this code: https://www.geeksforgeeks.org/scanner-nextline-method-in-java-with-examples/ as an experiment and naturally it worked but my own will not.
Variable "test" is copied from the file.
Code portion:
ObservableList adatok;
#Override
public void initialize(URL url, ResourceBundle rb) {
int lines = 0;
try {
File f = new File("C:\\Users\\EDU_BYQN_0965\\Documents\\NetBeansProjects\\JSZ_SB\\src\\jsz_sb\\fokonyvi_kivonat.txt");
String test = "113,Vagyoni értékű jogok,3600,0,\n" +
"1173,Vagyoni értékű jogok értékhelyesbítése,360,0,\n" +
"1193,Vagyoni értékű jogok értékcsökkenése,0,2400,\n" +
"5,t,5,5,";
Scanner s = new Scanner(f);
while (s.hasNext() && s.nextLine() != null) lines++;
String[][] array = new String[lines][4];
String[] temporary = new String[4];
for (int i = 0; i < lines; i++) {
temporary = s.nextLine().split(",");
for (int j = 0; j < 4; j++) {
array[i][j]=temporary[j];
adatok = FXCollections.observableArrayList(
new TrialBalance(array[i][0], array[i][1], Integer.parseInt(array[i][2]), Integer.parseInt(array[i][3])));
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FXML_scene2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
Array "temporary" should contain the first line of the file, leastwise at the first loop and runtime error should not appear.
You exhaust the Scanner in this line:
while (s.hasNext() && s.nextLine() != null) lines++;
It stops because there are no more lines. (Note that the check hasNext() pairs with a call to next(), and hasNextLine() pairs with nextLine()).
So, if you try to read any more lines from the Scanner, there is nothing more to read.
You either need to create a new instance of the Scanner; or use a data structure that you don't need to know the size of a-priori, like a List (or resize your array as required; but there is little point in doing this "by hand" when ArrayList does that for you transparently).

Java scanner reading null from my text file?

I'm writing some code to read an input file of book titles, and putting the read lines into an array and trying to print out the array. But when I try to print out the array, it just returns 'null' for each read line. I'm not sure what I'm doing wrong or what my code is doing. Any suggestions? Thanks!
Code:
import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
public static void main(String args[]) throws IOException{
int lineCount = 0;
File inputFile = new File("bookTitles.inp.txt");
Scanner reader = new Scanner(inputFile);
while(reader.hasNextLine()) {
reader.nextLine();
lineCount++;
}
String[] bookArray = new String[lineCount];
while (reader.hasNextLine()) {
for (int i = 0; i < lineCount; i++) {
bookArray[i] = reader.next();
}
}
for (int k = 0; k < lineCount; k++) {
System.out.println(bookArray[k]);
}
reader.close();
inputFile.close();
}
}
My text file I'm reading from is 20 book titles, all on different lines.
My output on the terminal is 20 lines of null.
Lets break this down:
This reads every line of the input file, counts each one, and then discards them:
while(reader.hasNextLine()) {
reader.nextLine();
lineCount++;
}
You are now at the end of file.
Allocate a string array that is large enough.
String[] bookArray = new String[lineCount];
Attempt to read more lines. The loop will terminate immediately because reader.hasNextLine() will return false. You are already at the end of file.
So you the statement assigning to bookArray[i] won't be executed.
while (reader.hasNextLine()) {
for (int i = 0; i < lineCount; i++) {
bookArray[i] = reader.next();
}
}
Since bookArray[i] = ... was never executed above, all of the array elements will still be null.
for (int k = 0; k < lineCount; k++) {
System.out.println(bookArray[k]);
}
One solution is to open and read the file twice.
Another solution is to "reset" the file back to the beginning. (A bit complicated.)
Another solution would be to use a List rather than an array so that you don't need to read the file twice.
Another solution is to search the javadocs for a method that will read all lines of a file / stream as an array of strings.
(Some of these may be precluded by the requirements of your exercise. You work it out ... )
The nested loop in step 3 is also wrong. You don't need a for loop inside a while loop. You need a single loop that "iterates" the over the lines and also increments the array index (i). They don't both need to be done by the loop statement itself. You could do one or the other (or both) in the loop body.
Stephen C has already pointed out the main problems with your logic. You're trying to loop twice through the file but you've already reached the end of the file the first time. Don't loop twice. "Merge" both the while loops into one, remove that for loop inside the while loop and collect all the book titles. You can then use the size of the list to print them later on. My Java might be rusty but here it goes -
import java.io.*;
import java.util.*;
public class LibraryInputandOutputs {
public static void main(String args[]) throws IOException {
// int lineCount = 0; - You don't need this.
File inputFile = new File("bookTitles.inp.txt");
Scanner reader = new Scanner(inputFile);
// Use an array list to collect book titles.
List<String> bookArray = new ArrayList<>();
// Loop through the file and add titles to the array list.
while(reader.hasNextLine()) {
bookArray.add(reader.nextLine());
// lineCount++; - not needed
}
// Not needed -
// while (reader.hasNextLine()) {
// for (int i = 0; i < lineCount; i++) {
// bookArray[i] = reader.next();
// }
// }
// Use the size method of the array list class to get the length of the list
// and use it for looping.
for (int k = 0; k < bookArray.size(); k++) {
System.out.println(bookArray[k]);
}
reader.close();
inputFile.close();
}
}
I agree with Stephen C. In particular, using a List is usually better than an array because it's more flexible. If you need an array, you can always use toArray() after the List is filled.
Are your book titles on separate lines? If so you might not need a Scanner class, and could use something like a BufferedReader or LineNumberReader.

Java - How to store values of a text file into an array using Scanner?

import java.io.*;
import java.util.*;
import java.text.*;
public class textFile {
public static void main(String args[]) throws IOException {
Scanner sf = new Scanner(new File("C:\\temp_Name\\DataGym.in.txt"));
int maxIndx = -1;
String text[] = new String[1000];
while (sf.hasNext()) {
maxIndx++;
text[maxIndx] = sf.nextLine();
}
sf.close();
double average[] = new double[100];
for (int j = 0; j <= maxIndx; j++) {
Scanner sc = new Scanner(text[j]);
int k = 0;
while (k <= 10) { //attempt to store all the values of text file (DataGym.in.txt) into the array average[] using Scanner
average[k] = sc.nextDouble();
k++;
}
}
}
}
My code doesn't work and keeps giving me this error at the place where I store sc.nextDouble() into the k element of the array:
java.util.NoSuchElementException:
null (in java.util.Scanner)
You should check out the Scanner API. It is suspicious that you have a call to Scanner.hasNext() with a following call to Scanner.nextLine(). There are complimentary calls to check and then get from a Scanner. For instance if you really want the next line then you should check if Scanner.hasNextLine() before calling Scanner.nextLine(). Similarly you call Scanner.nextDouble() without preceding it with a call to Scanner.hasNextDouble().
Also like some of the comments mention you should use a debugger or print statements to see if you are getting what you expect you should be getting. Steps like this are necessary to debugging.
For instance after sf.close you could use System.out.println(Arrays.toString(text)) and judge if the array contains what you expect.
Hope this helps your debugging.

Simple issue about possible to re use java Scanner?

I am still new to java and is it possible to re use the Scanner object?
The below example is I am reading a file to do characters, words and lines count. I know there must be a better way to do counting with one scanner object only but that is not the main point. I just wonder why there is input.close() but no input.open() or input.reset etc.. Since I am actually reading the same file, is it possible to create only one Scanner object and pass for 3 methods to use? Thanks
public class Test {
/**
* #throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
File file = new File("demo.java");
Scanner input = new Scanner(file);
Scanner input2 = new Scanner(file);
Scanner input3 = new Scanner(file);
int lines = 0;
int words = 0;
int characters = 0;
checkCharacters(input3);
checkLines(input);
checkwords(input2);
}
private static void checkLines(Scanner input) {
int count = 0;
while (input.hasNext()) {
String temp = input.nextLine();
String result = temp;
count++;
}
System.out.printf("%d lines \n", count);
}
private static void checkwords(Scanner input2) {
int count = 0;
while (input2.hasNext()) {
String temp = input2.next();
String result = temp;
count++;
}
System.out.printf("%d words \n", count);
}
private static void checkCharacters(Scanner input3) {
int count = 0;
while (input3.hasNext()) {
String temp = input3.nextLine();
String result = temp;
count += temp.length();
}
System.out.printf("%d characters \n", count);
}
}
No, there's no way to reset the Scanner from a method on the Scanner. You might be able to do it if you passed in an InputStream into the scanner and then reset the stream directly but I don't think it's worth it.
You seem to be parsing the same file 3 times and processing the same input 3 times. That seems like a waste of processing. Couldn't you perform all 3 counts at once?
private static int[] getCounts(Scanner input) {
int[] counts = new int[3];
while(input.hasNextLine()){
String line = input.nextLine();
counts[0]++; // lines
counts[2]+=line.length(); //chars
//count words
//for simplicity make a new scanner could probably be better
//using regex or StringTokenizer
try(Scanner wordScanner = new Scanner(line)){
while (wordScanner.hasNext()) {
wordScanner.next();
count[1] ++; //words
}
}
}
return counts;
}
Of course the Object Oriented way would be to return a new object named something like Counts with methods to getNumLines(), getNumChars() etc.
EDIT
One thing to note, I kept the calculations the same as you had in the original question. I'm not sure if the counts will always be accurate especially characters since Scanner may not return all end of line characters so the chars count may be off and the number of lines may be off if there are consecutive blank lines? You would need to test this.
No it is not possible because as the documentation says
void close()
throws IOException
Closes this stream and releases any system resources associated with
it. If the stream is already closed then invoking this method has no
effect.
Once a resource is relaesed there is no way to get it back, untill you have a reference to it , which is actually closed

Categories

Resources