Results 1 to 8 of 8
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Parse int not working as expected

    I have a 2D array and I'm trying to sum the numbers on each row but instead of adding together their concatenating. Why is this I thought if I parsed them it should work?

    Thanks

    Code:
    Java Code:
    public class HortonCodyHw11 {
    
    public static void main(String [] args){
    		
    		String[][] matrix = {
    				{" ","SU","M","T","W","R","F","SA"},
    				{"Employee0","2","4","3","4","5","8","8"},
    				{"Employee1","7","3","4","3","3","4","4"},
    				{"Employee2","3","3","4","3","3","2","2"},
    				{"Employee3","9","3","4","7","3","4","1"},
    				{"Employee4","3","5","4","3","6","3","8"},
    				{"Employee5","3","4","4","6","3","4","4"},
    				{"Employee6","3","7","4","8","3","8","4"},
    				{"Employee7","6","3","5","9","2","7","9"}
    		};
    
    		for(int i=1;i<matrix.length;i++){
    			for(int j=1;j<matrix[i].length;j++){
    				String temp = matrix[i][j];
    				int sum =+ Integer.parseInt(temp);
    				System.out.print(sum);
    			}
    			System.out.println();
    		}
    		
    	}
    	
    }

    Result:
    Java Code:
    2434588
    7343344
    3343322
    9347341
    3543638
    3446344
    3748384
    6359279

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Parse int not working as expected

    int sum = 0; <-- creating a new variable and setting its value to 0.
    sum += Integer.parseInt(temp); <-- Give an existing variable a value. (In this case adding to it)
    System.out.println(sum); <-- Printing the variable.

    Now do you know what you're doing wrong?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,442
    Rep Power
    25

    Default Re: Parse int not working as expected

    When could the code have the sum of all the columns on a row?
    Hint: after it has looked at all the columns of the row.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Parse int not working as expected

    Quote Originally Posted by codyjava View Post
    Java Code:
    				String temp = matrix[i][j];
    				int sum =+ Integer.parseInt(temp);
    				System.out.print(sum);
    That second line only creates a local variable 'sum' and initializes it with the int value from your matrix and then it is printed; The construct int foo= +bar is the same as int foo= bar.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Parse int not working as expected

    Yeah I'm kind of confused so is it looking at the whole row all at once. So it is working just not the way I want. In that case how would I add up each number in the row?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,442
    Rep Power
    25

    Default Re: Parse int not working as expected

    how would I add up each number in the row?
    Something like this:
    Set sum to 0
    set next to first item
    begin loop
    add next item to sum
    move to next item
    end loop
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,617
    Rep Power
    5

    Default Re: Parse int not working as expected

    Java Code:
    int sum = 10;
    sum =+ 20;  // now sum = 20;
    Java Code:
    int sum = 10;
    sum += 20;  // now sum = 30;
    And you are printing the numbers as you "sum them up." I expect you want to print them after they are summed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Parse int not working as expected

    Ha okay yeah I figured I did something dumb thanks I wasn't paying attention to to the small stuff.

    Here's what I got now:
    Java Code:
    		for(int i=1;i<matrix.length;i++){
    			int sum = 0;
    			for(int j=1;j<matrix[i].length;j++){
    				String temp = matrix[i][j];
    				sum += Integer.parseInt(temp);
    			}
    			System.out.print(sum);
    			System.out.println();
    		}
    Thanks

Similar Threads

  1. replaceAll(string a, string b) not working as expected
    By Bronsonboy in forum New To Java
    Replies: 3
    Last Post: 07-16-2012, 12:18 AM
  2. Sax Parse xml
    By siddheshwar in forum XML
    Replies: 1
    Last Post: 06-25-2011, 08:12 PM
  3. Gridbaglayout not working as expected
    By berkeleybross in forum AWT / Swing
    Replies: 7
    Last Post: 07-06-2010, 12:11 PM
  4. How to parse this value
    By Ajitha in forum New To Java
    Replies: 2
    Last Post: 06-03-2010, 07:26 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •