Reputation: 834
I have been trying to solve problem from a programming site and the data is as below. content in my text file as below.
2
100
3
5 75 25
200
7
150 24 79 50 88 345 3
The first number gives the number of test cases, here it is 2, and the number after it is the sum and the next line is the size of array followed by the array elements (numbers). I need to sum the array numbers and match with the sum that is given and print the position of the numbers that match the sum.
In the above case the results should be:
2, 3
1, 4
4, 5
29, 46
11, 56
4, 5
40, 46
16, 35
55, 74
7, 9
I managed to get this manually but unable to figure out how to do this from a text file.
The code I used to get the values:
public class StoreCredit {
public static void main(String[] args) {
int Avail=200;
int[] pri={150, 24, 79, 50, 88, 345, 3};
for(int i=0;i<pri.length-1;i++){
for(int j=i+1; j<=pri.length-1;j++){
int sum=pri[i]+pri[j];
//System.out.println(pri[i]+", "+pri[j]+" is "+sum);
if(sum==Avail){
System.out.println((i+1)+", "+(j+1));
}
}
}
}
}
The code I used to get the values from text file:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class FromFile {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(new File("D:/A1.txt"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next());
}
System.out.print(list);
s.close();
}
}
When I ran the above code, I got the below output.
[2, 100, 3, 5, 75, 25, 200, 7, 150, 24, 79, 50, 88, 345, 3]
How can the data be read from file (the sum, the number of inputs with arraysize etc.)?
Upvotes: 0
Views: 139
Reputation: 55599
Read the test case count.
int testCaseCount = scanner.nextInt();
Loop over the number of test cases.
for (int i = 0; i < testCaseCount; i++)
Read the sum.
int sum = scanner.nextInt();
Read the array size and construct the array.
int arraySize = scanner.nextInt();
int[] array = new int[arraySize];
Loop through the array, populating it.
for (int j = 0; j < array.length; j++)
array[j] = scanner.nextInt();
Do what you want to do with this data.
Upvotes: 1
Reputation: 1080
Scanner also has a nextInt which is good for this purpose. This will always jump to the next integer and parse it. I belive Dukelings code will generate a compile error as next() returns a String which you have to parse. This should work:
import java.util.*;
import java.io.*;
public class read{
public static void main(String[] args) {
Scanner s = null;
try{
s = new Scanner(new File("test"));
}catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
int testCases = s.nextInt();
for(int testCase=0; testCase<testCases; testCase++){
int sum = s.nextInt();
int numberOfCanidates = s.nextInt();
List<Integer> canidates = new ArrayList<Integer>();
for(int number = 0; number<numberOfCanidates ;number++){
canidates.add(s.nextInt());
}
solve(sum, canidates);
}
}
public static void solve(int sum, List<Integer> canidates) {
System.out.print("Trying to solve problem for sum=" + sum + " and candiates { ");
for(Integer canidate : canidates){
System.out.print(canidate);
System.out.print(" ");
}
System.out.println("}");
}
}
Upvotes: 1
Reputation: 3356
You could do it with the Scanner.nextLine() method to get full lines:
["2", "100", "3", "5 75 25", "200", "7", "150 24 79 50 88 345 3"]
After that you have to split the numbers to get an array of them.
Also consider using the Scanner.hasNextLine() method to proof, if there is another line in the stream.
Finally process your logic on these values.
Upvotes: 1