Read text and add to list - java

I am working on a method that takes a scanner as a parameter, and the values of the text file should be added to the doublyLinkedList. Right now the method is working fine but i have an issue where if it encounters a string in the text file, it stops. I want it in such a way that i skip over any line that has a string i tried using the nextLine() but it didnt work.
public static void addList(Scanner input, DoublyLinkedList list){
Number data=null;
if(input.hasNextLine()){
if(input.hasNextInt()){
data=input.nextInt();
list.addEnd(data);
parseScanner(input, list);
}
else if(input.hasNext()){
input.hasNext();
}
}
}

I think in.hasNext() should be input.hasNext()

I think you should use hasNextInt() method of the scanner. If this method will return false, you can just read string and then again try to read int.
if (input.hasNextInt()) {
// read int
} else {
input.nextLine(); // skip line that is not a number
}

Related

I'm confused the use of scanner.next()in JAVA

My intention is to print all characters in string. And I want to check if one character is "q" or "Q", just break the loop and end the program. Suppose I use "Hello World You" as input, the output can only be
World
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at tictactoe.ScannerDemo.main(ScannerDemo.java:22)
But if I comment that if statement, the output is correct, which is
Hello
World
K
So, I don't know why it happens and how to fix it ?
This is my code:
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World K";
Scanner scanner = new Scanner(s);
while (scanner.hasNext()) {
//if I comment the if statement, the result is normal
if(scanner.next().equalsIgnoreCase("q")){
break;
}
System.out.println(scanner.next());
}
// close the scanner
scanner.close();
}
}
Have a look at scanner.hasNext() and how often you call scanner.next() which is 2x per call to hasNext(). Since next() returns the next element, the first call will return "Hello" and the second returns "World" as you've already noticed.
Now on the second iteration of the loop there's still a "next" element with the value "You" and that is returned by the call to next() in the if-statement. However the next call to next() breaks because there is no more input.
To fix that, call next() only once and keep a reference to the string:
while (scanner.hasNext()) {
String token = scanner.next();
if(token .equalsIgnoreCase("q")){
break;
}
System.out.println(token );
}
Btw, I want to check if one character is "q" or "Q" doesn't match your code as you're only checking entire strings. If this is intended behavior then you're ok but otherwise you'd need to use toCharArray() or charAt(index) to check individual characters (this might be weird though, e.g. if input was "may I ask a question: is this printed?" it would stop at "question").
Have a look at your code
while (scanner.hasNext()) {
//if I comment the if statement, the result is normal
if(scanner.next().equalsIgnoreCase("q")){
break;
}
System.out.println(scanner.next());
}
In the condition of the if statement you are calling next(). This is will not throw any exception cause it is already checked by hasNext() call. But later you are printing the token while not checked by the hasNext() call. Moreover the previous token is lost in the if condition during the next() call.
Try using ..
while (scanner.hasNext()) {
String nextToken = scanner.next();
if (nextToken.equalsIgnoreCase("q")) {
break;
}
System.out.println(nextToken);
}
you're calling next() twice without checking if there's a token left...
try
while (scanner.hasNext()) {
String nextValue = scanner.next();
if(nextValue.equalsIgnoreCase("q")){
break;
}
System.out.println(nextValue);
}

Java - Add all integers from file to ArrayList

