0

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);
Sugarcoder
  • 167
  • 1
  • 2
  • 10

2 Answers2

0

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

Community
  • 1
  • 1
Nick
  • 677
  • 1
  • 11
  • 25
  • I already know how to add all the numbers in each array, but I am having a hard time figuring out how to just add one number from one array to another number in another array. The numbers are from a text file. So basically, adding each column (not row). – Sugarcoder Nov 18 '15 at 20:11
  • Then use the same logic, in your example if you want to add the adjacent indexes again you have to go through the numbers of the smaller array if they are not equal. If you want to add just the 1st numbers from both, you don't need loop. So for your example: `[100, 98, 95]` and `[85, 90, 92]` `for(I=0; I<3; I++)` `total[I]+=q1[I]+q2[I];` This should produce result `total[0] = 185`, `total[1] = 188` , `total[3] = 187` – Nick Nov 18 '15 at 20:17
  • Okay, I will give that a try. Thanks! – Sugarcoder Nov 18 '15 at 20:20
  • No, not yet. Since the numbers are from a file and I had to parse it, it is a lot more complicated than I thought. I wish it was straight-forward. – Sugarcoder Nov 18 '15 at 21:13
  • maybe [this](http://stackoverflow.com/questions/18524605/parsing-numbers-from-a-text-file) could help – Nick Nov 18 '15 at 21:22
  • Since I already parsed the numbers from the file, the challenge is summing each column. Sadly, that post is not relevant for this issue. :( – Sugarcoder Nov 18 '15 at 21:23
0

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

Community
  • 1
  • 1
Gromph
  • 123
  • 12
  • Unfortunately, I can't code the arrays like that. Otherwise, I solved it a long time ago. The numbers are from a text file, so its arbitrary. That is why I had to parse it from a string into an int first. However, I will try parts of what you coded and see if it works! – Sugarcoder Nov 18 '15 at 20:34
  • Since the numbers are from a text file, I can't just create my own strings. Supposedly, I'm not even supposed to know what the numbers are, because it is arbitrary. The challenge is finding the sum of each column after parsing them. I used bufferedReader instead of Scanner. – Sugarcoder Nov 18 '15 at 21:20