954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

BufferedReader (Skipping Lines Issues)

It's only reading every other line. It's supposed to print out:
28
80.0
22
1
2
2
3
1
3

But it's only reading:
80.0
1
2
1

//Load button - #=daynum, $=wallet, @=inventory, &=employees, !=customers
		            	if (btn2.getText().equals("Load")){
		            		int id=0;
		            		File file = new File("savefile1.txt");
		            		BufferedReader reader = null;
		            		
		            		try {
		            			reader = new BufferedReader(new FileReader(file));
		            			String text = null;
		            		
		            			while ((text = reader.readLine()) != null) {
		            				text = reader.readLine();
		            				if (text.contains("#")){
		            					text=text.replace("#", "");
		            					dayNum=Integer.parseInt(text);
		            				}
		            				if (text.contains("$")){
		            					text=text.replace("$", "");
		            					wallet=Double.parseDouble(text);
		            				}
		            				if (text.contains("!")){
		            					text=text.replace("!", "");
		            					customer.setCustTotalComplete(Integer.parseInt(text));
		            				}
		            				if (text.contains("@")){
		            					text=text.replace("@", "");
		            					id=Integer.parseInt(text);
		            					BuyInvItem(id);
		            					inventory.setNewInv(InvId[id],
	        									InvPrice[id],
	        									InvName[id],
	        									InvSellPrice[id],
	        									InvAmt[id],
	        									InvBuyPrice[id], id);
		            				}
		            				if (text.contains("&")){
		            					text=text.replace("&", "");
		            					id=Integer.parseInt(text);
		            					employee.setNewEmp(EmpId[id],
	        									EmpWage[id],
	        									EmpName[id],
	        									EmpSpeed[id], 
	        									id);
		            				}
		            			}//end while
		            			MainText();
		            			UpdateEmpDisplay();
		            			UpdateInvDisplay();
		            			txtSide.setText(MainText);
		            		}//end try
		            		catch (FileNotFoundException f) {
		            			f.printStackTrace();
		            			JOptionPane.showMessageDialog(null, "Save file not found");
		            		}
		            		catch (IOException f) {
		            			f.printStackTrace();
		            			JOptionPane.showMessageDialog(null, "There was an error loading the file");
		            		}
		            		catch (NullPointerException f){
		            			JOptionPane.showMessageDialog(null, "File loaded successfully");
		            		}
		            		finally {
		            			try {
		            				if (reader != null) {
		            					reader.close();
		            				}
		            			} catch (IOException f) {
		            				f.printStackTrace();
		            			}
		            		}
		            	}


HELP ME PLZ

Slyvr
Junior Poster in Training
74 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 
while ((text = reader.readLine()) != null) {
		            				text = reader.readLine();
		            				if (text.contains("#")){


The reason it only displays every other line is that you read each line twice. Once, when you check to see if the while loop is still good, again when you store it as the variable 'text'. Since you're already storing the next line (reader.readLine()) in the while loop, just drop the second readline:

while ((text = reader.readLine()) != null) {
		            				if (text.contains("#")){
ztini
Posting Whiz in Training
299 posts since Jan 2011
Reputation Points: 54
Solved Threads: 52
 

Ahhh, thank you. A second pair of eyes is always helpful for catching these silly little things ;)

Slyvr
Junior Poster in Training
74 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: