Results 1 to 14 of 14
Thread: Initialize a Variable
- 11-16-2010, 04:30 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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
This is the error message: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 ; } }
RepetitionTest.java:12: ';' expected
num = 5,3,7,9,4;
^
1 error
- 11-16-2010, 04:39 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
"num" is an int, so it can only hold one int.
Are you sure you don't want an int[] (an array)?
- 11-16-2010, 04:59 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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 ; } }
- 11-16-2010, 05:09 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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:
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:array[2]= 1;
Now all five houses have a value stored in them: 5, 3, ... 4 respectively.Java Code:int[] myA = { 5, 3, 7, 9, 4 };
If you want to calculate the sum of all those values you have to do something like this:
Study a tutorial on arrays before you start banging your keyboard; it really pays back.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
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 05:10 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 11-16-2010, 05:11 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Well I'm very new myself, so maybe I shouldn't answer, but to declare that array, you can do:
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. :)Java Code:int[] num = new int[5]; num = {1,2,3,4,5}; Or simply: int[] num = {1,2,3,4,5};
- 11-16-2010, 05:42 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 05:47 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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
- 11-16-2010, 06:00 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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?
- 11-16-2010, 06:05 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 11-16-2010, 06:10 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
Thanks Jos. That hadn't been made clear to me before now. I apologize to lala if I caused any confusion.
- 11-16-2010, 06:32 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
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);
- 11-16-2010, 06:36 PM #13
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
You want to make the line that prints the sum part of the for loop, like so:
Not sure why you're starting your output with "i:" though.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); }
- 11-16-2010, 06:51 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
how to initialize base class variable without creating object for that?
By kaka in forum New To JavaReplies: 3Last Post: 09-29-2010, 09:26 AM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 08:11 AM -
Int does not initialize, will this work?
By starchildren3317 in forum New To JavaReplies: 2Last Post: 07-09-2008, 10:42 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:22 AM -
I do not know how to initialize the two variables
By Daniel in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks