Hi guys! Ok so this is my first year in college and I am having a lot of trouble with my computer course which mostly consist of writing java programs. I am falling way behind in class and i have a mid term due next week. It is REALLY causing a lot of stress on me. I would usually go for extra help but I am super busy with mid term papers and studying. THANK YOU!

Please create 1 file called ArrayLab. In that file you will write methods that represent each one of the objectives below. When the methods are complete you will create the main method, create a new ArrayLab object and invoke each of the methods you created.

a.) In a method declare an array that stores the values from 1 to 10. Initialize and print the values using a single for loop.
b.) In a method declare an array that stores the names of your family. You should declare and initialize the values using the short hand method. Use a for loop to print out the values
c.) In a method declare an array that stores 4 lucky numbers. You should add the values long hand. You will need to use a for loop to print the values
d.) In a method declare a multidimensional array that will store the times tables up through 10. You will need to use 2 for loops to store the values and print them out

Recommended Answers

All 20 Replies

no offence, but if you can't write a single for loop, create and initialize an array and print their values by the time your mid term is approaching, maybe you should consider either choosing another major, or stepping up the studying a bit.

in cases like this, it is way better to double a course, than to cheat and get your diploma without knowing how to do the work, since once you're working, your boss won't pay you very long if he figures out you're not doing the work yourself.

commented: Exactly :) +7

Yeah, college can get stressful like that. If you don't keep up, you'll fail.

Good luck with your program. If you have some specific questions or errors in your code, post them and perhaps someone can help.

No one is going to write the program for you though.

thats what i got so far. i cannot figure the rest out.

a.

int[] numArray = new int[10];
for(i = 0; 1 < numArray.lengh;i++)
{

numArray[i] = i + 1;

System.out.println("" + numArray[i]);
}

b.

String[] names = {"Rich", "Andrew", "Marissa"};


c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;

d.

thats what i got so far. i cannot figure the rest out.

Okay, let's take a look at it.

A:

a. int[] numArray = new int[10];
for(i = 0; 1 < numArray.lengh;i++)
{

numArray = i + 1;

System.out.println("" + numArray);
}

The items which are marked in red are either unnecessary or wrong.
Problem description:

  • You want to assign a value to variable 'i', but you never declared that variable before, so your code won't compile.
  • The '1' has to be an 'i'.
  • The double quotes are not necessary, that is: System.out.println( numArray[i] ); would also be correct.

B:

b. String[] names = {"Rich", "Andrew", "Marissa"};

Correct!
Just out of curiosity: do you have such a small family :P?

C:

c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;

Well, that's half of the work since you haven't initialized all four lucky numbers. You also need to write a loop which will print out all lucky numbers, if you've done question A yourself, you'll also be able to do this loop without much trouble.

D:

d.

Uhm, nothing? Where's your attempt? Where are you having problems with?

c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;
luckyNums[2] = 12;
luckyNums[3] = 21;

for(i = 0; 1 < luckyNums.lengh;i++)
{

luckyNums[i] = i + 1;

System.out.println( luckyNums[i]);

} // is that right? im not sure what "1" needs to be an "i".


d. int[][] multiArray = new int[10][10];
for(i = 0; 1 < multiArray.lengh;i++)
{

multiArray[i] = i + 1;

System.out.println( multiArray[i]);
} //thats what i have for D so far?
}

a.

int[] numArray = new int[10];
for(int i = 0; i < numArray.lengh;i++)
{

numArray[i] = i + 1;

System.out.println( numArray[i]);
}

b.

String[] names = {"Rich", "Andrew", "Marissa"};

c.

int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;
luckyNums[2] = 12;
luckyNums[3] = 21;

for(int i = 0; i < luckyNums.lengh;i++)
{

luckyNums[i] = i + 1;

System.out.println( luckyNums[i]);

} // is that right? im not sure what "1" needs to be an "i".

d.

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.lengh;i++){

multiArray[i] = i + 1;

for(int j = 0; j <multiArray.length; j++){

multiArray[j] = j + 1;

System.out.println( multiArray[i]);
System.out.println( multiArray[j]);
} 
}//thats what i have for D so far?

C:

c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;
luckyNums[2] = 12;
luckyNums[3] = 21;

for(i = 0; 1 < luckyNums.lengh; i++)
{

luckyNums = i + 1;

System.out.println( "" + luckyNums );
}

is that right? im not sure what "1" needs to be a "i" the first or second "1"

