-3

Here is a sample of the text file I have:

NEXT K29275 01888 00125 0 00 l H& NEXT K2422 001838 00122 0 00 l H& NEXT   K07581 02051 00136 0

So what I am trying to do here is to go through the text file take the number that is after the K and add to it a random number between the next two numbers and print out the results. CMD would be preferred and any help would be awesome.

rafa316
  • 13
  • 2
  • 9
  • title asks for java, but is tagged javascript... java=/=javascript – Brian H. Dec 13 '16 at 14:44
  • please change to java not javacsript – Ishey4 Dec 13 '16 at 14:44
  • Fine. And what is your specific question? Do you want us to do your work? Please check out [how to ask](http://stackoverflow.com/help/how-to-ask) here! – aschipfl Dec 13 '16 at 15:40
  • im sure its a simple bit of code, iv seen people asking for that on here in the past... – rafa316 Dec 13 '16 at 16:27
  • [link](http://stackoverflow.com/questions/3806062/how-to-open-a-txt-file-and-read-numbers-in-java) here for example, if we are not allowed to ask our fellow man for help here then its fine. – rafa316 Dec 13 '16 at 16:36

1 Answers1

0

Not Java/(-script) nor cmd but a PowerShell script :

$InFile = 'x.txt'
$Search = [regex]' K(?<Base>\d+) (?<Max>\d+) (?<Min>\d+)'
(get-content $InFile -raw) -split("&")|
  ForEach{
    if ($_ -match $Search){
    [int]$Min = [math]::min($matches.Min,$matches.Max)
    [int]$Max = [math]::max($matches.Min,$matches.Max)
    $AddRand = [int]$(Get-Random -min $Min -max $Max)
    "Base {0,6} Min {1,6} Max {2,6} AddRand {3,4} Result {4,6}" -f $matches.Base,
      $Min,$Max,$AddRand,[int]([int]$matches.Base + $AddRand)
    }
  }

Output stays the same after edit.

Base  29275 Min  00125 Max  01888 AddRand  132 Result  29407
Base   2422 Min  00122 Max 001838 AddRand  772 Result   3194
Base  07581 Min  00136 Max  02051 AddRand 1786 Result   9367

Except for the proper evaluation of min and max the script is the same

  • First of Thank you :) Second yes please. I have never used PowerShell, I assume the & is in your text file to create the split? how do you run this program? would netbeeans do it or? – rafa316 Dec 14 '16 at 14:39
  • ok I found how to run powershell, still need to know how to tell the script to get each number. the text file is huge, over 400 lines and they do not have the & separating each of the numbers. I have a K at the beginning of the sets which can easily be replaced with an & but how does the script get the next two numbers, they do follow immediately after the first number separated with just a space. Ill upload the document if it helps. – rafa316 Dec 14 '16 at 15:26
  • Just add some typical lines to your above sample. –  Dec 14 '16 at 15:31
  • would this work &29275 &01888 &00125 or does it have to be like this &29275& &01888& &00125& – rafa316 Dec 14 '16 at 15:45
  • In your file some values min and max are exchanged which gives an error, I'll soon edit my answer with an updated script. –  Dec 14 '16 at 15:59
  • See updated answer, one minor error remains when min and max are the same in your file x.txt it may be ignored by inserting `-ea ignore` inside the parentheses following get-random. –  Dec 14 '16 at 16:16