Win a copy of Head First Android this week in the Android forum!

Dun Dagda

Ranch Hand
+ Follow
since Oct 12, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dun Dagda

Hi,

perhaps there is something wrong with your definition of the reference variable to the File object fileTestOutput. Maybe it is pointing to a file that is read-only or to a directory that you don't have write access to?

I suggest that you post more of the exact error message that is being returned. Are you using a Java program to read the file that you have written? It is not clear from your post exactly what you are doing.

You might also want to take a look at the getBytes() method of the String class, which might help you do what you want more efficiently.

DD
17 years ago
Hi Maureen,

That's fantastic news, well done! I see you have been helping others out as well, so good for you!

I found these initial OOP conceptual hurdles that you have run into to be the hardest part of learning Java that I have encountered so far. The rest seems to be just remembering what the various packages do and applying the appropriate class and/or method to the problem at hand.

Once you submit and pass your first assignment (concentrate on writing up a good Test Plan and documenting your code well, the tutors like that), you will be invited by Computeach to attend an in-centre 2-day Java workshop at their training centre in Dudley, near Wolverhampton. This is a good way of getting to know your tutors and of meeting a few other students (there are usually 3 or 4 other people there at the same time). I found the course great for getting answers to questions face-to-face from qualified Java professionals and for making me more confident in my coding.

So once again, congratulations and very well done!!!
Thanks also for your kind comments, I like to help people out if I can.
DD



17 years ago
There is some online documentation from Sun about this here.
There are also various javax.sound packages, which seem to support a variety of audio formats. More about them
on this web page.

Hope this helps,

DD
17 years ago
Hi Maureen,

Just a couple of suggestions: why are you subtracting one from the hash index? In the case where a Student name's total ASCII codes add up to a multiple of the number of students to be stored, the expression
totalASCII % numStudents will give a value of zero. If you then subtract 1 from this, you will get -1, which is not a valid index to use in an array (you will get an ArrayIndexOutOfBoundsException and crash the program).
So that is one thing. The other is that you are converting the name to upper case before adding up its ASCII codes. This means that the string "Fred Smyth" will give a different hash code to "FRED SMYTH" as the ASCII codes for lower and upper case characters are different. This might cause your code to behave in unexpected ways.
See the ASCII table web page for a more complete explanation.

DD
17 years ago
Hi Maureen,

Looking at your own code again, I see that you are making a lot of your methods and variables static. There should be no need for you to do this, as you should be calling these methods on instances of the various Student objects and the Storage object created by your program, so Java should be able to find them and use them without needing to make them static.

In fact, making variables static in this way can interfere with correct operation of your code, as a static variable only has one instance per class, so all objects made from that class will all be looking at the same variable. This is probably not a good idea in your case, as you want your Student objects to be separate from each other, storing different name and course information for each Student.

DD
17 years ago
Hi Maureen,

This is due to the data collision problem I mentioned. The names "Joe Bloggs", "Fred Smyth" (with a y) and "Sally Collins" do not cause this problem, because they all evaluate to a different hashCode. However, pretty much any other set of names will give you trouble.

One of the main objectives of this assignment from Computeach is to get you to devise your own code for circumventing this problem, and therefore to make you appreciate how useful the java.util.Collections classes are, whenever you come to use those, since they take care of this problem for you (by and large).

To avoid data collision, you need to insert code in your storeStudent() method that will:

1) check if the array position specified by the hash code is available for use.
2) If it isn't available (e.g. already taken by a different Student object), then search for the next available position and store the object there.

Each time you call the storeStudent method with a Student object, it should carry out the above steps before attempting to store the object. That way you should avoid collisions.

See how you get on with that, post again if you are stuck.

DD
17 years ago
Hi,

another situation where you might want to synchronize on a block rather than a method is if you want to use an unsynchronized method from another class whose source code you don't have access to, but you require to use that method in a synchronized manner in your own code. In this case, since you cannot access the source code of the original method to add the synchronized keyword, you have no choice but to synchronize on a block of code via the object that calls the method you want to use.

DD
17 years ago
Hi,

I had the same problem with the jakarta commons library, but on a PC system running Windows XP. I found from the Java API documentation that if you put third-party library .jar files in the ext directory, which on my system is C:\jdk\jre\lib\ext, the javac compiler will find them on the classpath automatically, so you can use import statements in your source code files as you described, just like with any other core java package. Perhaps there is an equivalent directory on your UNIX system?

At least, when I did this, it seems to work OK on my system. I didn't have to use the -classpath flag of javac to get the source to compile.

DD
17 years ago
Here's something that might help you out.


Try pasting this into a .java file and compiling and running it yourself.

DD
17 years ago
I think you need a space between the new keyword and the File or FileReader class names. You probably also need to import java.io.* to use the File class and the various reader classes.
Apart from that, Java variable names are usually in lowercase and something descriptive that tells the programmer something about the object or primitive being referred to.

Good luck with your course!

DD
17 years ago
That is because charAt will return the ASCII code of the character at that position in the String. The ASCII code of the character 0 (zero) is 48, character 1 is ASCII 49, etc.
So to convert the character code for the character "1" to its numerical equivalent, you need to subtract 48 from the character code.

DD
17 years ago
Once you have sorted out all the typos and spelling in your code, you still have a problem with your grading algorithm: you are trying to do
s.grade == 'A' etc.
This will not work as you are trying to compare a String (grade) with a char primitive 'A'. These are incompatible types and cannot be compared by using the comparison operator ==.

You want to do something like:
s.grade.equals("A"),
this should work better.
Apart from that, I can't see too many compiler errors with your code, except I'm not sure that the counting methods you are using are doing what you want.

DD
17 years ago
This is what a number of us were trying to suggest to you with the code we posted above.
You can think of String objects in Java as a sort of array: starting from the leftmost position, this is equivalent to the element at the first position in a Java array, with the index numbering starting from zero.

Thus in your string "1234567",
character at index 0 is 1
character at index 1 is 2
character at index 2 is 3

... and so on up to the end of the string.

To find out the size of the String, and thus the size of the string "array", you can call the String.length() method on the String, to return an integer defining its length, i.e
(number of characters in String) - 1, since numbering starts at index zero.

Thus to return the character at a particular index, you call the charAt()
method on the string, thus myString.charAt(2), will return the char representation of the character at index 2, which in the case of the string
"1234567" is the Unicode value corresponding to the character "3".
So to go from right to left, you can use a for loop:
for (int i = (myString.length()-1); i >= 0; i--)
this tells the JVM:
"initialise the int primitive i to the length of the string myString -1,
i.e. the position of the rightmost character. Then, for as long as i is
greater than or equal to zero, execute the expressions in the for loop, decrementing i by 1 after each pass through the loop".
You can then read the characters one at a time from right to left on each pass through the for loop, convert them to integers using one of the methods described in the postings above, and use them to build up the value of your decimal total.
OK?

DD
17 years ago
Hang on, that won't quite work right, will it? I need to convert the ASCII value in the char to an integer, like this:


Sorry about that!
DD
17 years ago
You said:


Example:

String "1234567"

I need to read "7" as an int and perform an operation
then I need to read "6" as an int
then "5"
then "4" etc....



You might try a for loop that repeatedly calls the charAt() method of String on the String you want to process. For example:

DD
17 years ago