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
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:
- How can I get a certain font onto my MSN?Best solution by Yahoo! Answers
- How Do I Get A Blyk Invite Code?Best solution by Yahoo! Answers
- How do you get a car stereo code?Best solution by Yahoo! Answers
- Do you have to have certain grades to get a normal workers permit?Best solution by Yahoo! Answers
- Can I get a LookBook code?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.