GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Apr-2008, 11:42
mamz mamz is offline
New Member
 
Join Date: Jan 2008
Posts: 12
mamz is on a distinguished road
Exclamation

map int values for letters


hi,
can any one pls help me on this. I wanna assign values 1 to 26 for char values taken as user inputs.
  #2  
Old 15-Apr-2008, 20:35
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 935
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: map int values for letters


One potential way, is to create a string of the alphabet, and index their position:
JAVA Code:
import java.io.*;

public class Alpha
{
   final static String alphabet = "abcdefghijklmnopqrstuvwxyz";
	
   public static void main(String[] args)
   {
      System.out.println("Simply input a hyphen to quit.");
      Alpha a = new Alpha();		
   }

   public Alpha()
   {
	char b = ' ';
        BufferedReader keybd = 
                new BufferedReader(new InputStreamReader(System.in));

	do
	{
	   try
	   {
		b = (char)keybd.read();
				
		/* skip the enter key */
		if ( b == '\n' )
		{
			continue;
		}
		else if ( Character.toLowerCase(b) >= 'a' && 
		          Character.toLowerCase(b) <= 'z') 
		{
		   int position = alphabet.indexOf( String.valueOf(b).toLowerCase() );
		   System.out.println("'" + b + "', is at index: " + (position + 1) );
		}
		else
		{
		   System.out.println("Input '" + b + "' is NOT in the alphabet.");
		}
	   }
	   catch (IOException ioe)
	   {
	      ioe.toString();
	   }
	}
	while( b != '-' );
		
	System.out.println("Bye!");	
   }	
}
...and sample interaction:
Code:
r 'r', is at index: 18 X 'X', is at index: 24 z 'z', is at index: 26 ( Input '(' is NOT in the alphabet. $ Input '$' is NOT in the alphabet. - Input '-' is NOT in the alphabet. Bye!
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB 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
Please help me to convert a simple C++ program into an object-oriented one. zekesteer CPP / C++ Forum 5 05-Jan-2008 10:05
Need Help with input files. Efferus CPP / C++ Forum 2 24-Nov-2007 16:19
GetwindowRect() function returns different values in different OS. Sujith MS Visual C++ / MFC Forum 0 15-Jan-2007 22:55
Inpage values in code behind ajitham .NET Forum 0 25-Jan-2006 05:25
paasing values in fk_t_diectories zuzupus MySQL / PHP Forum 6 12-Aug-2003 07:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.