aurelio
aurelio

Reputation: 57

Reading file and setting to variable

I'm new to programming, so I was wondering if someone could help me. What I'm trying to is make the user input a number, and if the number is in the file, it'll spit something back out, and if it's not in the file, it'll keep asking for a number. I don't know exactly how to do this, but if someone could point me in the right direction, that would be great. Thanks in advance!

Upvotes: 1

Views: 42

Answers (1)

amit
amit

Reputation: 178451

  • First read the file and store it (as a Collection, or as a big String, the exact type depends on how you actually trying to achieve this).
    Reading a file can be done in numerous ways, some of them are using a Scanner, BufferedReader or File.readAllLines()
  • Now, create a while loop - that repeatidly asks the user for a number, until the condition that the number is in your data is met.
  • Next, you need to process and yield the desired output.

Bonus:
Try to do each of the bullets in a different method and only combine them in your main() - it will help you to learn about basic methods design as well.

Upvotes: 4

Related Questions