I need to read a .txt file into a list and then pass one element of the list to a method and return to the main loop, run through the loop again and pass in the next element...

Thanks

File file = new File("pathToFile.txt"); 
List<String> contents = FileUtils.readLines(file);

do some stuff....


if(!blah.blah().equals(something)){
				
for (String line : contents) 
//pass one element of the list into the method and then go
//back to the top of the loop to navigate to another
//page and pass the next element...
call.method(line);
}

Recommended Answers

All 9 Replies

Does your code do what you want it to do?
If not please explain the problem you are having.

The issue I am having is that I pass the entire list into the method, I only need one element. The list contents are a,c,b,d...

All I want to do is pass in one element (a), then go back to the top of the loop and then pass in c, and so forth...

What happens when you clean up the code you posted and execute it?
It works for me. The variable line gets one element at a time from contents.

The list has 70 elements, to answer questions. All 70 answers are selected in the first question. I just want to pass one answer into one question. Then move onto the next question with the next answer.

Can you post the code.
I copied the code you posted, changed it so it compiles and get one item at a time.

ArrayList<String> contents = new ArrayList<String>();
      contents.add("asdf");
      contents.add("dasdfffd");
      contents.add("werdfsfsf");
      
      for (String line : contents)  {
        System.out.println("line=" + line);  // Here you get one element at a time
      }
//Read the file into a list of potential answers
		//readFile();
		File file = new File("path.txt"); 
		//Use a list for the contents of the file
		List<String> contents = FileUtils.readLines(file);
		//String question = null;
		//int position = 1;
		while (testPage != null ){
			long startTime = System.currentTimeMillis();
		for (int i = 1; testPage != null; i++) {
			log.debug("i -> "+i);
			testPage = testPage.navigateToQuestion(i);
			if (testPage == null)
				break;
			//Figure out what page I am on and start to answer questions when the (question) =
			//itemType.Q_MC
			if(!testPage.isAnswerableQuestion().equals(itemType.Q_NA))
				//System.out.println("What page am I on - " + testPage.isAnswerableQuestion());
				for (String line : contents){ 
					//pass one element of the array to the method and then go
					//back to the top of the loop to navigate to another
					//answerable question...
					testPage.answerQuestion(line);
				}				
		  }	
	}		
the method that I call

public void answerMC(String question) throws STWTestItemException {
		//
		// driver.switchTo().defaultContent();
		// driver.switchTo().frame("mainFrame");
		//
		List<WebElement> radioButtons = driver.findElements(By
				.className("radio_table"));

		MC_ANSWER_MAP answer = MC_ANSWER_MAP.valueOf(question);
		
		if (radioButtons.size() == 0){
			STWTestItemException e =  new STWTestItemException("No answers available!");
			e.setCapturedImage(snapScreenShot());
			throw e;
		}
		radioButtons.get(answer.ordinal()).click();

	}

Can you make the code into something that will compile and execute for testing?

You can run it out of Eclipse as JUnit...

Ok, I don't use Eclipse or JUnit. I need a complete program that executes. Small without non relevant code.

I'll have to let someone else pick this thread up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.