How can I properly read data from bar code scanner?

Fix java code to get a certain input?

  • This is my code so far, and it works just fine: public static void main(String[] args) { Scanner scanner = new Scanner (System.in); List<Integer> list = new ArrayList<Integer>();{ while (scanner.hasNext()) { // do we have one more line? list.clear(); // we need empty list for (int i=0;i<3;i++) { // read 3 numbers list.add(scanner.nextInt()); } Collections.sort(list); int max = list.get(2); int middle = list.get(1); System.out.println(list); System.out.println(max+middle); } } } } What should I do to fix it so the input would have to be entered like this? 12 //number of sets 33 -34 18 //first of 12 data sets -42 44 -34 //second of 12 -42 7 44 //third etc... 4 33 44 14 7 -34 44 33 7 -34 18 18 7 -34 -34 4 14 18 7 18 -34 14 14 4 7 33 33

  • Answer:

    The first int you read would be an exit condition for a for loop: int numberOfDataSets=scanner.nextInt(); for(int i=0;i<numberOfDataSets;i++) { ... Your code } That way, your app will only run for the number of sets indicated in the input data.

BamBam at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The first int you read would be an exit condition for a for loop: int numberOfDataSets=scanner.nextInt(); for(int i=0;i<numberOfDataSets;i++) { ... Your code } That way, your app will only run for the number of sets indicated in the input data.

godfathe...

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.