简体   繁体   中英

Add one number from an array to another (by column)

I have been trying to figure out how to add one number in an array to another number in an array. I parsed the numbers in a String into integers, and separated them by columns. Then, I added each column into an array.

What I would like to solve is how to add all the numbers in a column.

The numbers are from a text file.

// numbers.txt: 

Bob, 100, 98, 95
Alex, 85, 90, 92

I already used bufferedReader and parsed the numbers, from String to int.

The challenge is adding the numbers by column.

For example, if there are 3 numbers in each array, I just want to add the first numbers in each array.

Q1 is [100, 98, 95] Q2 is [85, 90, 92]

Only 100 + 85 together.

Below are the codes that I have so far. Any help on how to proceed will be awesome! Thanks for your time.

int Q1 = Integer.parseInt(columns[1]);
int Q2 = Integer.parseInt(columns[2]);

ArrayList<Integer> Q1list = new ArrayList<>();
Q1list.add(Q1);
Q1list.add(Q2);


double total = 0.0;


for (int i = 0; i < Q1list.size(); i++) {
total += Q1list.get(i);
}

System.out.println(total);

Well, usually when you want to add up the numbers from the array in to a sum you want to iterate over all the indexes in that array. From the loop you've written I cannot see going to all the numbers into the array in any way. Please revise how for loop is used!

Here is a good explanation, hope it helps Java: Array with loop

I think you should have at least 2 columns array.

After don't forget your index (in your loop)

Code suiggested:

public static void main(String[] args) {

int [] q1 = { 100 , 98 , 95 };
int [] q2 = { 85 , 90 , 92 };

List<Integer> sumList = new ArrayList<>();

// First solution (what you ask)

sumList.add( q1[0] + q2[0] );
System.out.println("Add Q1[0] + Q2[0]: " + sumList.get(0));

// Second solution (add all)
for( int i = 0 ; i < q1.length ; i++)
{
    sumList.add(q1[i] + q2[i]);
}

// Check your result
for( int i : sumList )
  System.out.println("Result: " + i);

}

And result gives:

// First solution (what you ask)
Add Q1[0] + Q2[0]: 185

// Second solution (add all)
Result: 185
Result: 185
Result: 188
Result: 187

I find what you want:

// Scanner
StringTokenizer i1 = new StringTokenizer(" [100,98,95]", "[,]");
StringTokenizer i2 = new StringTokenizer(" [85,90,92]", "[,]");

List<Integer> q1List = new ArrayList<>();
List<Integer> q2List = new ArrayList<>();

while( i1.hasMoreTokens() ){
  try {
  Integer intRes = Integer.parseInt(i1.nextToken());
  System.out.println("Test1: " + intRes);
  q1List.add(intRes);
  }
  catch( NumberFormatException e) {}
}

while( i2.hasMoreTokens() ){
  try {
  Integer intRes = Integer.parseInt(i2.nextToken());
  System.out.println("Test2: " + intRes);
  q2List.add(intRes);
  }
  catch( NumberFormatException e) {}
}

// Second solution (add all)
for( int i = 0 ; i < q1List.size() ; i++)
{
    sumList.add(q1List.get(i) + q2List.get(i));
}


// Check your result
for( int i : sumList )
  System.out.println("Result 2 : " + i);

Sorry for the long time but I have to find on web the answer.

Simples reads file line by line and set new string for each new line... After that, for each string you can use Strink tokenizer with delimiter in your case : ",". Take carefull that your first parameter shall be: null (code for that) name (catch this string) other (maybe try catch)

I find this link on stack:

Using Java 8, what is the most preferred and concise way of printing all the lines in a file?

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM