java - Reading number from a file and checking against a random generation -


objective:

generate random integer (from 1-99) , use numbers.dat file (from last lab) determine if generated random number in file. not count number of times find random number, output it’s in file or not in file.

possible pseudo-code:

  1. generate random # 1-99
  2. declare , initialize boolean variable false
  3. loop until end of file or match found: read number.
  4. compare numbers. if match, set boolean var true
  5. if boolean true number found else not found.

part 2. put code in part 1 inside for-loop part 1 runs 3 times. b. output – similar output below:

the random number 35 in file. random number 10 not in file. random number 9 in file. 

my code:

import java.util.*; import java.io.*; public class randomnumbermatch {     public static void main(string[] args) throws exception {         scanner num = new scanner (new file ("numbers.dat"));         random ran = new random();         while(num.hasnext()) {             int number = num.nextint();             int x = ran.nextint(100);;              if(number == x) {                 system.out.println("the number " + x + " in file");             }             else {                 system.out.println("the number " + x + " not in file");             }         }     } } 

output:

the number 80 not in file number 52 not in file number 50 not in file number 76 not in file number 21 in file number 18 not in file number 40 not in file (cut short summary purpose) 

contents of file:

16 43 56 8 12 35 65 12 26 3 65 4 75 86 9 79 0 78 5 78 4 63 65 3 53 64 7 5 87 6 96 96 70 7 95 78 46 4 37 46 3 53 93 84 76 56 48 93 3 74 76 46 5 47 38 4 39 4 84 57 46 37 48 4 9 84 7 36 3 0 58 6 7 84 7 5 6 4 9 30 4 3 8 4 7 4 4 55 76 4 53 6 47 5 85 74 63 2 3 45 48 73 63 5 21 42 21 55 1  

what doing wrong besides not nesting output in loop, majority of these numbers in file. trying match created random number , if equal output file , proceed indicate if number in file or not. assistance appreciated, stuck on , can't seem figure out!

since seems school assignment won't give code answer, think these questions:

you need determine whether random number exists in entire file. if compare single number file, have enough information make definitive statement "the number not in file"?

is necessary continue loop through entire file if you've found number you're looking for?


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -