|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Help with a Java program
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||||
|
Photography nut
![]() |
OK well I'm writing this program for school. Its one that a user enters a class name then enters the grades, then finds the total for each letter grade then the total grades entered , and then does all that again and compares the two grade histograms to see if they are equal.
The Prof. already gave us the javadoc for the class. My first problem is in one of the methods, trying to convert a instance variable of type char to uppercase, but the string class method .toUpperCase does not work(since its type char). So is there another way to do this? Like with type casting or the Wrapper classes? Thanks for any help.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||
|
Programmer
|
Quote:
public char convert(char lowerChar) { String temp = "" + lowerChar; temp = temp.toUpperCase(); return temp.charAt(0); } OR public char convert(char lowerChar) { lowerChar -= 32; return lowerChar; } Since the difference b/w ascii 'a' and ascii 'A' is 32. hope it helps.
__________________
Relax and enjoy life!
Last edited by mjoc13 : 09-05-06 at 10:13 AM. |
||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
Thank you for the help though. I just want to keep the instance variable as type char and not mess with a temporary string for it.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
if you want it in one line
yourchar = (((new String(yourchar)).toUpperCase()).toCharArray())[0]; or yourchar = ((new String(yourchar)).toUppercase()).charAt(0);
Last edited by rabidgnome229 : 09-05-06 at 02:36 PM. |
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||
|
Programmer
|
char grade = 'a';
... work... grade -= 32; //convert to upperCase If you know your always dealing with lowercase then just subtract 32 from the char and the ascii will be upercase. No nested if's or type casting needed.
__________________
Relax and enjoy life!
|
|||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
that only works if you know you have a lowercase letter. if you start with uppercase that method will mess the data up
|
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Photography nut
![]() |
its a mixer of upper and lower case letters. I just wanted them all in uppercase to simplify the if statement.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#8 (permalink) | ||||||||||||||
|
Photography nut
![]() |
well I've ran into yet another problem. Seems like over the summer I've really forgotten some stuff.
Problem is I've got an infinite loop. Code:
public String toString()
{
String temp = "A ";
int star = 0, largest = 0;
for(int i=0; i<gradeArray.length; i++)
if(largest < gradeArray[i])
largest = gradeArray[i];
for(int s=0; s < gradeArray.length; s++)
{
while(star != this.gradeArray[s])
{
temp = temp + "*";
star++;
}
while(star < largest)
temp = temp + " ";
temp =temp + " " + gradeArray[s] + "\n";
if(s == 0)
temp = temp + "B ";
else if(s == 1)
temp = temp + "C ";
else if (s == 2)
temp = temp + "D ";
else if(s == 3)
temp = temp + "F ";
star = 0;
}
temp = temp + "\n" + getTotalGrades();
return temp;
}
public int getTotalGrades()
{
int total=0;
for(int i=0;i < gradeArray.length; i++)
total = total + gradeArray[i];
return total;
}
The first method is just to print out the results like so: (say gradeArray[0] = 2 & gradeArray[1] = 1) A ** 2 B * 1 etc. can anyone spot the problem? Thanks
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
Last edited by dangerousHobo : 09-05-06 at 09:11 PM. |
||||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Code:
while(star < largest) temp = temp + " ";
|
|||||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
Thank you have much rabidgnome.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|