I'm trying to read all integers from a file into an ArrayList in the #BeforeClass of a java JUnit test. For testing purposes, I am then simply trying to print all values of the arraylist to the screen. Nothing is being output however. Any input would be greatly appreciated.
public class CalcAverageTest
{
static List<Integer> intList = new ArrayList<Integer>();
#BeforeClass
public static void testPrep() {
try {
Scanner scanner = new Scanner(new File("gradebook.txt"));
while (scanner.hasNextInt()) {
intList.add(scanner.nextInt());
}
for (int i=0;i<intList.size();i++) {
System.out.println(intList.get(i));
}
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
}
(promoting a comment to an answer)
If gradebook.txt is an empty file, or starts with something that does not parse as an int, such as text or comments at the top of the file, then scanner.hasNextInt() will immediately return false, and intList will remain empty. The for loop will then loop over the empty list zero times, and no output will be generated, as observed.
I have some strings to skip over before the integers.
scanner.readLine() can be used to skip over comment lines before the numbers. If it is not a set number of lines that need skipping, or if there are words on the line before the numbers, we would need to see a sample of the input to advise the best strategy for finding the numbers in the input file.
You need to iterate over the file till the last line, so you will need to change the condition in the loop and use .hasNextLine() instead of .nextInt()
while (scanner.hasNextLine()) {
String currLine = scanner.nextLine();
if (currLine != null && currLine.trim().length() > 0 && currLine.matches("^[0-9]*$"))
intList.add(Integer.parseInt(currLine));
}
}
Here, we read each line and store it in currLine. Now only if it contains a numeric value it is added to the intList else it is skipped. ^[0-9]$* is a regex used to match only numeric values.
From the docs, hasNextLine()
Returns true if there is another line in the input of this scanner.
This method may block while waiting for input. The scanner does not
advance past any input.

Printing lines from a file in Java

I am making a method that prints lines in a file which contain a certain word. If the parameter is an empty String, it is supposed to print the entire file.
I've gotten the first part to work. Everything in the "else" statement works great; it scans each line and prints the lines that contain the word in the parameter.
BUT I can't get it to print the whole file when an empty String ("") is entered as the parameter "word". I'm not sure why this is.
public void printLinesWhichContain(String word) {
while (this.reader.hasNextLine()) {
String line = this.reader.nextLine();
if (word.isEmpty()) {
System.out.println(line);
} else {
Scanner lineReader = new Scanner(line);
while (lineReader.hasNext()) {
if (lineReader.next().equals(word)) {
System.out.println(line);
}
}
}
}
}
Once you have the line in String format, you can use the indexOf method to get the index of the word.
I wouldn't create a Scanner for each and every line.
I mean you might want to change the else part as follows.
int indexOfWord = line.indexOf(word);
if (indexOfWord >= 0)
{
System.out.println(line);
}

Not asking for input after the while loop starts again

My program is to enter a substring and return all the books based upon the search.i want to ask the user again to search again from the same records.The code is not asking for the user input for the second time.Kindly help
boolean runnable=true;
while(runnable)
{
System.out.println("\n\nInput Books you wish for search");
String search;
search=br.nextLine();
System.out.println("\n\nBooks by your search");
for(int i=0;i<noOfrecords;i++)
{
if(books[i].toLowerCase().contains(search.toLowerCase()))
{
System.out.println(books[i]);
}
}
System.out.println("\n\nMore Books");
for(int i=0;i<noOfrecords;i++)
{
if(!(books[i].toLowerCase().contains(search.toLowerCase())))
{
System.out.println(books[i]);
}
}
System.out.println("do you wish to search again from the same records?? (y/n)");
char searchagain=br.next().charAt(0);
if(searchagain!='y')
{
runnable=false;
}
else if(searchagain=='y')
{
runnable=true;
}
}
I am sure that you have an extra unconsumed new line character in your buffer. Consume it by adding a line at the end of the while loop after else-if
else if(searchagain=='y')
{
runnable=true;
}
br.nextLine();
When you hit enter after keying the first input, Scanner.nextLine() advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. so, the line seperator \n is still in the buffer, which is been consumed while iterating the loop for the second time.we need to consume it purposefully by invoking br.nextLine(); at the end of the while loop and leave the new line character \n in air and go for second loop for fresh input. This will make it to stop and get the input for the second time.
Let me explain you in a simple way. Your program asks for
Input Books you wish for search
Now, you give Effective Java and press enter. This would become
EffectiveJava\n
\n (unconsumed char) in the end of your input will not be consumed by br.nextLine(). So, we need to explicitly get rid of it at the end of while loop. so, that \n will not be consumed by the br.nextLine() during the second loop and it will ask for your input
Because you are not able to read single character from console - System.in because new bytes are available in stream after Enter key is pressed.
Can be useful:
http://sourceforge.net/projects/javacurses/
http://jline.sourceforge.net/
I made little changes in your code.
public void searchMethod(List<String> books){
Scanner scanner = new Scanner(System.in);
System.out.println("\n\nInput Books you wish for search");
//this code is executed when books is null, so first time you can invoke that method passing as argument simply null.
if(books == null){
while(scanner.hasNext())
books.add(scanner.nextLine());
}
System.out.println("\n\nBooks by your search");
for(String book : books)
{
if(book.toLowerCase().contains(search.toLowerCase()))
{
System.out.println(book);
}
}
System.out.println("\n\nMore Books");
for(String book:books)
{
if(!(book.toLowerCase().contains(search.toLowerCase())))
{
System.out.println(book);
}
}
System.out.println("do you wish to search again from the same records?? (y/n)");
char searchagain=scanner.nextLine();
if(!searchagain.equals("y"))
{
return;
}
else if(searchagain.equals("y"))
{
searchMethod(books);
}
}
}
change next().charAt(0) to nextLine().charAt(). it will work.i think reason for this is next() sperates the token by spaces and read the token one by one from the buffer and stay at the line. when next() reaches the end of line and if u call nextLine(), then nextLine() encounter the end of line and nextLine() return blank line.
char searchagain = br.next().charAt(0);
to
char searchagain = br.nextLine().charAt(0);

Reading Strings next() and nextLine() Java

The problem is I cant read the variable input with next() cause when I try to split (.split" ") every whitespace then the array just get the first two words I type so I had to use keyboard.nextLine() and the splitting process works the way it should work and I get all the words in the array but the problem is that If I use nextLine() then I have to create another keyboard object to read the first variable (answer) and that is the only way I can make it work here is the code
Scanner keyboard=new Scanner(System.in);
Scanner keyboard2=new Scanner(System.in);//just to make answer word
int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).
int current=1;
int left=0,right=0,forward=0,back=0;
for(int count=0;count<answer;count++,current++)
{
String input=keyboard.nextLine();
String array[]=input.split(" ");
for (int counter=0;counter<array.length;counter++)
{
if (array[counter].equalsIgnoreCase("left"))
{
left++;
}
else if (array[counter].equalsIgnoreCase("right"))
{
right++;
}
else if (array[counter].equalsIgnoreCase("forward"))
{
forward++;
}
else if (array[counter].equalsIgnoreCase("back"))
{
back++;
}
}
}
}
Thanks :)
Put keyboard.nextLine() after this line:
int answer=keyboard.nextInt();
This is a common problem that usually happens when you use nextLine() method after nextInt() method of Scanner class.
What actually happens is that when the user enters an integer at int answer = keyboard.nextInt();, the scanner will take the digits only and leave the new-line character \n. So you need to do a trick by calling keyboard.nextLine(); just to discard that new-line character and then you can call String input = keyboard.nextLine(); without any problem.

Categories

Resources