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.

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.