Okay, now you've successfully completed the first part of Question C, but the second part is still not fully correct, let's take a close look at it:

for(i = 0; 1 < luckyNums.lengh; i++)
{
  luckyNums[i] = i + 1;
  System.out.println( "" + luckyNums[i] );
}

First error: You haven't defined the variable 'i', before you can modify a variable's value, the variable you're referring to must be known. In Java you can define a variable as follows: [I]typeName[/I] [I]variableName[/I] [I][[/I]= value[I]][/I]; where: typeName indicates the variable type you want to create an instance of, variableName is the name you want to give to the variable, and (optionally) value denotes the initial value you want the variable to be initialized with.
When you want to define a variable you must make sure that: the variableName is not the name of an already existing identifier or Java keyword, and that you put the variable definition code before the place where the variable is accessed for the first time.
In your case it seems the most appropriate to just define and initialize the variable in the for-loop's initialization part, like this:

for(int i = 0; 1 < luckyNums.lengh; i++)
{
luckyNums = i + 1;
System.out.println( "" + luckyNums );
}

Second error:
In the conditional part of the for-loop you've written this piece of code: [COLOR="Red"]1[/COLOR] < luckyNums.lengh , while this code is syntactically correct, there's something wrong with it, can you see it?
The condition will always be true in your case since luckyNums.length is always bigger than 1 (explanation: luckyNums.length yields the number of elements in the array, since you defined the array to hold 4 elements, this will yield 4, thus: the conditional part of the for loop looks like this every time the condition is checked: 1 < 4 , since 1 is always less than 4, the condition is always evaluated to true, and the loop won't ever end).
Moreover this will cause your program to generate an exception at runtime because inside the loop's body you have code which accesses the array at a certain index kept in the variable 'i', since every time the loop iterates, the value in variable 'i' is increased by 1, there comes a time where variable 'i' holds a value bigger than luckyNums.length - 1 , when you then try to access luckyNums[i], your program will generate an exception at runtime simply because you can't access an element which doesn't exist.
Solution: replace the damn 1 with i. After this change, your code should look like this:

for(int i = 0; i < luckyNums.lengh; i++)
{
luckyNums = i + 1;
System.out.println( "" + luckyNums );
}

Third error:
Actually I shouldn't call this an error, but I see it as an 'unnecessary complexity', this is the line of code I'm offending: System.out.println( [COLOR="Red"]"" +[/COLOR] luckyNums[i] ); , again, the part in red denotes some error or an unnecessarity, the second one in this case.
If I were you, then I'd written that line like this instead: System.out.println( luckyNums[i] ); But your approach is correct as well, the only difference between my approach and yours is that your approach causes to call the System.out.println() method which takes a String, while mine will cause to run the System.out.println() method which takes an integer as it's argument.

Fourth error:
The (last?) error I was able to spot is the following line: [COLOR="Red"]luckyNums[i] = i + 1;[/COLOR] The reason why this line is wrong is obvious: you're overwriting all lucky numbers before they get displayed on the screen, so the output will be:

1
2
3
4

instead of:

3
7
12
21

After applying all fixes discussed in this post, your code should now look like:

for(int i = 0; i < luckyNums.lengh; i++)
{
  System.out.println( luckyNums[i] );
}

I hope you've now gained a better insight in the mistakes you had made, and of course that you won't make them ever again :)

a.

int[] numArray = new int[10];
for(int i = 0; i < numArray.lengh;i++)
{

numArray[i] = i + 1;

System.out.println( numArray[i]);
}

b.

String[] names = {"Rich", "Andrew", "Marissa"};

c.

int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;
luckyNums[2] = 12;
luckyNums[3] = 21;

for(int i = 0; i < luckyNums.lengh;i++)
{


System.out.println( luckyNums[i]);

}

d.

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.lengh;i++){

multiArray[i] = i + 1;

for(int j = 0; j <multiArray.length; j++){

multiArray[j] = j + 1;

System.out.println( multiArray[i]);
System.out.println( multiArray[j]);
}
}

how does that look?

how does that look?

A: Correct
B: Correct
C: Not Correct*
D: Not Correct**

* I've added an appropriate bugfix to my previous post, you can find it here
** Hint:

int[][] multiArray = new int[10][10];
for(int i = 0; i < multiArray.lengh; i++) {
multiArray = i + 1; // see *
for(int j = 0; j < multiArray.length; j++){
multiArray[j] = j + 1; // see *
System.out.println( multiArray );
System.out.println( multiArray[j] );
}
}

