473,289 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Help with a java program (arrays)

Hi, I need help with my java program. I am new with java, so go easy on me if this is a dumb question.

Ok the question is

"You will complete two methods for this exercise. The headers (and a comment that describes each one) are below: Create a main method that asks the user for the length of the array, uses readArray to read in the array, and uses printArray to print the resulting array."

[PHP]public static void readArray(int a[], int length)
//This method reads "length" values into array a

public static void printArray(int a[], int length)
//This method prints the values from array a[/PHP]

The program that I am working with is:

[PHP]// Example9.java
import java.util.Scanner;
public class Example9{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
final int MAX = 5;
int[] a = new int[MAX];
int i = 0;
while (i < MAX){
System.out.print("Enter a number: ");
int n = sc.nextInt();
a[i] = n;
++i;
}
System.out.println("The data are:");
i = 0;
while (i < MAX){
System.out.println(a[i]);
++i;
}
}

}[/PHP]
Nov 15 '08 #1
13 2089
Nepomuk
3,112 Expert 2GB
OK, first of all: Welcome to bytes.com!

It's great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags (as you seemingly have done here :-) ).

OK, about your task: Do you understand, what the program you have does? For example, what does
Expand|Select|Wrap|Line Numbers
  1. final int MAX = 5;
  2. int[] a = new int[MAX];
do? That's code you'll have to change!
Next, you have a while loop:
Expand|Select|Wrap|Line Numbers
  1. while (i < MAX){
  2.    System.out.print("Enter a number: ");
  3.    int n = sc.nextInt(); 
  4.    a[i] = n;
  5.    ++i;
  6. }
This will have to be changed to - but do you understand, what it does now?
Last but not least, there's
Expand|Select|Wrap|Line Numbers
  1. while (i < MAX){
  2.    System.out.println(a[i]);
  3.    ++i;
  4. }
What does that take care of at the moment?

Oh, I assume you got those fuction headers from your teacher? I don't know if he realises, but this is Java, not C++, so arrays like a have a method a.length(), which gives you the length of the array.

Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

Greetings,
Nepomuk
Nov 15 '08 #2
[PHP]final int MAX = 5;
int[] a = new int[MAX]; [/PHP]

Thanks for the nice welcome

Ok I understand that I can enter a number a total of 5 times into the java program. The other two you posted I sorta got a good idea of what it means. The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.

The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.
Nov 15 '08 #3
Nepomuk
3,112 Expert 2GB
Ok I understand that I can enter a number a total of 5 times into the java program.
Correct. Now, you want to be able to choose, how many you enter, right? So, you'll have to change something there.
The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.
I'm not quite sure - you may mean the right thing, but you may not, so I'll explain it here:
You have an array which has the length 5. "i" is a counter (or index), which is set to 0 at first. It is, as you said correctly, incremented, but before that, it is used in the array.
Now how is it used in the array? An array has a certain number of values it can save, and each of these values has an index number. So, if a is an array, then a[0] is the first element of a. So, i goes through the possible numbers for elements in a, so that you access a different one each time.
The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.
Correct, and "i" is incremented again. (By the way, there's a special type of loop called the for loop for that sort of task, but that's probably still going to be addressed in your course.)

OK, what else do you have to know? That Scanner called sc can, as you see in the first loop, read input from the user. Now, is there anywhere you would like to read user input, but don't so far?

Greetings,
Nepomuk
Nov 15 '08 #4
Sorry, I don't know what you mean by "Now, is there anywhere you would like to read user input, but don't so far?"
Nov 15 '08 #5
Nepomuk
3,112 Expert 2GB
Create a main method that asks the user for the length of the array...
Greetings,
Nepomuk
Nov 15 '08 #6
Ok, how bout this.

[PHP]System.out.print ("Enter length for the array: ");[/PHP]
Nov 15 '08 #7
Nepomuk
3,112 Expert 2GB
Good start, but that's just output. How do you get the answer from the user and use it?

Greetings,
Nepomuk
Nov 15 '08 #8
Good start, but that's just output. How do you get the answer from the user and use it?

Greetings,
Nepomuk
How about

[PHP]
System.out.print ("Enter length for the array: " );
int[] a = sc.nextInt(); [/PHP]
Nov 15 '08 #9
Nepomuk
3,112 Expert 2GB
How about

[PHP]
System.out.print ("Enter length for the array: " );
int[] a = sc.nextInt(); [/PHP]
Good try, but not quite right. Why not?
First of all, you want to use a somewhere else, so choose another name. Maybe n? Or length? Or iCantThinkOfAnythingThatMakesSenseSoIllJustGoForTh is?
The other error is, that you define your variable as a int[] here - an array of integers. But you don't want an array of integers, you only want one. Can you figure out how to correct your code?

Greetings,
Nepomuk
Nov 15 '08 #10
ok, then how about this.

[PHP]System.out.print ("Enter length for the array: " );
int n = sc.nextInt(); [/PHP]
Nov 15 '08 #11
Nepomuk
3,112 Expert 2GB
Great, that will do the job! Now, what does your code look like, when you use that? Remember, you will want to use the n in some places...

Greetings,
Nepomuk

PS.: You can use [code] instead of [PHP] for posting your code - it looks the same at the moment, but it will change at some point. (At least, hopefully.)
Nov 15 '08 #12
ok, I think this is correct


[PHP]// Example9.java
import java.util.Scanner;
public class Example9{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
final int MAX = 5;
int[] a = new int[MAX];
int i = 0;
System.out.print ("Enter length for the array:");
int n = sc.nextInt();

while (i < MAX){
System.out.print("Enter a number: ");

a[i] = n;
++i;

}
System.out.println("The data are:");
i = 0;
while (i < MAX){
System.out.println(a[i]);
++i;
}
}

}[/PHP]
Nov 15 '08 #13
JosAH
11,448 Expert 8TB
ok, I think this is correct
No it isn't; what if the user types '7' because she wants the program to process
an array with seven elements; your program just processes MAX == 5 elements,
no matter what was typed.

kind regards,

Jos
Nov 15 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
2
by: Zerofury | last post by:
Okay this is what i'm attempting to do. I have to modify this program that i wrote so that it allows the user to list items by alpha as an option on the main menu. Here is my problem. If i sort the...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
2
by: Prime8 | last post by:
I've been researching so much about arrays and matrices and such, but it hasn't helped me at all on my program. Everything is either over my head or doesn't help with the specific program I have to...
7
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.