How to find end of line and perform addition of two integer arrays in java?
How to find how many tokens are there in a line in the below program and then I need to add two integer arrays using Java, since I am more familiar with php, this is a bit challenging for me . Also I am getting the input from a text file. Hence, here is how I have my program so far.
The input file would have multiple lines like this 3736 17234 29823 84478 123745 2371 34237 823712
import java.io.*;
import java.util.*;
public class Sum {
//must use a constant for the length of the array
static int[] total= new int[25];
static int[] val = new int[25];
static int line= 0;
static int word =0;
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("Input.txt"));
// System.out.println("0");
processFile(input);
}
public static void processFile(Scanner input) {
//in this method you need to read your input file
//. read one line at a time and call the method processLine to
while (input.hasNextLine())
{
line++;
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
//System.out.println("1");
processLine(input);
//System.out.println(line);
}
}
public static void processLine(Scanner data) {
//in this method you read tokens from line that has been passed to
// this methd as the parameter. method transfer needs to be called to
// transfer each token into an array of length DIGITS. Note that in a
// line you might only have one token
while(data.hasNext())
{
String x = data.next();
//System.out.println("2");
transfer(x,val);
}
}
public static void transfer(String data, int[] digits) {
//This method transfer the string into array of integers value.
int len = data.length();
int n=24;
for(int i=0;i<=n;i++)
digits[i]=0;
//System.out.println("3");
while(len>0)
{
// System.out.println(data.charAt(len-1));
char z=data.charAt(len-1);
int d = Character.digit (z, 10);
digits[n]=d ;
len=len-1;
n=n-1;
}
for(int i=0;i<=n;i++)
digits[i]=0;
for(int i=0;i<25;i++)
{
//System.out.println(digits[i]);
}
System.out.println("\n");
add(digits);
}
public static void add(int[] digits) {
word++;
if (word开发者_开发百科>1)
{
for(int i=0; i<= 4; i++)
{
total= total[i]+digits[i];
}
}
if(word==0)
total=digits;
}
public static void print(int[] digits) {
//For printing
}
}
Use a for loop to go through the 2 arrays and add each element.
Quick example code below:
private int[] sumTwoArrays(int [] a, int [] b){
if(a.length!=b.length){
throw new IllegalArgumentException("Arrays are not of same length!");
}
int[] sum = new int[a.length];
for(int i=0;i<a.length;i++){
sum[i] = a[i]+b[i];
}
return sum;
}
UPDATE: After the comments below I added another method on how to add just the elements in one array.
private void readFile(){
long sum=0;
//do your FILE I/O here
//for each line you read into an array called input[] you call this method
sum += sumArray(input);
System.out.println(sum);
}
private long sumArray(long [] a){
long sum=0;
for(int i=0;i<a.length;i++){
sum += a[i];
}
return sum;
}
You complicated this far more than is necesary. To begin with, you can remove the multiple function references, the scanner class can actually parse your file into integers, and can read multiple lines - so you don't need a new scanner for each line.
I think you can simply have one Scanner per file, and then constantly use the Scanner.nextInt() function to find the next integer value in that file.
Now depending on your definition of add, there are two things you could do.
- If you're attempting to add corresponding elements of each array, to come with with a third array where x[i] = y[i] + z[i], then you simply need a for loop with the previous statement in it. Remember of course to substitute the real names of the variables.
- If you're attempting to add up all the integers in both the arrays to get one integer (or long), you could use two for loops, one after another and constantly add the current element of the current array to a variable.
精彩评论