473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Lowercase to Uppercase?

Hello, I am having a "loss of precision error" every time I try to convert a lower case to an upper case. We are not allowed to use the touppercase and tolowercase method in the program. Any advice on how to go about doing this without the error would be appreciated, thank you.

if (c >= 'a' && c <= 'z' )
{
x = c - 32;
}



c is the number read in
x was declared at the top of the program
Feb 17 '08 #1
9 3222
jx2
228 100+
we need to see more of your code a specialy how you declared your variables
i am quite sure tou need to cast the values(tell the compiler that you know what you trying to do
e.g. if you want to change from float to int
Expand|Select|Wrap|Line Numbers
  1.  
  2. int i = 0;
  3. float f = 4.54;
  4.  
  5. i = (int)(f);
  6.  
i hope that helps
jx2
Feb 17 '08 #2
public class P4
{
public static void main(String []args)
{
char x = ' ';
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sequence of characters.");
String a = input.nextLine();
char c = a.charAt(0);

if(c >= 'a' && c <= 'z')
{
x = (char) c - 34;
}

I tried adding the (char) next to the variable c(input) and it did not work. I am not used to working with chars and when I did have to convert a lower case to an uppercase I would just use the method toupper, ect.
Feb 17 '08 #3
jx2
228 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. x =  (char) c - 34;// remember about bracets what you trying to do in here 
  4. //is trying to convert char c into char.
  5. // c is a char !! 
  6. // and later you substracting 34
  7. // wich change it into number
  8.  
  9. //try something like this:
  10.  
  11. x = (char)(c-34);
  12.  
  13.  
should help
Feb 17 '08 #4
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. x =  (char) c - 34;// remember about bracets what you trying to do in here 
  4. //is trying to convert char c into char.
  5. // c is a char !! 
  6. // and later you substracting 34
  7. // wich change it into number
  8.  
  9. //try something like this:
  10.  
  11. x = (char)(c-34);
  12.  
  13.  
should help
worked like a charm, I wasnt aware that you could use it like that, appreciate your help.
thank you :D
Feb 17 '08 #5
BigDaddyLH
1,216 Expert 1GB
That 34 (or is it 32?) is mysterious. Why not write:
Expand|Select|Wrap|Line Numbers
  1. x = (char) (c - 'a' + 'A');
Feb 17 '08 #6
That 34 (or is it 32?) is mysterious. Why not write:
Expand|Select|Wrap|Line Numbers
  1. x = (char) (c - 'a' + 'A');
It was actually 32, i changed it in my program to see if i made a mistake in other char tests. Thanks for the check though :) just started to program again so i forget all the checks sometimes.
Feb 17 '08 #7
JosAH
11,448 Expert 8TB
It was actually 32, i changed it in my program to see if i made a mistake in other char tests. Thanks for the check though :) just started to program again so i forget all the checks sometimes.
It has nothing to do with 'checks'; everytime you see a 'magic' number in a piece
of code other than -1, 0 or 1 (and I feel tempted to spit on -1) throw the code away
or let any scabby cat pee on it and forget it afterwards.

Nowadays programming languages allow you to define those magic monstrosities
as either a (defined) constant or final value; there is no excuse anymore for silly
literal values other than -1, 0 and 1 (and even -1 is close to the edge).

kind regards,

Jos
Feb 17 '08 #8
BigDaddyLH
1,216 Expert 1GB
Nowadays programming languages allow you to define those magic monstrosities
as either a (defined) constant or final value; there is no excuse anymore for silly
literal values other than -1, 0 and 1 (and even -1 is close to the edge).
Once when I was teaching, I told a student that he could improve his code if he replaced 10 (which was just an arbitrary array size) with a constant. When he handed in his code, I saw that he had...
Expand|Select|Wrap|Line Numbers
  1. public static final int TEN = 10;
  2.  
Feb 18 '08 #9
JosAH
11,448 Expert 8TB
Once when I was teaching, I told a student that he could improve his code if he replaced 10 (which was just an arbitrary array size) with a constant. When he handed in his code, I saw that he had...
Expand|Select|Wrap|Line Numbers
  1. public static final int TEN = 10;
  2.  
:-) I once saw this:

Expand|Select|Wrap|Line Numbers
  1. public interface Constants {
  2.    int contant00= 42; // I don't remember their values
  3.    ... // and the list continued up to this:
  4.    int constant71= 54; // but I do remember there were 72 of them
  5. }
  6.  
Seventy two constants with meaningless names; some people deserve to have
their head nailed to the floor and covered in smelly parrot droppings.

kind regards,

Jos
Feb 18 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name...
30
by: Steve Edwards | last post by:
Hi, I'm re-writing some code that had relied on some platform/third-party dependent utility functions, as I want to make it more portable. Is there a standard C/C++/stl routine for changing an stl...
6
by: BSHELTON | last post by:
How do I convert existing lowercase data to uppercase in Access 2000? I used the following which did not work? UPDATE HousingTowns SET tMunis=UPPER(tMunis); "HousingTowns" is the table name....
2
by: evantri | last post by:
Hi everyone, I am required to write a standard C function to import the single character variable and return the lowercase version of the character int upper_to_lower ( char singlecharacter ) {...
2
by: t8ntboy | last post by:
I have a table the contains field called PersonID. Each record in the personID field begins with the letter "p" in uppercase or lowercase. I want to run a query that finds all of the lowercase...
14
by: fniles | last post by:
In VB.NET 2005 can I check if a letter in a string is upper case or lower case ? For example: I have the following 2 lines: NQ,Z2003,11/11/2003,1416.5,1420,1402,1411.5...
9
by: humaid | last post by:
hi guys,i have this problem.first let me show you the so far code #!/user/bin/perl while ($a = <STDIN>) { if ($a =~ tr/A-Z/a-z/) { print "$a\n"; }
1
by: gsreenathreddy | last post by:
Hi! <html> <head> <script type="text/javascript"> function upperCase() { var x=document.getElementById("fname").value; document.getElementById("fname").value=x.toUpperCase(); }
2
by: Bob Nelson | last post by:
Concerning program startup in a hosted environment, both C90 and C99 require that the implementation provide main's ``argv'' strings in lowercase if the host environment is not capable of supply...
1
by: peacock97 | last post by:
I have an access mailing list. I received it in UPPERCASE and have been able to change the 1st letter to UPPERCASE and the rest to lowercase except that in Proper Names like McDonald, McInnis, they...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.