For some reason when I try scan a .txt file, it is failing to find any lines and thus causing the error:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
Code:
File file = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
Scanner scanner = new Scanner(new FileInputStream (file), "UTF-8");
while(scanner.hasNextLine()){
modsList.add(scanner.nextLine());
System.out.println(scanner.nextLine());
}
I do not close the scanner.
modsList is a List that I try add each line to so I can check if a mod exists in chat (user) matches one in the list, however the error is on System.out...
I checked the .txt file exists with the following:
File f = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
if(f.exists() && !f.isDirectory()) {
System.out.println("file exists");
}
Anyone any idea why the lines are not being read?
.txt contents:
abkayckay
kayc01
Thanks, any help appreciated.
You are currently reading two lines instead of one, save the line you read to add to your list and display with the same line.
while(scanner.hasNextLine()){
String line = scanner.nextLine();
modsList.add(line);
System.out.println(line);
}
You call nextLine tice. Change to:
while(scanner.hasNextLine()){
String value = scanner.nextLine()
modsList.add(value);
System.out.println(value);
}
Related
I am trying to read a log file word by word using a scanner and using the code
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
String word = scanner.next();
}
But the problem is that this stops after it reaches the end of the file but I need to read it as it gets generated
I tried this to solve this using the below code
Scanner scanner = new Scanner(file);
while(true){
while(!scanner.hasNext()){
Thread.sleep(1000);
}
String word = scanner.next();
}
But the code does not seem to work and gets stuck in the while loop even when the log file has more data appended to it.
Can someone point out what i am doing wrong.
I made an small snippet for reading a log file for another system. I used BufferedReader instead of Scanner. Because it will run until the end of file and still reading. Instead of using the Scanner Next.
readLine() Doc:
* #return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
And My Snippet
BufferedReader br = new BufferedReader(new InputStreamReader(file));
while (true)
{
strLine = br.readLine();
if(strLine!=null)
{
System.out.println(strLine);
}else{
Thread.sleep(100);
}
}
I am working on homework but currently stuck.
I need to process a file and return number of characters, words, and lines.
I am particularly lost on how I can process the file. Any help would be appreciated.
you need to use Scanner scan = new Scanner(new File(your_file)); and scan the file using while(scan.hasNext()). place your counter inside the loop and count the lines,words and characters.
Try:
if (file.exists()) {
Scanner console = new Scanner(file);
//rest of code
}
I am not sure while am receiving a no such element exception. It seems to be an issue with the scanner not reading my file correctly, but I am not sure where I am going wrong.
I am reading a file, then using the scanner to go line by line. But I get unusual behavior, such as missing lines or filenotfound exceptions when I try to do it.
public static void readMylifeLikeABook(String fileName,int maxItems) {
// Read file line by line with different elements on each line
int bookCount=0;
int movieCount = 0;
Book [] bookItem =new Book[maxItems];
Movie [] movieItem =new Movie[maxItems];
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
Scanner line;
while (scanner.hasNext() && ((bookCount+movieCount)<maxItems)) {
line = new Scanner(scanner.nextLine()); // scan next line
if (line.next().contains("Movie")){
movieItem[movieCount]= new Movie();
movieItem[movieCount].setMediaType("Movie");
movieItem[movieCount].setTitle(scanner.nextLine());
movieItem[movieCount].setRef(scanner.nextLine());
movieItem[movieCount].setPrice(Double.valueOf(scanner.nextLine()));
movieItem[movieCount].setDirector(scanner.nextLine());
movieItem[movieCount].setActor(scanner.nextLine());
System.out.println(movieItem[movieCount].getTitle());
movieCount++;
line.close(); // close line
}
else if (scanner.next().contains("Book")){
bookItem[bookCount]= new Book();
bookItem[bookCount].setMediaType("Book");
bookItem[bookCount].setTitle(scanner.nextLine());
bookItem[bookCount].setRef(scanner.nextLine());
bookItem[bookCount].setPrice(Double.valueOf(scanner.nextLine()));
bookItem[bookCount].setAuthor(scanner.nextLine());
System.out.println(bookItem[bookCount].getTitle());
bookCount++;
line.close(); // close line
}
}
scanner.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
//System.out.println(count);
for (int i=0;i<(bookCount+movieCount);i++) {
System.out.println(bookItem[i] +"\n\n "+ movieItem[i]);
}
}
Hmm, is it possible if you can provide the file being read? This can very well be a problem with that File as if the Scanner object does not a following line to be read, it will throw an exception. Make sure the file has the needed amount of lines.
You call line.next() immediately after constructing line. Do you need to do this? If so, perhaps you need to call hasNext() first to ensure there's a token there.
(I'm pretty sure line.next() throws a NoSuchElementException if there's no token.)
Also, check your code against the file format. Are there blank lines in your file?
I am trying to read contents of a jar file. Below code i have written for this. I have placed the jar file under lib directory(\practice\lib\abc.jar).
final InputStream is = JarReader.class.getResourceAsStream("/abc.jar");
BufferedReader input = new BufferedReader(new InputStreamReader(is));
Scanner scan = new Scanner(input);
while (scan.hasNext()) {
String s = scan.next();
System.out.println(s);
}
scan.close();
every time inputStream coming as null. Below post i have used for my reference
getResourceAsStream returns null
How to read a file from jar in Java?
String s = scan.next();
This line in your code will read the file line after line. It's up to you on how to use those lines. You might want to create a StringBuilder and keep appending those lines. Or store it as plain String or something else if you prefer.
But you already have the file with you (in the form of individual lines).
I'm trying to read in a .txt file in but when i use debugger it gets stuck on nextline? Is there some logic error that im doing? It's all being stored into an array through multiple objects:
public static File readFileInfo(Scanner kb)throws FileNotFoundException
{
System.out.println("Enter your file name");
String name = "";
kb.nextLine();
name = kb.nextLine();
File file = new File(name);
return file;
}
The scanner I passed into it is:
Scanner fin = null, kb = new Scanner(System.in);
File inf = null;
inf = FileUtil.readFileInfo(kb);
fin = new Scanner(inf);
You're reading from two different "files" here:
System.in, the standard input (or "terminal"), which you're using to ask the user for a filename
the file with the name you get from the user
When you call name = kb.nextLine();, you're asking the parameter (the Scanner built with System.in) for its next line. Generally, that will actually block ("hang") until it receives another line of input (the filename) from the user. If running from a command line, enter your text into that window; if running in an IDE, switch to the Console tab and enter it there.
As quazzieclodo noted above, you probably only need to call readLine once.
After that, you can open up your second Scanner based on the File that readFileInfo returns, and then you're actually reading from a text file as expected.
Assuming that your intention is to use Scanner to read a text file:
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}