13 Replies - 1091 Views - Last Post: 23 January 2011 - 05:37 AM Rate Topic: -----

#1 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

JAVA program accepts ten (10) words and be converted into numbers

Posted 16 January 2011 - 05:55 AM

MERC is an encryption leading company for more than 10 years. Assume that you are the programmer of the company and you were asked to lead the development of one project. They are currently developing an encryption program accepts ten (10) words and be converted into numbers.

can you give me tips to answer this because i've researched alot but still i'm not doing well.

heres my last produced code.

import javax.swing.*;
public class sample{
	   public static void main(String[] args)
	   {
		   CharSequence A="A", B="B",C="C",D="D",E="E",F="F",G="G",H="H",I="I",J="J",K="K",L="L",
		   M="M",N="N",O="O",P="P",Q="Q",R="R",S="S",T="T",U="U",V="V",W="W",X="X",Y="Y",Z="Z";
		  String AA="AA",YY="YY",BB="BB",DD="DD",EE="EE",XX="XX",WW="WW",CC="CC",UU="UU",VV="VV";
		   String asa = "";

		       String letter = JOptionPane.showInputDialog(null,"Enter value: ");
		       
		      
		       if(letter.contains(A))
		       {asa = asa+" 1 ";}
		       if(letter.contains(B)/>)
		       {asa = asa+" 2 ";}
		       if(letter.contains(C))
		       {asa = asa+" 3 ";}
		       if(letter.contains(D))
		       {asa = asa+" 4 ";}
		       if(letter.contains(E))
		       {asa = asa+" 5 ";}
		       if(letter.contains(F))
		       {asa = asa+" 6 ";}
		       if(letter.contains(G))
		       {asa = asa+" 7 ";}
		       if(letter.contains(H))
		       {asa = asa+" 8 ";}
		       if(letter.contains(I))
		       {asa = asa+" 9 ";}
		       if(letter.contains(J))
		       {asa = asa+" 10 ";}
		       if(letter.contains(K))
		       {asa = asa+" 11 ";}
		       if(letter.contains(L))
		       {asa = asa+" 12 ";}
		       if(letter.contains(M))
		       {asa = asa+" 13 ";}
		       if(letter.contains(N))
		       {asa = asa+" 14 ";}
		       if(letter.contains(O))
		       {asa = asa+" 15 ";}
		       if(letter.contains(P))
		       {asa = asa+" 16 ";}
		       if(letter.contains(Q))
		       {asa = asa+" 17 ";}
		       if(letter.contains(R))
		       {asa = asa+" 18 ";}
		       if(letter.contains(S))
		       {asa = asa+" 19 ";}
		       if(letter.contains(T))
		       {asa = asa+" 20 ";}
		       if(letter.contains(U))
		       {asa = asa+" 21 ";}		       
		       if(letter.contains(V))
		       {asa = asa+" 22 ";}		      
		       if(letter.contains(W))
		       {asa = asa+" 23 ";}		      
		       if(letter.contains(X))
		       {asa = asa+" 24 ";}	       
		       if(letter.contains(Y))
		       {asa = asa+" 25 ";}		       
		       if(letter.contains(Z))
		       {asa = asa+" 26 ";}
		       
		       
		       
		      
		      
		       
		      
		      
		       JOptionPane.showMessageDialog(null,asa);
		       
	   }}





but when i input AA i the value does not repeat . it only print one time.

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 16 January 2011 - 06:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: JAVA program accepts ten (10) words and be converted into numbers

#2 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 16 January 2011 - 07:43 AM

i am looking for logics or codes that will determine each letter in a string that is inputed and prints the value of each of the letter.

thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 16 January 2011 - 07:57 AM

You can use the String.charAt method
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12179
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 16 January 2011 - 03:40 PM

In combination with a for loop. Treat the String like an array, essentially.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 16 January 2011 - 07:58 PM

This is really one of the most HORRIBLE code ever posted in this forum
rethink your design
Was This Post Helpful? 1
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 17 January 2011 - 02:40 PM

Quote

This is really one of the most HORRIBLE code ever posted in this forum
rethink your design


Agreed. :^:
Just to explain what causing the problem, the reason you treat "AA" as "A" is because you first check if the input contains "A".
Now, if an input "AA" is inserted, of course, it contains "A", so the first if condition executes, and the program ignores the rest of the if statements.
To solve such problem in the future, check the longer Strings first, like:
if(letter.contains("AA")){
 //do something
}
else if(letter.contains("A")){
  //do something
}


The reson I say "in the future", is because, as pbl said, you will have to redesign your code.

This post has been edited by japanir: 17 January 2011 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 06:00 AM

but how about detecting a double letter in a string.
i need to detect the double adjacent letter and prints its given value.
for example i inputed AAA in a string.
the out put must be the value of AA and A.
Was This Post Helpful? 0
  • +
  • -

#8 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 06:03 AM

how can a logic detect a double letter in a string.
i need to detect the double adjacent letter and prints its given value.
for example i inputed AAA in a string.
the output must be the value of AA and A.
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 06:03 AM

Why post a new thread? you have already opened a thread about the same issue.
http://www.dreaminco...d-into-numbers/
Was This Post Helpful? 0
  • +
  • -

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 06:06 AM

That is the third time you open the same thread.
avoid doing it :angry2:
That is really confusing, and make the job of helping you much harder.
refer to your original post, and post the updated code you have.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12179
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 07:23 AM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#12 glandrum101  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 13-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 18 January 2011 - 07:42 AM

You should start off by deleting all the code you have because the structure of your code is about as bad as it can be. After you do that, take a look at this method within the String class: http://download.orac...va.lang.String)

You can use that to search for specific substrings in a string. A value of -1 returns if the specified substring is not found or the value of the index that the substring first occurs is returned. As someone mentioned above do not forget to search for your longer strings first i.e AA before A and AAA before AA. You do not have to do it this way, but that is the most efficient method that I can think of off the top of my head.
Was This Post Helpful? 0
  • +
  • -

#13 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 23 January 2011 - 04:35 AM

i really dont get it can you give an example ?
Was This Post Helpful? 0
  • +
  • -

#14 jorelkim.cruz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-January 11

Re: JAVA program accepts ten (10) words and be converted into numbers

Posted 23 January 2011 - 05:37 AM

import javax.swing.*;
public class sample{
public static void main(String[]args)
{String s="";
		for(int ca=1; ca<=10; ca++)
		{String str = JOptionPane.showInputDialog(null,"Enter value "+ca+" ");
			for ( int i = 0; i < str.length(); ++i ){
		if(str.isEmpty()){JOptionPane.showMessageDialog(null, "no value entered");}
		else if(Character.isUpperCase(str.charAt(i)))
		{ 
		      char c = str.charAt( i );
		       int j = (int) c;
		       s=s+(j-64)+"   ";
		}
		else
		{JOptionPane.showMessageDialog(null,"Invalid input!\n you can only input capital letters");}
		}}
			
	JOptionPane.showMessageDialog(null,""+s);
	
	
		
}
}

heres my new code. the only problem is to determine the double ajacent letters how can i do that ?





Was This Post Helpful? 0
  • +
  • -

Page 1 of 1