Short Notes on Java Scanner
Contrary to the misconception that a Java Scanner is only for beginners while the Reader classes are much more powerful and advance. I strongly disagree to that belief. Java Scanner is there to make our life easier however it also encapsulates a lot of powerful methods that is more advance than those in BufferedReader. Because of ease of use this class is offering, this opens up a lot of discussion in several java forums. But I am telling now, if you have not yet learned the usage of Java Scanner, start learning it now because sooner or than later, its implementation will drastically replaced BufferedReader.
While browsing the net the other day, I came across an interesting topic, and I got the itch to solve it myself. Below code is the taken info from the forum.
Why won't this extremely simple if statement work?
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Container;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Type yes");
input = scanner.next();
if (command == "yes"){
System.out.println("Your input is correct");
}
}
The discussion is getting out of hand, maybe because its an open forum and there are a lot of egoistic wannabe java newbie where in fact the problem is very simple. It's too funny how it goes in reddit. Ooops sorry, I just really hate that community. The people there are so damn arrogant. Though the discussion are not healthy, the questions are totally legit questions and its a good source of material for discussion. Meanwhile let me try to answer the issue at hand.
More on Java Scanner: http://javatutorialhq.com/java/util/scanner-class-tutorial/
You know what is the first issue here? Although it's not show stopper, however its bad practice so I will point that out first. There are too many import statement been introduced. Delete all the import statements except import java.util.Scanner. This is not really an issue but it is a good practice not to load classes which we don't need. It will enhance the readability of the source code and it will conserve system resources.
Second issue is the use of this statement command = scanner.next(); There is a know behaviour of java scanner that if you read from console the newline at the end of the input is not getting consumed. This behaviour is evident if you require multiple input from console. I would really suggest to use nextLine() instead. Anyway as far as the source code above is concern, there would not be an issue here because it is only asking for single input.
Third which is the the main issue is the use of the statement command == "yes". This is the main problem here. Remember that when you compare two objects which in this case a String, the use of equals is encouraged.
The last issue is that the scanner object is not close thus it may result to buffer overflow. Just add scanner.close() at the end of the statement and the source code is complete.
Below code is the proper implementation
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String command;
System.out.println("Type yes");
command = scanner.nextLine();
if (command.equals("yes")){
System.out.println("Your input is correct");
}
scanner.close();
}
While browsing the net the other day, I came across an interesting topic, and I got the itch to solve it myself. Below code is the taken info from the forum.
Why won't this extremely simple if statement work?
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Container;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Type yes");
input = scanner.next();
if (command == "yes"){
System.out.println("Your input is correct");
}
}
The discussion is getting out of hand, maybe because its an open forum and there are a lot of egoistic wannabe java newbie where in fact the problem is very simple. It's too funny how it goes in reddit. Ooops sorry, I just really hate that community. The people there are so damn arrogant. Though the discussion are not healthy, the questions are totally legit questions and its a good source of material for discussion. Meanwhile let me try to answer the issue at hand.
More on Java Scanner: http://javatutorialhq.com/java/util/scanner-class-tutorial/
You know what is the first issue here? Although it's not show stopper, however its bad practice so I will point that out first. There are too many import statement been introduced. Delete all the import statements except import java.util.Scanner. This is not really an issue but it is a good practice not to load classes which we don't need. It will enhance the readability of the source code and it will conserve system resources.
Second issue is the use of this statement command = scanner.next(); There is a know behaviour of java scanner that if you read from console the newline at the end of the input is not getting consumed. This behaviour is evident if you require multiple input from console. I would really suggest to use nextLine() instead. Anyway as far as the source code above is concern, there would not be an issue here because it is only asking for single input.
Third which is the the main issue is the use of the statement command == "yes". This is the main problem here. Remember that when you compare two objects which in this case a String, the use of equals is encouraged.
The last issue is that the scanner object is not close thus it may result to buffer overflow. Just add scanner.close() at the end of the statement and the source code is complete.
Below code is the proper implementation
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String command;
System.out.println("Type yes");
command = scanner.nextLine();
if (command.equals("yes")){
System.out.println("Your input is correct");
}
scanner.close();
}