Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How can I modify a text so that it doesn't contain two equal numbers?

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Rome
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I modify a text so that it doesn't contain two equal numbers?

    I would like to create a program that takes some files and modifies them in this way:
    The files should contain text formatted in this way:

    ##############
    #various comments#
    ##############
    something{
        identifier=1234
        anotherIdentifier=1235
        anotherOne=12345
        //and so on...
    }//I need only this
     
    #Comments are sometimes made this way
     
    somethingAgain{
     
        #comments that explains what's below them
        I:dentifier:something, I, do, not, need
     
        #see ^that
        A:notherIdentifier:boolean
     
        //and so on..
    }

    and I have to make so that the numbers contained in something{} in all the files don't match.

    I could ask to make so that the input file is only one, and it is formatted this way:

    identifier=1234
    anotherIdentifier=1235
    anotherOne=12345
    //and so on...

    but I don't know how to do the rest of the program...
    That's what I've done (the names of the classes, package etc. are in Italian and there's some useless code that NetBeans prevents me from deleting):

    package confronto;
     
    import java.awt.Color;
    import java.io.*;
    import javax.swing.*;
     
    /**
     *
     * @author Oxen Eknat
     */
    public class confrontoTesti extends javax.swing.JFrame {
     
        /**
         * Creates new form confrontoTesti
         */
        public confrontoTesti() {
            initComponents();
        }
     
        /**
         *
         * @param percorsoIniziale is the string that contains the folder of the unedited file
         * @param percorsoFinale is the string that contains the folder where the edited file will be put in
         */
        public static void modificaTesto (String percorsoIniziale, String percorsoFinale){
            try{
                FileReader f= new FileReader(percorsoIniziale);
                String a=f.toString();
                int l = a.length();
                for (int i=0; i<l; i++){
                    char c1 = a.charAt(i);
                    String n = "";
                    if (c1=='1'|c1=='2'|c1=='3'|c1=='4'|c1=='5'|c1=='6'|c1=='7'|c1=='8'|c1=='9'|c1=='0'){
                        do{
                        n += c1;
                        i++;
                        c1 = a.charAt(i);
                        } while (c1=='1'|c1=='2'|c1=='3'|c1=='4'|c1=='5'|c1=='6'|c1=='7'|c1=='8'|c1=='9'|c1=='0');
                    }
                    int n1 = Integer.getInteger(n);
                    try{
                        try (PrintWriter writer = new PrintWriter(percorsoFinale, "UTF-8")) {
                            writer.println(n1);
                        }
                    }
                    catch(FileNotFoundException | UnsupportedEncodingException errore1){
                        JOptionPane.showMessageDialog(null,"Errore imprevisto! Errore:" + errore1 ,"ERRORE!",JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
            catch(FileNotFoundException errore){
                JOptionPane.showMessageDialog(null,"Errore imprevisto! Errore:" + errore ,"ERRORE!",JOptionPane.ERROR_MESSAGE);
            }
        }
     
        /**
         * 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() {
     
            jTextField2 = new javax.swing.JTextField();
            jTextField1 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jTextField3 = new javax.swing.JTextField();
            jButton2 = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jTextField2.setEditable(false);
            jTextField2.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
            jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
            jTextField2.setText("CONFRONTO");
            jTextField2.setBorder(null);
            jTextField2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            jTextField2.setFocusable(false);
            jTextField2.setHighlighter(null);
            jTextField2.setOpaque(false);
            jTextField2.setSelectedTextColor(new java.awt.Color(0, 0, 0));
            jTextField2.setSelectionColor(new java.awt.Color(240, 240, 240));
     
            jTextField1.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
            jTextField1.setText("percorso del file");
            jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
            jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jTextField1MouseClicked(evt);
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    jTextField1MouseEntered(evt);
                }
            });
            jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    jTextField1KeyPressed(evt);
                }
            });
     
            jButton1.setText("Verifica!");
            jButton1.setToolTipText("");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
     
            jTextField3.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
            jTextField3.setText("percorso del nuovo file");
            jTextField3.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
            jTextField3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jTextField3MouseClicked(evt);
                }
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    jTextField3MouseEntered(evt);
                }
            });
            jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    jTextField3KeyPressed(evt);
                }
            });
     
            jButton2.setText("Crea il nuovo file!");
            jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton2MouseClicked(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addComponent(jTextField1)
                        .addComponent(jTextField3))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(65, 65, 65))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {                                         
            jTextField1.setFont(new java.awt.Font("Tahoma", 0, 11)); 
            jTextField1.setForeground(Color.black);
            jTextField1.setText("");
        }                                        
     
        private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
        }                                      
     
        private void jTextField1MouseEntered(java.awt.event.MouseEvent evt) {                                         
        }                                        
     
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
            String percorso;
            percorso=jTextField1.getText();
            try {
                FileReader f= new FileReader(percorso);
            }
            catch (FileNotFoundException errore){
                jTextField1.setFont(new java.awt.Font("Tahoma", 2, 11));
                jTextField1.setForeground(Color.red);
                jTextField1.setText("Errore: percorso non valido");
            }
        }                                     
     
        private void jTextField3MouseClicked(java.awt.event.MouseEvent evt) {                                         
            jTextField3.setFont(new java.awt.Font("Tahoma", 0, 11)); 
            jTextField3.setForeground(Color.black);
            jTextField3.setText("");
        }                                        
     
        private void jTextField3MouseEntered(java.awt.event.MouseEvent evt) {                                         
            // TODO add your handling code here:
        }                                        
     
        private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {                                       
            // TODO add your handling code here:
        }                                      
     
        private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
            String percorsoIniziale=jTextField1.getText();
            String percorsoFinale=jTextField3.getText();
            modificaTesto (percorsoIniziale, percorsoFinale);
        }                                     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(confrontoTesti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(confrontoTesti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(confrontoTesti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(confrontoTesti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new confrontoTesti().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        // End of variables declaration                   
    }
    Please forgive any grammatical error, I'm an Italian student and I'm still studying English.
    Using NetBeans IDE 8.0 (only because I can't make Eclipse work on my old laptop)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I modify a text so that it doesn't contain two equal numbers?

    For the first version of the program I would not use any GUI. Write a console program that reads the file, parses the lines and saves the numeric parts for comparison against future lines. Save each line in a list that will be used to write out the updated version of the file. When a match is found, change the number part of the line before saving it in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    Rome
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I modify a text so that it doesn't contain two equal numbers?

    Thanks, I'll try this as soon as I can (now it's kinda late, 23:45 :P)
    Please forgive any grammatical error, I'm an Italian student and I'm still studying English.
    Using NetBeans IDE 8.0 (only because I can't make Eclipse work on my old laptop)

Similar Threads

  1. Output doesn't display numbers
    By Sylis in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2012, 11:51 PM
  2. read and modify a text file
    By Ayda Alhosany in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 26th, 2012, 12:00 AM
  3. string.equals(anotherString) Anything like this for doesn't equal?
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2011, 06:50 PM
  4. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 05:30 AM
  5. Replies: 2
    Last Post: February 25th, 2010, 04:17 PM