Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > I need Java help!

I need Java help!
Thread Tools
Senior User
Join Date: Dec 2002
Location: Canada.
Status: Offline
Reply With Quote
Nov 13, 2006, 08:30 AM
 

import psJava.KeyIn;

public class ExamStats {

private int [ ] scores;

public ExamStats (int num) {
scores = new int [num];
}

public void readScores () {
for (int i = 0; i < scores.length; i++)
scores[i] = KeyIn.readInt("Exam score for the next student:");
}

public int findMin () {
if (scores.length > 0) {
int minSoFar = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] < minSoFar)
minSoFar = scores[i];
}
return minSoFar;
}
else
return 0;
}

public int findMax () {
if (scores.length > 0) {
int maxSoFar = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] > maxSoFar)
maxSoFar = scores[i];
}
return maxSoFar;
}
else
return 0;
}

public double computeAverage () {
int sum = 0;
for (int i = 0; i < scores.length; i++)
sum += scores[i];
if (scores.length > 0)
return (double)sum / (double)scores.length;
else
return 0;
}
public double computeStaDeviation(double average) {
int sumSquares = 0;
for (int i = 0; i < scores.length; i++)
sumSquares += scores[i] * scores[i];
if (scores.length > 0)
return Math.sqrt((double) sumSquares / scores.length - average * average);
else
return 0;
}

public String toString () {
String examStr = "";
for (int i = 0; i < scores.length; i++) {
examStr = examStr + scores[i] + " ";
}
return examStr;
}
}
Compile Time Errors (the bold text):

Error: package psJava does not exist

Error: cannot find symbol
symbol : variable KeyIn
location: class ExamStats


Basically I do not understand how to remedy this. I tried searching online and through my notes, and I cannot find an answer.
..13" MacBook Pro | 2.53gHz | 4gb RAM | 320gb Seagate Momentus XT | OSX.6.6.. // iPhone 4 32gb
     
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Nov 13, 2006, 09:17 AM
 
You haven't instantiated the KeyLn object first in that procedure. A correction would look something like this:
public void readScores () {
Keyln input = new Keyln();
for (int i = 0; i < scores.length; i++)
scores[i] = input.readInt("Exam score for the next student:");
}
     
Senior User
Join Date: Dec 2002
Location: Canada.
Status: Offline
Reply With Quote
Nov 13, 2006, 09:20 AM
 
It's not KeyLn it's KeyIn. I've checked my notes, and the way I posted the code is exactly how it is in the notes. I do not get why this is happening.
..13" MacBook Pro | 2.53gHz | 4gb RAM | 320gb Seagate Momentus XT | OSX.6.6.. // iPhone 4 32gb
     
Mac Elite
Join Date: Nov 2005
Location: Seattle, WA, USA
Status: Offline
Reply With Quote
Nov 13, 2006, 05:32 PM
 
Nonetheless, you still need to instantiate the object. sray must have meant "KeyIn", and not "KeyLn". Otherwise, his suggestion is valid.

Any ramblings are entirely my own, and do not represent those of my employers, coworkers, friends, or species
     
Senior User
Join Date: Dec 2002
Location: Canada.
Status: Offline
Reply With Quote
Nov 14, 2006, 03:25 AM
 
Actually it was caused because I didn't have the psJava package in my extensions.

I do appreciate your posts though because they're still useful to me.
..13" MacBook Pro | 2.53gHz | 4gb RAM | 320gb Seagate Momentus XT | OSX.6.6.. // iPhone 4 32gb
     
Mac Elite
Join Date: Oct 2004
Location: Downtown Austin, TX
Status: Offline
Reply With Quote
Nov 15, 2006, 12:13 PM
 
Java puked all over that because it couldn't find the package. For future reference, Sun's provided packages usually start with Sun.java.* etc etc. Any other packages need to be copied to the extensions directory, but you know that now.
     
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Nov 17, 2006, 08:26 AM
 
1) Any other packages need to be added to the application classpath! Adding something to the Extesions directory (the global classpath) is the worst you can do because it may screws other apps.

2) The readInt(...) method might be a static method in which case KeyIn.readInt(...) is correct.
     
Fresh-Faced Recruit
Join Date: May 2003
Status: Offline
Reply With Quote
Nov 17, 2006, 10:05 AM
 
psJava is almost certainly a package provided for you by your professor. Contact your professor for help with getting your environment set up. If you are getting this code from elsewhere, you will not be able to use it without modifying it to use standard libraries.
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 1, 2006, 12:22 PM
 
When using the command line, look at the -classpath (and -cp) params to the javac and java commands. (Use man or java/javac [no args] for info on these)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:40 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2