Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
package c15;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileProcessing {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
//loading name of file
File file = new File("input.txt");
//reading data from this file
Scanner reader;
try {
//read file
using scanner
reader = new
Scanner(file);
//read until
file having more to reads
while(reader.hasNext()){
//get the name
String name = reader.next();
//to store numbers
int num;
//to store sum
int sum=0;
//now read file until there is integers in
it
while(reader.hasNextInt()) {
//get next integers
num =reader.nextInt();
//if curr number is -999
break
if(num==-999) {
break;
}else {
sum
+=num;
}
}
//print name and sum
System.out.print("Name : "+name);
System.out.println("\t\tSum : "+sum);
}
} catch (FileNotFoundException e)
{
System.out.println("file not found");
}
sc.close();
}
}
my input.txt is as below
Khaled 65 78 56 89 -999
Kamal 37 55 44 22 18 56 26 -999
Ali 66 33 87 66 87 25 39 78 -999
Taher 12 67 -999
output
Get Answers For Free
Most questions answered within 1 hours.