ok so for c i should look like

int[] luckyNums = new int[4];
luckyNums[0] = 1;
luckyNums[1] = 2;
luckyNums[2] = 3;
luckyNums[3] = 4;

for(int i = 0; i < luckyNums.lengh;i++)
{

System.out.println( luckyNums);

}

and D

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.lengh;i++){

for(int j = 0; j <multiArray.length; j++){


System.out.println( multiArray);
System.out.println( multiArray[j]);
}
}

A: Correct
B: Correct
C: Correct
D: Incorrect

This is your current code:

int[][] multiArray = new int[10][10];
for(int i = 0; i < multiArray.length; i++) {
  for(int j = 0; j < multiArray.length; j++) {
    System.out.println( multiArray[i] );
    System.out.println( multiArray[j] );
  }
}

(please use code tags and indent your code the next time)

Your assignments asks you to implement the following:

A program which uses a multidimensional array to store the times tables up to ten.

What you've done so far: you tried to implement a way to display all the values in the array, but the code to display an element is incorrect, it shouldn't be:

System.out.println( multiArray[i] );
System.out.println( multiArray[j] );

but

System.out.println( multiArray[i][j] );
  // This line causes each element in the array to be displayed
  // on a separate line, so the only output you'll get is just a list of
  // numbers, why not beautify it a bit by printing the numbers in a
  // way similar like this:
  // 1 x 1 = 1
  // 1 x 2 = 2
  // 1 x 3 = 3
  // etc.
  // I'll leave that as an exercise to you ;-)

Unless you've manually initialized multiArray with the times tables, you'll get a compiler error when trying to compile this code because you haven't initialized the elements in the array.
Since the assignment doesn't explicitly state that you have to initialize the array with the times tables, I would suggest you to not manually initialize the array.

Also notice that when your array does not have the same number of columns and rows, the above code would be incorrect.

i am getting 24 errors when i compile the code

 ----jGRASP exec: javac -g C:\Users\Mike\Desktop\Computer Science\Week 11\ArrayLab.java

ArrayLab.java:3: class, interface, or enum expected
int[] numArray = new int[10];
^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.lengh;i++)
^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.lengh;i++)
               ^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.lengh;i++)
                                  ^
ArrayLab.java:9: class, interface, or enum expected
System.out.println( numArray[i]);
^
ArrayLab.java:10: class, interface, or enum expected
}
^
ArrayLab.java:14: class, interface, or enum expected
int[] luckyNums = new int[4];
^
ArrayLab.java:15: class, interface, or enum expected
luckyNums[0] = 1;
^
ArrayLab.java:16: class, interface, or enum expected
luckyNums[1] = 2;
^
ArrayLab.java:17: class, interface, or enum expected
luckyNums[2] = 3;
^
ArrayLab.java:18: class, interface, or enum expected
luckyNums[3] = 4;
^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.lengh;i++)
^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.lengh;i++)
               ^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.lengh;i++)
                                   ^
ArrayLab.java:25: class, interface, or enum expected
}
^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.lengh;i++){
^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.lengh;i++){
               ^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.lengh;i++){
                                   ^
ArrayLab.java:30: class, interface, or enum expected
for(int j = 0; j <multiArray.length; j++){
               ^
ArrayLab.java:30: class, interface, or enum expected
for(int j = 0; j <multiArray.length; j++){
                                     ^
ArrayLab.java:32: class, interface, or enum expected
}
^
21 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

In the conditional part of the second for-loop change: multiArray.length to multiArray[i].length

still getting 21 errors.

 ----jGRASP exec: javac -g C:\Users\Mike\Desktop\Computer Science\Week 11\ArrayLab.java

ArrayLab.java:3: class, interface, or enum expected
int[] numArray = new int[10];
^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.length;i++)
^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.length;i++)
               ^
ArrayLab.java:4: class, interface, or enum expected
for(int i = 0; i < numArray.length;i++)
                                   ^
ArrayLab.java:9: class, interface, or enum expected
System.out.println( numArray[i]);
^
ArrayLab.java:10: class, interface, or enum expected
}
^
ArrayLab.java:14: class, interface, or enum expected
int[] luckyNums = new int[4];
^
ArrayLab.java:15: class, interface, or enum expected
luckyNums[0] = 1;
^
ArrayLab.java:16: class, interface, or enum expected
luckyNums[1] = 2;
^
ArrayLab.java:17: class, interface, or enum expected
luckyNums[2] = 3;
^
ArrayLab.java:18: class, interface, or enum expected
luckyNums[3] = 4;
^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.length;i++)
^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.length;i++)
               ^
