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("\n\nPlease 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("\n\nEnter 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.
> 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!
> 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.
> This is what I tried to do but it really didn't work:
> ================================================
[quoted text clipped - 11 lines]
>
> if (comptsIn[x] > 0);
Empty if block - remove the semicolon.
> {
> for (int i = 0 ; i < comptsIn.length ; i++)
[quoted text clipped - 10 lines]
> int comptsNum;
> int markNum;
variable markNum defined, but not initialized.
> do
> {
[quoted text clipped - 5 lines]
> int [ ] compts = new int [comptsNum];
> getCompts(compts);
See below:
> int [ ] marks = new int [markNum];
This should throw a NullPointer exception.
> getCompts(marks);
Your getCompts method expects two arrays, not one, so perhaps you mean:
getCompts(compts, 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.
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("\n\nPlease 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("\n\nEnter 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
Yuriy_Ivanov - 08 Jan 2006 18:08 GMT
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.
Mark Thomas - 08 Jan 2006 19:45 GMT
> 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
[quoted text clipped - 42 lines]
> 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
Yuriy_Ivanov - 13 Jan 2006 14:58 GMT
>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("\n\nPlease 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("\n\n\tResults Breakdown\n\n "
+ "\t==========================\n");
System.out.println("\tFails :\t\t\t " );
System.out.println("\tPasses :\t\t " );
System.out.println("\tMerits :\t\t " );
System.out.println("\tDistinctions :\t\t " );
System.out.println +("\n\t==========================\n\n"
+ "\tTotal graded: \t\t " );
System.out.println("\tPercentage failed :\t " );
System.out.println ("\t==========================\n\n");
System.out.println("\n\n\tThe '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("\n\nPlease enter the forename : ");
foreName = EasyIn.getString();
while (foreName.length() < 0 || foreName.length() > 20);
{
System.out.print("\n*** Error: forename is " + foreName.length() + "
characters\n "
+ " Enter forename between 2-20 characters : ");
foreName = EasyIn.getString();
}
System.out.print("\n\nPlease enter the surname : ");
surName = EasyIn.getString();
while (surName.length() < 0 || surName.length() > 20);
{
System.out.print("\n*** Error: surname is "
+ surName.length() + " characters\n "
+ " Enter forename between 2-20 characters : ");
surName = EasyIn.getString();
}
do {
System.out.print("\n\nEnter 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("\n\nDo 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("\n\n\t\t\t*** End of program ***
+ \t\t\n\n" );
}
}
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.