Reputation: 73
This CSV reader which also checks the validity of an email address and password through the use of the map tool.
import java.io.*;
import java.util.*;
public class CSVReaders{
public static void run(String[] args) throws Exception {
Map<String, String> emailPasswordMap = new HashMap<String, String> ();
BufferedReader CSVFile =
new BufferedReader(new FileReader("testa453.csv"));
String dataRow = CSVFile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
emailPasswordMap.put (dataArray[0], dataArray[1]);
dataRow = CSVFile.readLine();
}
CSVFile.close();
//Scanner in = new Scanner(System.in);
//String email = in.nextLine();
//String password = in.nextLine();
String password = ("raj45");
String email = ("[email protected]");
if (password.equals (emailPasswordMap.get (email))) {
System.out.println ("The entered email and password are valid");
}
else {
System.out.println ("The entered email and password are invalid");
}
}
}
The problem which I am getting is that upon runing when i change the '//' over to the string password and email and attempt to use the scanner which I have included the program 'runs' but console window does not appear and I have to force stop the program to stop it running. Whilst using it as I have shown here it works perfectly. Previously I had an error with the scanner that related to static and non-static variables. I have looked them up and attempted to use instance variables but to little success. Is the way in which I have declared the scanner wrong or can I not use Mapping in conjuction with the scanner?
EDIT: I am currently using BlueJ on Mac since I am reasonably new to java programming. And yes it does work as I have quoted it, it only stops working when I try to use the scanner.
Upvotes: 3
Views: 306
Reputation: 20442
Is the way in which I have declared the scanner wrong or can I not use Mapping in conjuction with the scanner?
The Scanner
declaration appears to be correct. No, there is no restriction prohibiting the simultaneous use of any two parts of the Java standard library. So it is perfectly okay to use Map
and Scanner
together.
At current, the SO community's best guess is that you are using an IDE (like eclipse) that has a built-in console window/view. Under this assumption, it is assumed that you expect a black terminal/cmd window to open, however in most IDEs this is not the case. In eclipse the "console view" is where you will do your input. In Netbeans this will be the output window.
Upvotes: 1