Results 1 to 14 of 14
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Initialize a Variable

    Hi.
    I am trying to assign more than one value to a variable. the variable is "num" and the values are 5 3 7 9 4. I have read the chapter on Declaring & Initializing variables and also Googled it. What am I doing wrong? I just want to run a simple Repetition test. Thank you

    Java Code:
    	import java.util.*;
    	public class RepetitionTest 
    {
    	 static Scanner console = new Scanner(System.in);
    	 public static void main(String[] args)
      {
      	int sum, num;
    	
    	num = 5,3,7,9,4;
    	 sum = 0;
    	 num = console.nextInt() ;
    	 sum = sum + num ;
    	}
    }
    This is the error message:
    RepetitionTest.java:12: ';' expected
    num = 5,3,7,9,4;
    ^
    1 error

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    "num" is an int, so it can only hold one int.

    Are you sure you don't want an int[] (an array)?

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Initialize a Variable

    Thanks for responding Tolls. I've never coded an array. Is this how it should be done?

    Java Code:
    myA = new int[5]; 
    myA = {5 3 7 9 4};
    sum = 0;
    	 myA = console.nextInt() ;
    	 sum = sum + myA ;
    	}
    }

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

    Default

    Quote Originally Posted by lala View Post
    Thanks for responding Tolls. I've never coded an array. Is this how it should be done?

    Java Code:
    myA = new int[5]; 
    myA = {5 3 7 9 4};
    sum = 0;
    	 myA = console.nextInt() ;
    	 sum = sum + myA ;
    	}
    }
    Don'y guess; arrays are like a block of houses; an array with five elements is like a block of five houses where the house numbers are 0, 1, 2, 3 and 4. You can store of fetch one value in each house like this:

    Java Code:
    array[2]= 1;
    This sets the value 1 in the house with house number 2. You don't have to use constants for the house numbers; any integer value will do. You can declare and initialize an array like this:

    Java Code:
    int[] myA = { 5, 3, 7, 9, 4 };
    Now all five houses have a value stored in them: 5, 3, ... 4 respectively.

    If you want to calculate the sum of all those values you have to do something like this:

    Java Code:
    int sum= 0; // we haven't added any value yet
    for (int i= 0; i < maA.length; i++) // let i be all house numbers in a loop
       sum+= myA[i]; // add the value of house at number i
    Study a tutorial on arrays before you start banging your keyboard; it really pays back.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You can just do:
    int[] myA = {5,3,7,9,4};

    However, if all you want to do is add the different elements from the array then why read form the console?

    ETA: Oh, beaten to it.

  6. #6
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Well I'm very new myself, so maybe I shouldn't answer, but to declare that array, you can do:
    Java Code:
    int[] num = new int[5];
    num = {1,2,3,4,5};
    
    Or simply:
    int[] num = {1,2,3,4,5};
    Edit: wow, you guys are fast. Two replies in the time it took me to write that. Guess I'll have to get used to the volume of posts on here. :)

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

    Default

    Quote Originally Posted by lonegreyride View Post
    Well I'm very new myself, so maybe I shouldn't answer, but to declare that array, you can do:
    Java Code:
    int[] num = new int[5];
    num = {1,2,3,4,5};
    
    Or simply:
    int[] num = {1,2,3,4,5};
    Edit: wow, you guys are fast. Two replies in the time it took me to write that. Guess I'll have to get used to the volume of posts on here. :)
    Your first example doesn't work; the sequence {1, 2, 3, 4, 5} could have been for a byte or short or long array as well and the compiler can't figure out your intentions, i.e. they are ambiguous.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Initialize A Variable

    Whew, this is a lot to digest. Tolls, JosAH, & lonegreyride, I am going to retreat and meditate on all of this information. Be back in a bit.
    Thank to all of you,
    lala

  9. #9
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your first example doesn't work; the sequence {1, 2, 3, 4, 5} could have been for a byte or short or long array as well and the compiler can't figure out your intentions, i.e. they are ambiguous.

    kind regards,

    Jos
    Ah, thanks. See, I knew I should have held off and let the experts answer. So, to clarify, assigning values to the array with this method ( = {1,2,3,4,5}) should only be done in the same statement that declares the array in the first place? Would this mean that if an array is declared like this (int [] num = new int[5];), then each element has to be assigned a value individually?

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

    Default

    Quote Originally Posted by lonegreyride View Post
    Ah, thanks. See, I knew I should have held off and let the experts answer. So, to clarify, assigning values to the array with this method ( = {1,2,3,4,5}) should only be done in the same statement that declares the array in the first place? Would this mean that if an array is declared like this (int [] num = new int[5];), then each element has to be assigned a value individually?
    Yup, that's all correct. The only place you can initialize all the array's values is in its declaration and initialization expression.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    lonegreyride is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks Jos. That hadn't been made clear to me before now. I apologize to lala if I caused any confusion.

  12. #12
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Initialize a Variable

    Hello again.
    I'll start over with my question. I want to see what the values of variables num and sum are for each value: 5 3 7 9 4 (for each iteration of the program). The System.out.print statement only gives me the total of "28" . I apologize for any confusion.

    Java Code:
    int sum;
    	int[] num = {5,3,7,9,4};
    	sum = 0;
    	for (int i= 0; i < num.length; i++) 
     	sum+= num[i]; 
    	System.out.print("i:"  + sum);

  13. #13
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    You want to make the line that prints the sum part of the for loop, like so:

    Java Code:
    int sum;
    int[] num = {5,3,7,9,4};
    sum = 0;
    for (int i= 0; i < num.length; i++) {
        sum+= num[i]; 
        System.out.println("i:"  + sum);
    }
    Not sure why you're starting your output with "i:" though.

  14. #14
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Initialize A Variable

    :o
    Ahhhhhhhhhhhhhhhhhhhh... Gee Iron Lion, that just makes so much sense. I wonder why I didn't think of that? <sigh>

    Again my deepest gratitude to everyone.
    lala

Similar Threads

  1. Replies: 3
    Last Post: 09-29-2010, 10:26 AM
  2. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 09:11 AM
  3. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 11:42 PM
  4. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:22 PM
  5. I do not know how to initialize the two variables
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:42 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
  •