ArrayLab.java:20: class, interface, or enum expected
for(int i = 0; i < luckyNums.length;i++)
                                    ^
ArrayLab.java:25: class, interface, or enum expected
}
^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.length;i++){
^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.length;i++){
               ^
ArrayLab.java:29: class, interface, or enum expected
for(int i = 0; i <multiArray.length;i++){
                                    ^
ArrayLab.java:30: class, interface, or enum expected
for(int j = 0; j <multiArray[i].length; j++){
               ^
ArrayLab.java:30: class, interface, or enum expected
for(int j = 0; j <multiArray[i].length; j++){
                                        ^
ArrayLab.java:32: class, interface, or enum expected
}
^
21 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

ok i fixed it. here is my code.

//Mike DiBiasio
//March 2 2010

public class ArrayLab {
   public static void main(String[] args) {

int[] numArray = new int[10];
for(int i = 0; i < numArray.length;i++)
{

numArray[i] = i + 1;

System.out.println( numArray[i]);
}

String[] names = {"Rich", "Andrew", "Marissa"};

int[] luckyNums = new int[4];
luckyNums[0] = 1;
luckyNums[1] = 2;
luckyNums[2] = 3;
luckyNums[3] = 4;

for(int i = 0; i < luckyNums.length;i++)
{

System.out.println( luckyNums[i]);

}

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.length;i++){
for(int j = 0; j <multiArray[i].length; j++){
System.out.println( multiArray[i][j] );
}
}

}

}

WHEN I RUN IT THE RESULTS ARE:

----jGRASP exec: java ArrayLab

1
2
3
4
5
6
7
8
9
10
1
2
3
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

----jGRASP: operation complete.
is that how its supposed to be?

Please post down the whole code you're trying to compile, just copy it from your editor, and paste it between code tags in a new post.
I guess you forgot to put your code into a method into a class.

ok i fixed it. here is my code.

//Mike DiBiasio
//March 2 2010

public class ArrayLab {
   public static void main(String[] args) {

int[] numArray = new int[10];
for(int i = 0; i < numArray.length;i++)
{

numArray[i] = i + 1;

System.out.println( numArray[i]);
}

String[] names = {"Rich", "Andrew", "Marissa"};

int[] luckyNums = new int[4];
luckyNums[0] = 1;
luckyNums[1] = 2;
luckyNums[2] = 3;
luckyNums[3] = 4;

for(int i = 0; i < luckyNums.length;i++)
{

System.out.println( luckyNums[i]);

}

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.length;i++){
for(int j = 0; j <multiArray[i].length; j++){
System.out.println( multiArray[i][j] );
}
}

}

}

WHEN I RUN IT THE RESULTS ARE:

----jGRASP exec: java ArrayLab

1
2
3
4
5
6
7
8
9
10
1
2
3
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

----jGRASP: operation complete.
is that how its supposed to be?

is that how its supposed to be?

No, you forgot to fill multiArray with the times tables.
Because you didn't initialize the array, every element is automatically assigned a default value, in case of an integer, this default value is zero (that's the reason why the last 100 lines of your output are zeroes).

In one of my previous posts I wrote this:

Unless you've manually initialized multiArray with the times tables, you'll get a compiler error when trying to compile this code because you haven't initialized the elements in the array.

, this is incorrect, just forget that I said it.

public class ArrayLab {
   public static void main(String[] args) {

int[] numArray = new int[10];
for(int i = 0; i < numArray.length;i++)
{

numArray[i] = i + 1;

System.out.println( numArray[i]);
}

String[] names = {"Rich", "Andrew", "Marissa"};

int[] luckyNums = new int[4];
luckyNums[0] = 1;
luckyNums[1] = 2;
luckyNums[2] = 3;
luckyNums[3] = 4;

for(int i = 0; i < luckyNums.length;i++)
{

System.out.println( luckyNums[i]);

}

int[][] multiArray = new int[10][10];
for(int i = 0; i <multiArray.length;i++){
for(int j = 0; j <multiArray[i].length; j++){
multiArray[i][j]=(i+1)*(j+1);
System.out.print( multiArray[i][j]+" ");
if(multiArray[i][j]<10)
System.out.print(" ");
if(j==9)
System.out.println();

}
}

}

}

how does that look?

how does that look?

The code seems to produce the expected output, although I would recommend you to indent your code before you submit it for a grade.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.