• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

How to Read from Console and File in a program!

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, but we ask that you ShowSomeEffort and DoYourOwnHomework. What have you tried so far?
 
Sheriff
Posts: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which error did you get? TellTheDetails.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the error/ Exception
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22895
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic