Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

converting int to string

string

  • Please log in to reply
3 replies to this topic

#1 brothacode

brothacode

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:Java
  • Learning:Java

Posted 14 October 2012 - 09:39 AM

hi I am making a program that asks for your name and genarates a password but im having trouble at the last step of the program. How it works is you type in your name and then it will take the 3rd 13th and 19th letters in the string. if the string doesnt have 19 letters it will add the name to itself till it does have 19 or more letters. the 3rd 13th and 19th letters are then converted to their ascii values and then added into 1 string. i then want to convert each value to a letter but im having difficulties with doing so. i do not know how to change a number to a specific letter can anyone help me. the password should be created by taking all the chars in the string and then converting it to a letter like e.g. (1334 = ACCD and i know the ascii value for A is 65 but i want 1 to be A). here is code. Please help.




public String titlcase( String st )
	{

		List let = new ArrayList();
		for ( int i = 0; i < st.length(); i++ )
			{
				let.add(st.substring(i,(i+1)));
			}

		String str = "";
		String strlet = "";
		String strprev = " ";

		Iterator iter = let.iterator();
		while ( iter.hasNext() )
		{
			strlet = ( String ) iter.next();

			if ( strprev.equals(" ") == true )
			{

				str += strlet.toUpperCase();
			}
			else
			{

			str += strlet;
			}
			strprev = strlet;
		}
		System.out.println(str);

		String jo1 = str;

		int len = jo1.length();

		if(len == 1)
		{
			jo1 = jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(2);
			int som1 = jo1.charAt(12);
			int som2 = jo1.charAt(18);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}
		else if(len == 2)
		{
			jo1 = jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}
		else if(len == 3)
		{
			jo1 = jo1 + jo1 + jo1 + jo1 + jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}
		else if(len == 4)
		{
			jo1 = jo1 + jo1 + jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}
		else if(len == 5||len == 6)
		{
			jo1 = jo1 + jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}		
		else if(len == 7||len == 8||len == 9)
		{
			jo1 = jo1 + jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}	  
		else if(len == 10||len == 11||len == 12||len == 13||len == 14||len == 15||len == 16||len == 17||len == 18)
		{
			jo1 = jo1 + jo1;
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}
		else
		{
			System.out.println(jo1);
			int len2 = jo1.length();
			System.out.println(len2);

			int som = jo1.charAt(3);
			int som1 = jo1.charAt(13);
			int som2 = jo1.charAt(19);
			System.out.println(som + " " + som1 + " " + som2);

			String sub = ""+som;
			String sub1 = ""+som1;
			String sub2 = ""+som2;
			String subf = sub + sub1 + sub2;
			System.out.println(sub + " " + sub1 + " " + sub2 + " \n" + subf);

		}

		return str;
	}  



	public static void main(String[] args) {
		String jo = JOptionPane.showInputDialog("Enter your name in lowercase: ");
		Question1 q1 = new Question1();
		q1.titlcase(""+jo);

	}

here is example of the output:

name: ace stone
Ace StoneAce StoneAce Stone
32 83 99
328399
password here: (CBHCII)

Edited by BenW, 16 October 2012 - 12:06 AM.
Added code tags

  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 14 October 2012 - 09:55 AM

int number = Character.getNumericValue( {yourchar});

this is some getnumericvalue doc

or you can simple subtract 65 from the value (for uppercase) you will see lowercase gives a bigger number, oh, good news is that the ansi alphabet is correlative, so A = 65, B=66 and so on
  • 0

#3 brothacode

brothacode

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:Java
  • Learning:Java

Posted 14 October 2012 - 10:19 AM

i dont understand how the int number = Character.getNumericValue({yourchar}) thing works say i have a int value of 3 and i want that to be C and i have a value 4 that is D bassicaly from 1 to 9 = from A to I nothin more nothing less. now i have a string containing the value 3614 then i want that string to be converted to CFAD (3=C,6=F,1=A,4=D). thats kinda what im going for and if there is a 0 in the string it should just ignore that.
  • 0

#4 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 15 October 2012 - 02:30 PM

You could create a separate array with all the letters in indexes that are relative to their desired position, for example, if you wanted 4 to be D, then it could look like this.
...
final String[] LETTERS = {"A","B","C","D"...};
public String getStringForInt(int i){
   return LETTERS[i];
}
//Or to get it based on an int value in a string
public String replaceWithLetters(String in){
   for(int i = 0; i < LETTERS.length; i++){
	  in = in.replace(i.toString(), LETTERS[i]);
   }
   return in;
}
...

  • 0
Speaks fluent Java





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download