View Full Version : Anybody good at JAVA?
If u are, can u tell me whats wrong here, and how i sort it?
static void outputAveSpeed(int []CarNo, int []Laps, double Speed) throws IOException{
boolean CarNum;
double AveSpeed =0;
screen.println("Type in the car number whos average speed you require");
screen.flush();
CarNum = [i] keyboard.readLine().intValue();
for ( int i=0; i<50; i++){
if(CarNo[i]>0){
AveSpeed = Speed * Laps[i]; //to find the average speed of a given car number
screen.println("The average Speed of car number " + CarNo + " was " + AveSpeed + " Kph");
}
} //average speed is the laps times the speed
} //end of outputAveSpeed
*****
Its part of a program im doing to be handed in 2moro.
This is the only bit i cant get to work.
the basic idea is to be able to punch in a number of a car thats raced, and this program will pick out the number and its laps from a parrallel array, but when i try to compile it, it gives me 1 error, that this line :-
CarNum = [i] keyboard.readLine().intValue();
Is invalid due to the "[i]" being in the statement.
Im not being very clear here i realise!
Basically i want a command that will read a number i input and im not sure how to get my program to do this.
I realise this will be silly piddley stuff for ,most of u, but its my first programming module of the IT degree, ive never seen this stuff b4.
Kev
OH god, only done a TINY bit of Java but plenty of other programming languages.... lets see ....
Right, first off you've declared CarNum as boolean, this would be a TRUE / FALSE (1/0). Did you mean to declare it as an array instead??
Dave
You also don't appear to have an else statement, or anything for it to do if the Carnum < 0 ?
Dave
Aaah having a brainwave here,
wouldnt it be
CarNum[i] = keyboard.readline().intValue();
// This would suggest that whatever the CarNum array was initialised with (0??) we would start at. Ie, CarNum[0] then i++ CarNum(1) etc ?
rather than
CarNum = [i] etc ?????
Dave
and another ! sorry!
Any reason you've declared AveSpeed as a double?? wouldn't int have done?? surely you don't need a double?? or did you want the decimal places?
Dave
Sorry mate, that aint worked either.
The array is the two mentioned in the original line, CarNo and Laps, both int's.
Avespeed was double cos as u reckoned, i want dps for more acuracy.
Its telling me that it cant resolve symol for the squate brackets, nor the brackets after readLine???????
And it dont like the boolean now, cos its wanting an array, was a class sugestion anyway mate.
I dont want it to count up the cars from 0 though, only give me the laps of the car i request, then work the avespeed from that.
I appreciate ur efforts mate, cos im pulling my hair out just now.
Gonna head down the pub for a few hours break, then ill attack it again when i get back, so any ideas, just keep piling them up mate.
Kev
No probs,
On a late shift at work and it's gone quiet.
Just gonna download the Java SDK from sun.com and have a go myself :)
fingers crossed.
Dave
U want me to e-mail u the program so it makes a tad more sence?
Kev
Thatd be cool thanks.
I'll pm you my work email addy.
Dave
Hope you guys manage to get it sorted. I'd help but I know nothing about Java. And many apologies, Kev, for not coming up with any help before...things got a bit hectic at work and I'm ashamed to say that I never got round to it.
Right,
I think I've sort of fixed it :)
Check out the email I sent. For all those who may know, is this ok ..... (I practically had to learn Java to do this LOL :D )
//start of method outputAveSpeed. It receives the arrays of CarNo, Laps
//and Speed. It outputs to the screen the average Speed of the given CarNo.
//INPUT: CarNo (int[]), Lap (int[]), Speed (double[])
//OUTPUT: none
static void outputAveSpeed(int []CarNo, int []Laps, double Speed) throws IOException{
int[] CarNum = new int[50];
int x=0;
double AveSpeed =0;
screen.println("Type in the car number whos average speed you require");
screen.flush();
DataInputStream in = new DataInputStream(System.in);
CarNum[x] = in.readInt();
for (x=0; x<50; x++){
if(CarNo[x]>0){
AveSpeed = Speed * Laps[x]; //to find the average speed of a given car number
screen.println("The average Speed of car number " + CarNo[x] + " was " + AveSpeed + " Kph");
}
} //average speed is the laps times the speed
} //end of outputAveSpeed
It compiles and runs now at least :)
I've entered in data for 5 cars then chosen the average speed option and it showed 5 cars and some average speeds. You'll need to piss around with the amount of Decimal points though lol. Also another exception error :( It's taken me from when you emailed me the code until now to sort though and my brain is tired so I'll leave that one up to you :D
Good luck btw :)
Dave
Dave, ur a start mate, thats excellent, ill have a faff about and see if i can get by the exception thinggy, but at least its compiling now.
i owe u a big one mate:)
Trace, its fine, dinnae worry.
Im sure if they move me on to COBOl ill be giving u a shout;)
Kev
:eek: :eek: :eek:
i Just realised its bloody 6;20 in the morn, which means ive been faffin about for 4 hours!!!!!!
Ok i re-arranged thigs as many ways as is possible and eventually came up with this:-
static void outputAveSpeed(int []CarNo, int []Laps, double Speed) throws IOException{
int CarNum =0;
double AveSpeed =0;
screen.println("Type in the car number whos average speed you require");
screen.flush();
CarNum = new Integer (keyboard.readLine()).intValue();
for ( int i=CarNum; i<50; i=50){
if(CarNo[i]<0 && CarNo[i]>50){
screen.println("the number u have entered is incorrect");
screen.println("please input a car that was racing today");
}
else{
AveSpeed = Speed * Laps[i]; //to find the average speed of a given car number
screen.println("The average Speed of car number " + CarNo [i]+ " was " + AveSpeed + " Kph");
}
//average speed is the laps times the speed
}
} //end of outputAveSpeed
This works almost exactly how its meant to, with one problem........ when i give it numbers it basically takes them and puts them into the array as its meant to, but when i call them back, im using the number of the array (0,1,2,3,4) rather than the car number, so help.
Its really too late and i need to sleep now, ive to hand this thing in at 4pm 2moro, but i need to do rep[orts for it too.
I think ill just need to hand it in mostly working unless anyone (Dave;) or any other IT bods) can give me a quick fix?
Kev
Aha,
I was hoping you might re-write it with the things that you'd already learnt :)
I'll have another bash this morning and see if I can help (without drastically changing your code :p )
Dave :D
IGNORE this, I was talking arse lol
Dave
Kev,
Are you planning on accepting 50 cars eventually, or is it always going to be 5?
Dave
Just woke up, cos i never got to bed till bloody 8:30:eek: :eek:
dave, cheers mate, but ive decided im just gonna hand it in as is, only 30 % of the marks is on the actual program running, and it runs 90% so im just gonna risk it.
Thanx alot mate, ur contributions were very much appreciated:)
Kev
No probs :)
Glad to have been of a little help ;)
I might keep messing around with it anyway at work because it never hurts to learn another language :)
Dave
Just seem this, hope you guys have sorted it.
If I'd seen it ealier i would have screamed cos just finished a java assingment for uni that's been doing my head in for two days!
Well,
I think Kev handed it in, sort of working :)
Any ideas how to sort it, it's bugging ME now :)
Dave
Powered by vBulletin® Version 4.1.9 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.