How to read numbers from file and take actions on them in java?

103
January 01, 2020, at 11:50 AM

this is my file information:

mahdi aspanani 664 22.0 ali moghadasi 675 26.0

I want to read this information from file and count 22.0 + 26.0

how can I do this in java?

Answer 1
  1. First, open your file with a Scanner instance.
  2. Read each line.
  3. Split on spaces
  4. Convert the fourth token to a double.
  5. Add it to sum.
  6. Continue until no more lines.
        Scanner input;
        String myFileName = "......";
        try {
            input = new Scanner(new File(myFileName)); 
            double sum = 0;
            while (input.hasNextLine()) {
                String line = input.nextLine();
                String[] vals = line.split("\\s+");
                sum += Double.parseDouble(vals[3]);
            }
            System.out.println("sum = " + sum);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Answer 2

Assuming you mean the content of the file is structured like this:

<string> <string> <int> <summand 1> <string> <string> <int> <summand 2>

and none of the strings contains spaces and both summands are doubles, you can do the following:

try(BufferedReader br=new BufferedReader(new FileReader(filename))){
    String[] split=br.readLine().split(" ");
    double result=Double.parseDouble(split[3])+Double.parseDouble(split[7]);
    //do with it whatever you want
}catch(IOException e){
     e.printStackTrace();//or do something else or don't catch it and add it to the signature
}

Don't forget to put the name ofthe file instead of filename.

READ ALSO
Failing when inserting generated column of date or time type from a datetime/timstamp source

Failing when inserting generated column of date or time type from a datetime/timstamp source

A generated column is a column containing the result of an expression or function based on another fieldI want a simple date and time field derived from a datetime field, but to insert records I must adopt a workaround

99
What is the best way to load the bootstrap card with contents dynamically?

What is the best way to load the bootstrap card with contents dynamically?

I am a beginner in web developmentI need to create a page with bootstrap card dynamically by fetching values from database

97
Why a named function declaration isn&#39;t hoisted if you put it inside the return statement?

Why a named function declaration isn't hoisted if you put it inside the return statement?

is putting the function declaration beside the return statement causing the function not to hoist? or putting it inside the return statement turns it into a function expression that's why it isn't hoisted?

68
how to set the parameter NODE_EXTRA_CA_CERTS in the project instead of the scripts

how to set the parameter NODE_EXTRA_CA_CERTS in the project instead of the scripts

I have an api testing project build with nodejs and Jest which send http requests to multiple domain using the 'request-promise' npm package

143