AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

2 Arrays (New to arrays)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Yuriy_Ivanov
Guest





PostPosted: Sun Jan 08, 2006 11:51 am    Post subject: 2 Arrays (New to arrays) Reply with quote




I have an array within a method that works with a loop. It works to
collect components which act as boundaries.

e.g Mark input cannot be more than 10 if user inputs 10 for first
component.

I need another array that collects the user input marks for these
components.

But I don't know quite how to go about doing it because these arrays need
to work together and it won't let me put two arrays in one method...


This is what I tried to do but it really didn't work:
================================================
(segment of code, class and braces not included)

public static void getCompts(int [] comptsIn, int [] marks)
{
System.out.println("nnPlease enter " + comptsIn.length + " mark
components ");

for (int x = 0 ; x < comptsIn.length ; x++)
{
System.out.print("Component Number " + (x+1) + " : ");
comptsIn[x] = EasyIn.getInt();

if (comptsIn[x] > 0);
{
for (int i = 0 ; i < comptsIn.length ; i++)
{
System.out.print("Mark Number " + (i+1) + " : ");
marks[i] = EasyIn.getInt();
}
}
}

public static void main (String [ ] args)
{

int comptsNum;
int markNum;
do
{
System.out.print("nnEnter number of mark components (1-5) : ");
comptsNum = EasyIn.getInt();
}
while (comptsNum <1 || comptsNum >5);


int [ ] compts = new int [comptsNum];
getCompts(compts);


int [ ] marks = new int [markNum];
getCompts(marks);
=================================================


As you can see it's a big mess. Please can someone suggest something that
I can do to get this input working together.

Back to top
Mark Thomas
Guest





PostPosted: Sun Jan 08, 2006 3:23 pm    Post subject: Re: 2 Arrays (New to arrays) Reply with quote



Yuriy_Ivanov wrote:
Quote:
I have an array within a method that works with a loop. It works to
collect components which act as boundaries.

e.g Mark input cannot be more than 10 if user inputs 10 for first
component.

? Better explanation please!

Quote:

I need another array that collects the user input marks for these
components.

But I don't know quite how to go about doing it because these arrays need
to work together and it won't let me put two arrays in one method...

Of course it will. You've another problem somewhere.

Quote:


This is what I tried to do but it really didn't work:
================================================
(segment of code, class and braces not included)

public static void getCompts(int [] comptsIn, int [] marks)
{
System.out.println("nnPlease enter " + comptsIn.length + " mark
components ");

for (int x = 0 ; x < comptsIn.length ; x++)
{
System.out.print("Component Number " + (x+1) + " : ");
comptsIn[x] = EasyIn.getInt();

if (comptsIn[x] > 0);

Empty if block - remove the semicolon.

Quote:
{
for (int i = 0 ; i < comptsIn.length ; i++)
{
System.out.print("Mark Number " + (i+1) + " : ");
marks[i] = EasyIn.getInt();
}
}
}

public static void main (String [ ] args)
{

int comptsNum;
int markNum;

variable markNum defined, but not initialized.

Quote:
do
{
System.out.print("nnEnter number of mark components (1-5) : ");
comptsNum = EasyIn.getInt();
}
while (comptsNum <1 || comptsNum >5);


int [ ] compts = new int [comptsNum];
getCompts(compts);

See below:

Quote:


int [ ] marks = new int [markNum];

This should throw a NullPointer exception.

Quote:
getCompts(marks);

Your getCompts method expects two arrays, not one, so perhaps you mean:
getCompts(compts, marks);

Quote:
=================================================


As you can see it's a big mess. Please can someone suggest something that
I can do to get this input working together.


Yes it is a big mess. You need to take it one step at a time, working
out what you want to do before trying to jump in to coding. Use the
facilities of your IDE to layout your code - the indentation you have
shown here does not reflect what your code is doing, like the empty if
statement.

Here is your code cleaned up and properly indented. It probably doesn't
do what you want, because you haven't explained what you want, but it
might get you going:

public class Test {

private static void getCompts(int[] comptsIn,
int[] marks) {
System.out.println("nnPlease enter "
+ comptsIn.length
+ " mark components ");

for (int x = 0; x < comptsIn.length; x++) {
System.out.print("Component Number "
+ (x + 1) + " : ");
comptsIn[x] = EasyIn.getInt();

if (comptsIn[x] > 0) {
for (int i = 0; i < comptsIn.length; i++) {
System.out
.print("Mark Number "
+ (i + 1)
+ " : ");
marks[i] = EasyIn.getInt();
}
}
}
}

public static void main(String[] args) {
int comptsNum;
int markNum = 5;
do {
System.out
.print("nnEnter number of mark components (1-5) : ");
comptsNum = EasyIn.getInt();
} while (comptsNum < 1 || comptsNum > 5);

int[] compts = new int[comptsNum];

int[] marks = new int[markNum];
getCompts(compts, marks);
}
}

Good luck

Mark

Back to top
Mark Thomas
Guest





PostPosted: Sun Jan 08, 2006 3:24 pm    Post subject: Re: 2 Arrays (New to arrays) Reply with quote



Yuriy_Ivanov wrote:
Quote:
I have an array within a method that works with a loop. It works to
collect components which act as boundaries.

e.g Mark input cannot be more than 10 if user inputs 10 for first
component.

? Better explanation please!

Quote:

I need another array that collects the user input marks for these
components.

But I don't know quite how to go about doing it because these arrays need
to work together and it won't let me put two arrays in one method...

Of course it will. You've another problem somewhere.

Quote:


This is what I tried to do but it really didn't work:
================================================
(segment of code, class and braces not included)

public static void getCompts(int [] comptsIn, int [] marks)
{
System.out.println("nnPlease enter " + comptsIn.length + " mark
components ");

for (int x = 0 ; x < comptsIn.length ; x++)
{
System.out.print("Component Number " + (x+1) + " : ");
comptsIn[x] = EasyIn.getInt();

if (comptsIn[x] > 0);

Empty if block - remove the semicolon.

Quote:
{
for (int i = 0 ; i < comptsIn.length ; i++)
{
System.out.print("Mark Number " + (i+1) + " : ");
marks[i] = EasyIn.getInt();
}
}
}

public static void main (String [ ] args)
{

int comptsNum;
int markNum;

variable markNum defined, but not initialized.

Quote:
do
{
System.out.print("nnEnter number of mark components (1-5) : ");
comptsNum = EasyIn.getInt();
}
while (comptsNum <1 || comptsNum >5);


int [ ] compts = new int [comptsNum];
getCompts(compts);

See below:

Quote:


int [ ] marks = new int [markNum];

This should throw a NullPointer exception.

Quote:
getCompts(marks);

Your getCompts method expects two arrays, not one, so perhaps you mean:
getCompts(compts, marks);

Quote:
=================================================


As you can see it's a big mess. Please can someone suggest something that
I can do to get this input working together.


Yes it is a big mess. You need to take it one step at a time, working
out what you want to do before trying to jump in to coding. Use the
facilities of your IDE to layout your code - the indentation you have
shown here does not reflect what your code is doing, like the empty if
statement.

Here is your code cleaned up and properly indented. It probably doesn't
do what you want, because you haven't explained what you want, but it
might get you going:

public class Test {

private static void getCompts(int[] comptsIn,
int[] marks) {
System.out.println("nnPlease enter "
+ comptsIn.length
+ " mark components ");

for (int x = 0; x < comptsIn.length; x++) {
System.out.print("Component Number "
+ (x + 1) + " : ");
comptsIn[x] = EasyIn.getInt();

if (comptsIn[x] > 0) {
for (int i = 0; i < comptsIn.length; i++) {
System.out
.print("Mark Number "
+ (i + 1)
+ " : ");
marks[i] = EasyIn.getInt();
}
}
}
}

public static void main(String[] args) {
int comptsNum;
int markNum = 5;
do {
System.out
.print("nnEnter number of mark components (1-5) : ");
comptsNum = EasyIn.getInt();
} while (comptsNum < 1 || comptsNum > 5);

int[] compts = new int[comptsNum];

int[] marks = new int[markNum];
getCompts(compts, marks);
}
}

Good luck

Mark

Back to top
Yuriy_Ivanov
Guest





PostPosted: Sun Jan 08, 2006 6:08 pm    Post subject: Re: 2 Arrays (New to arrays) Reply with quote

Thank you very much for clearing it up, I can think more clearly now after
getting muddled with what I started. It's good to know Arrays can be used
that way, sometimes compiler errors make it seem like nothing is
possible!

And yes, apologies, I should have explained more about what this program
is required to do which I will do now.

It gets a little complex which is why I got muddled in the first place
because I have to restrict so much.

1. The compts array collects components which are boundaries for the
marks.

2. The marks must be within this boundary but there must be one mark for
each component.

3. The components must add up to 100 thus the marks must be under 100,
they don't necessarily need to be 100 since a component could be out 25
with a mark of 15.

5. There must only be 5 components (but I included that in a while loop as
a start anyway and it seems to work).

So instead of asking for an input of 5 marks when the number of components
= 5, the program needs to ask for 5 components which set the highest number
the mark for that component can be.

Example-

I input 5 for the number of components.

component 1 = 20
(2-4)
component 5 = 20

total is 100

Then when marks are collected, they can only be a maximum of 20 for all
components. (but not necessarily 20 for all of them every time)

I hope this makes sense of things.

Also, is it possible for arrays to work in different methods? because I
have another method that finds the highest mark inputted so it would need
to corespond to the mark array.

Back to top
Mark Thomas
Guest





PostPosted: Sun Jan 08, 2006 7:45 pm    Post subject: Re: 2 Arrays (New to arrays) Reply with quote

Yuriy_Ivanov wrote:
Quote:
Thank you very much for clearing it up, I can think more clearly now after
getting muddled with what I started. It's good to know Arrays can be used
that way, sometimes compiler errors make it seem like nothing is
possible!

And yes, apologies, I should have explained more about what this program
is required to do which I will do now.

It gets a little complex which is why I got muddled in the first place
because I have to restrict so much.

1. The compts array collects components which are boundaries for the
marks.

2. The marks must be within this boundary but there must be one mark for
each component.

3. The components must add up to 100 thus the marks must be under 100,
they don't necessarily need to be 100 since a component could be out 25
with a mark of 15.

5. There must only be 5 components (but I included that in a while loop as
a start anyway and it seems to work).

So instead of asking for an input of 5 marks when the number of components
= 5, the program needs to ask for 5 components which set the highest number
the mark for that component can be.

Example-

I input 5 for the number of components.

component 1 = 20
(2-4)
component 5 = 20

total is 100

Then when marks are collected, they can only be a maximum of 20 for all
components. (but not necessarily 20 for all of them every time)

I hope this makes sense of things.

Also, is it possible for arrays to work in different methods? because I
have another method that finds the highest mark inputted so it would need
to corespond to the mark array.

Good - that's clear. Now have a go at doing it, based on the previous

reply, and get back to us with your next attempt. We'll help, but we
won't do it for you.

Mark

Back to top
Yuriy_Ivanov
Guest





PostPosted: Fri Jan 13, 2006 2:58 pm    Post subject: Re: 2 Arrays (New to arrays) Reply with quote

Quote:
Good - that's clear. Now have a go at doing it, >based on the previous
reply, and get back to us with your next attempt. >We'll help, but we
won't do it for you.

Mark

Well I've had ago at applying the cleaned up code to the program, it's
getting there at least. There are things within the code which have not
been calculated yet, I'm at this stage just attempting to get the mark and
component values working together before I do any more output
calculations.

This is my second attempt at it:

(The code shouldn't look too messy elsewhere, I still need to work on
clearer designs)


public class Marks2 {

public static void getInput(int[] compts, int[] marks) {
// Enter Components
System.out.println("nnPlease enter "+ compts.length +
+ " mark components ");

// Loop around until number of components reached
for (int x = 0; x < compts.length; x++) {
System.out.print("Component Number " + (x + 1) + " :
");
compts[x] = EasyIn.getInt();
compts[x]++;

// Loop to collect marks
for (int i = 0; i < 1; i++) {
System.out.print("Please enter mark : ");
marks[i] = EasyIn.getInt();
}

do {
for (int i = 0; i < 1; i++) {
System.out.print("***Error: Please enter valid
mark
+ : ");
marks[i] = EasyIn.getInt();
}
}

while (marks[x] > compts[x] || marks[x] < 0);

}
//System.out.print("Component Total " + compts[x] );
}


public static void findHighest(int[] compts, int[] marks ) {
int highest;
highest = marks[0];

for (int x = 1 ; x < marks.length ; x++) {
if (marks[x] > highest)
highest = marks[x];
}

System.out.println("Discounted scores : highest : " + highest);

}


public static void outputMarks(int[] compts, int[] marks) {

System.out.println("nntResults Breakdownnn "
+ "t==========================n");
System.out.println("tFails :ttt " );
System.out.println("tPasses :tt " );
System.out.println("tMerits :tt " );
System.out.println("tDistinctions :tt " );
System.out.println +("nt==========================nn"

+ "tTotal graded: tt " );
System.out.println("tPercentage failed :t " );
System.out.println ("t==========================nn");

System.out.println("nntThe 'best program of the year' award goes to:
n"
+ "twith a final mark of " );

}

public static void main(String[] args) {

String foreName;
String surName;
int comptsNum;
int markNum = 5;
char Yes_No;

do {

System.out.print("nnPlease enter the forename : ");
foreName = EasyIn.getString();

while (foreName.length() < 0 || foreName.length() > 20);
{
System.out.print("n*** Error: forename is " + foreName.length() + "
charactersn "
+ " Enter forename between 2-20 characters : ");
foreName = EasyIn.getString();
}


System.out.print("nnPlease enter the surname : ");
surName = EasyIn.getString();

while (surName.length() < 0 || surName.length() > 20);
{
System.out.print("n*** Error: surname is "
+ surName.length() + " charactersn "
+ " Enter forename between 2-20 characters : ");
surName = EasyIn.getString();
}

do {
System.out.print("nnEnter number of mark components
+ (1-5) : ");
comptsNum = EasyIn.getInt();

} while (comptsNum < 1 || comptsNum > 5);


int[] compts = new int[comptsNum];
int[] marks = new int[markNum];
getInput(compts, marks);
findHighest(compts, marks);
System.out.println("nnDo you wish to enter another
+ set of marks?");

do {
System.out.println("nPlease enter Y or N : ");
Yes_No = EasyIn.getChar();
}

while (Yes_No != 'N' && Yes_No != 'n' && Yes_No != 'Y'
+ && Yes_No != 'y');
}

while (Yes_No == 'Y' || Yes_No == 'y' );

if (Yes_No == 'N' || Yes_No == 'n' )
{
int[] compts = new int[comptsNum];
int[] marks = new int[markNum];

outputMarks(compts, marks);
}

System.out.println("nnttt*** End of program ***
+ ttnn" );

}
}

Ironically, the best programmer of the year award for this program
certainly won't go to me...

Based on what I explained in my previous post, it is still missing a few
things in order to output and calculate the right things.

There are a few errors in it, such as the array doesn't fill properly for
some reason and some of the error catching while loops don't work in the
way I intend them to but they should hopefully show what they are meant to
restrict.

Can I get some suggestions on how I can go about adding/fixing things so I
can take it away and work on it again?

If more information is needed please let me know.

Thanks in advance.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.