Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2011, 4:44 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 189
Rep Power: 0 357mag is an unknown quantity at this point
How to input a character

I'm trying to write a program that will input three characters and convert them to the opposite case. I'm first starting with a reduced, simplifed form just working with inputting one character. Too bad Java does not have a nextChar() method for inputting characters. I think what Herb wants me to do is use the System.in read() method for doing it and I'm okay with that.

But I'm curious as to whether there is some way to get a character from the keyboard and just cast it to an integer. But I've tried various ways and it always says "Input Mismatch Exception."

Here is what I'm working with so far:
import java.util.Scanner;

public class ConvertCase
{
	public static void main(String args[])
	{
		char ch1;
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("\nEnter the first character: ");
		ch1 = (char)input.nextInt();
		
		ch1 += 32;
		
		System.out.println("After conversion: " + ch1);
	}
}

I thought you could treat characters as integers. There is some relationship there.
357mag is offline   Reply With Quote
Old Jun 28th, 2011, 5:03 PM   #2
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,326
Rep Power: 11 sixstringartist will become famous soon enough
Re: How to input a character

You can get a single character by using Scanner to read in a string and index into the elements of that string:

Scanner tScan = new Scanner(System.in);
String tBuffer = tScan.next();
char oneChar = tBuffer.charAt(0);

or you can use the System.in.read() method, but I dont really recommend that.
try {
int tmp = System.in.read ();
  char c = (char) tmp;
}
catch (IOException e) {}

I recommend just using strings and manipulating the strings for something like this.
sixstringartist is offline   Reply With Quote
Old Jun 28th, 2011, 5:55 PM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 189
Rep Power: 0 357mag is an unknown quantity at this point
Re: How to input a character

Well I'm gonna not use strings since I have not studied them yet. Here is what I got now:
public class ConvertCase
{
	public static void main(String args[])
		throws java.io.IOException
	{
		char ch1;
		
		System.out.print("\nEnter the first character: ");
		ch1 = (char)System.in.read();
		
		if(ch1 >= 'A' && ch1 <= 'Z')
			ch1 += 32;
			
		if(ch1 >= 'a' && ch1 <= 'z')
			ch1 -= 32;	
		
		System.out.println("After conversion: " + ch1);
	}
}
But something is wrong cuz if I enter a capital letter it spews out the same capital letter. I suspect the value 32 is never being added to the character. I'm looking at my if statement thinking that perhaps the way I have it written it's evaluating to false and if that happens the statement in the body never runs.

But I don't see a problem with the way I have it written.
357mag is offline   Reply With Quote
Old Jun 28th, 2011, 5:58 PM   #4
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 189
Rep Power: 0 357mag is an unknown quantity at this point
Re: How to input a character

Ah...I discovered a bug. The second if statement simply undoes what I accomplished in the first if statement. That's where the bug is.
357mag is offline   Reply With Quote
Old Jun 28th, 2011, 6:27 PM   #5
sixstringartist
Programming Guru

 
Join Date: Jun 2005
Posts: 1,326
Rep Power: 11 sixstringartist will become famous soon enough
Re: How to input a character

What you logically want there is an if{} elseif{} set of statements, not two if's. Alternatively you can assign the result to a new variable rather than overwriting the input.
sixstringartist is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
masking console input with a character pythonscript Python 4 Dec 31st, 2009 10:06 PM
Having trouble creating a function that treats string input as c++ character literals. lrh9 C++ 6 Jun 1st, 2009 11:29 PM
Reading character input into an array (raw mode) shoeyfighter C 3 Nov 2nd, 2006 3:49 PM
How to suppress character input aznluvsmc C 20 Nov 17th, 2005 8:59 AM
Determining whether input from scanf() is an integer or character? bivhitscar C 10 Nov 1st, 2005 12:32 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC