• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Read from Console and File in a program!

 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys !

Could you please let me know how I can make the Scanner object read String like password from file and check it with user input from console?!
I tried to use one scanner object which I got error.

thanks
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, but we ask that you ShowSomeEffort and DoYourOwnHomework. What have you tried so far?
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which error did you get? TellTheDetails.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah , this is the code which I was talking about it

as you can see I have two Scanner object (filePass and inputPass) but system try o read both of them by FileInputStream class which id not desierd
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the error/ Exception
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Behrouz Hosseini wrote:



Are you sure you want to reassign the standard input stream? I'll answer this for you: you really don't want to do this.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
Behrouz Hosseini wrote:



Are you sure you want to reassign the standard input stream? I'll answer this for you: you really don't want to do this.

what do you mean?
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.in is now pointing to a text file that apparently only has one line. To read from both the file and the console use "new Scanner(new File("D:/Java/test.txt"))" as well as "new Scanner(System.in)". This will give you two Scanner objects. You currently don't; filePass and inputPass are not Scanner objects but Strings, and keyboard is your only Scanner object.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:System.in is now pointing to a text file that apparently only has one line. To read from both the file and the console use "new Scanner(new File("D:/Java/test.txt"))" as well as "new Scanner(System.in)". This will give you two Scanner objects. You currently don't; filePass and inputPass are not Scanner objects but Strings, and keyboard is your only Scanner object.


Thanks Rob,
I got it, so I had to create two objects of Scanner,
perfect it's working now
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!