Results 1 to 15 of 15
Thread: Translation/Decoding Program
- 05-04-2009, 05:57 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Translation/Decoding Program
Hey Guys i am a Javanewb and i am trying to write a small program for a friend of mine.
Background: My friend is comunicating with someone in greece per e-mail. Problem here is that he does not get the right letters in his answers. For example the greek η is shown as a ÿ in his mails.I want to write him a little program where he can paste his mail and let the program translate the mail letter by letter.
Problem: I have no clue how to do it :-D.My idea was to create a huge array and write every letter in, then translate the letters by saying ÿ = α; and post them on the screen! (I am using a GUI because he is not able to use commandline, i was unsure if this is the right section. I think my question fits here more then in the Swing section)
Any ideas how to solve that, i guess that creating an 1k array is not the best way!
thanks for reading
My code:
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Translate.java * * Created on 04.05.2009, 16:55:44 */ package my.Translate; /** * * @author wyldstyle */ public class Translate extends javax.swing.JFrame { /** Creates new form Translate */ public Translate() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField3 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(java.awt.Color.orange); jPanel1.setBackground(new java.awt.Color(4, 50, 108)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "HowTo: Oben Text einfügen und umwandeln", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Salat:"); jLabel3.setFont(new java.awt.Font("Comic Sans MS", 3, 12)); // NOI18N jLabel3.setForeground(new java.awt.Color(255, 255, 255)); jLabel3.setText("Original:"); jTextField1.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N jTextField1.setText("jTextField1"); jTextField3.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N jTextField3.setText("jTextField3"); jButton1.setText("Übersetzen"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Löschen"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton4.setText("Exit"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jButton1) .addGap(18, 18, 18) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE) .addComponent(jButton4)) .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton4)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE) .addComponent(jLabel3) .addGap(128, 128, 128)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> [B]private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String num1, result; num1 = (jTextField1.getText()); num2 = sdsd; result = num2; jTextField3.setText(String.valueOf(result)); [/B] } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(""); jTextField3.setText("");// TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Numberaddition().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField3; // End of variables declaration }Last edited by wyldstyle; 05-04-2009 at 06:05 PM.
- 05-04-2009, 07:01 PM #2
don't use an array. You can have him paste the email into a text file and use a FileInputStream to read the lines and convert them one line at a time and then FileOutputWriter to write them to a seperate text file that he can open up. You can actually have all this within your Swing application with an InnerFrame(I think that's what it's called).
Essentially you have your Application with a input button, translate button.
Input button opens/embeds a blank text document in the application that he pastes the email into.
Translate button will read one line at a time from that file, run your translation algorithm, print translated line into new text file. After it's all finished translating it opens the new text file beside the old one.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-04-2009, 08:00 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
The Program as it is works just perfectly ...i am just too stupid to implement the main function of it.
How do i "split" the incoming string into chars, change them (a->x for example) and then make them to a string together so i can give them out on the screen?
- 05-04-2009, 09:28 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
It still does not transformate any sign into another -.-Java Code:String result; String eingabe; char a = 0,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,α = 0,β; eingabe = (jTextArea1.getText()); char[] myCharArray = eingabe.toCharArray(); b = α; myCharArray[a] =myCharArray[α]; // myCharArray[b] =myCharArray[β]; String A = new String(myCharArray); result = A; jTextArea2.setText(String.valueOf(result));
- 05-04-2009, 10:31 PM #5
why not use the String.replace(old char, new char) method?
String translated = new String(eingabe.replace(b,β))Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 09:33 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
I still get the same output using your codefragment, input=output there is no transformation.
Code looks this way now:
There is in Error in line " String trans = new String(eingabe.replace(b,β));", it says the variables b,β need to be inititialized.If i do this...the output is = input, otherwise program wont compile.Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // First we define float variables. String result; String eingabe; char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,α,β; eingabe = (jTextArea1.getText()); //char[] myCharArray = new char [1000]; //myCharArray = eingabe.toCharArray(); String trans = new String(eingabe.replace(b,β)); // trans = result; jTextArea2.setText(String.valueOf(trans)); // TODO add your handling code here: }
As i said i am beginner :-)! Thanks a lot for your help mate i really appreciate it!
- 05-05-2009, 05:43 PM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Your line starting "char ..." is just declaring a load of variables with these names, but they'll all have the value zero (since you've not set anything else). Your replace() call does nothing because your string presumably doesn't contain the value zero.
You'd need to do eingabe.replace('a', 'β'); etc.Neil Coffey
Javamex - Java tutorials and performance info
- 05-05-2009, 06:26 PM #8
what Neil said. I can't believe I didn't catch my mistake when I gave you the code lol.
If you really wanted to use the character variables you would have to set them all up like a = 'a', b = 'b'.....
However it seems pointless because you can just use 'b' when doing comparisons and changes. You don't need the variables.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 08:12 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Ok Guys thanks a lot i can finally translate with it a bit.. i have just one final question:
How do i add more then just one "translation" to this?Java Code:String trans = new String(eingabe.replace('b','β'));
Pseudo: eingabe.replace(a->α,b->β,c->γ);
Actually i can just translate one sign of the whole string :)!
- 05-05-2009, 08:33 PM #10
i'm not sure what you mean by translate oen sign of the string but you can just keep translating the current string
String trans = new String(eingave.replace('a','b'));
trans = new String(trans.replace('b','c'))...etcLiberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 08:38 PM #11
If you want to get really fancy you can have two arrays of translate values
Char[] english = {'a','b','c','d'....};
Char[] greek = {'A','B','C'.....};
making sure each spot in english has its translated character at the same spot in greek and then do a loop around your replace code.
for(int ii = 0; ii < greek.length; greek++){
eingabe= new String(eingabe.replace(english[ii],greek[ii]));
}
but whatever works works I guess.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 08:51 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Oh yeah, the first one is working perfectly thus far. I guess my teachers would use the second suggestion since the first seems to be "bad code" but i do not care, it works perfetly and it runs very fast. Now i just need to get the translationtable from my friend, then i will add the finalcode here so you can try out your work if you like *gg*!
Thanks a lot for this i find it a bit hard to find solutions in java...i often have the solution in front of my eyes but i cannot find it!
//edit: I will use the time untill he answers to find out more about how to make this program and applet!
- 05-05-2009, 08:56 PM #13
applet? I believe you mean application. An applet is something that will show up in a web browser when you run it. You can bundle your code into a jar file when you're finished with it and your friend can run it as an application from there whenever they want to use it. (However I'm no help with that, I've never bundled anything into a jar file, but there are plenty of examples throughout the fora)
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-05-2009, 09:02 PM #14
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
Actually i really meant applet, i have a little webspace and if i host it he can access it from everywhere.
- 05-05-2009, 09:17 PM #15
ahh that works. Well good luck on your project. Come back if you need more help. Plenty of knowledgeable folks here willing to assist.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads
-
Rotation & translation combined
By ScottVal in forum Java 2DReplies: 4Last Post: 02-27-2011, 06:57 PM -
X / Y Translation Problem
By SkinnyK in forum New To JavaReplies: 4Last Post: 01-16-2009, 12:20 AM -
Transform Translation and Rotation
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 10:58 PM -
Transform Rotation Translation
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:53 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks