Hi

I've already read data in another file which is an array, which is now in a string format, what I want to do is change the array into an integer so that I can add the elements of that array.

Code given below

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/*
* @author kagisoboikanyo
 */
public class ReadInput {

    public static void main(String[] args) throws FileNotFoundException, IOException {

   try{


       FileInputStream fstream = new FileInputStream("superIncrease.txt");


      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      String strLine;



               in.close();


   }
   catch (Exception e){
       System.err.println("Error:" +e.getMessage());
   }
   }

    //System.out.println("Test: " + knapsack.toString());
 } 

As I said earlier I am able to view the elements which is an array, but its in a string format now what I need to do is change it to an integer then add the elemnts together to find a sum.

Thanks in advance.

Dani AI

Generated

Two practical fixes before parsing: the posted code creates a BufferedReader but calls in.close() before reading any lines — the stream must be read first (or use try-with-resources) so data is actually available. Also determine the exact string format stored in the file (comma-separated, space-separated, a JSON-like "[1,2,3]" line, or one number per line) because the parsing method should match that format.

As pointed toward parsing, a robust, format-agnostic approach is to extract integer tokens with a regex and convert them with parseLong (safer than parseInt for sums). The routine below handles negatives and works for inputs like [1,2,3], 1 2 3, or 1, 2, 3:

public static long sumNumbersFromString(String s) {
    long sum = 0;
    java.util.regex.Matcher m = java.util.regex.Pattern.compile("-?\\d+").matcher(s);
    while (m.find()) {
        sum += Long.parseLong(m.group());
    }
    return sum;
}

Use this on each line read from the file (or on the whole file text). Important cautions: catch NumberFormatException if inputs might be malformed; switch to Double.parseDouble for decimals; choose long (or BigInteger) if values/aggregate may overflow int. Prefer try-with-resources for file I/O so streams are closed automatically. If the file is strictly comma-separated and guaranteed well-formed, splitting on \\s*,\\s* and mapping with Integer.parseInt (or Long.parseLong) is simpler and slightly faster.

The Integer class has a parseInt(s) method that parses a string and converts it to an int value (or thows a NumberFormatException if the string does not represent a valid int value).
You'll need an array of ints, same size as the array of Strings, and a loop that onverts the strings one at at time and stores the results in the int array. Then you can do whatever arithmetic stuff you want on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.