Skip to main content

roblem Parsing String

2 replies [Last post]
spartan563
Offline
Joined: 2008-03-04
Points: 0

Hey, just having a problem parsing a string to an Integer, maybe one of you can help.
I am using Netbeans 6.0 and this is done using a jFrame.

</p>
<p>package PGC;</p>
<p>import java.awt.event.*;</p>
<p>//Author: SPARTAN-563<br />
import java.util.Vector;<br />
import javax.swing.JList;<br />
public class GUI extends javax.swing.JFrame {<br />
        boolean prime = false;<br />
        int checker;<br />
        int generator;<br />
        int number = 5;<br />
        int i = 1;<br />
        boolean flag = false;<br />
        int count = 2;<br />
        int oldnumber;</p>
<p>    public GUI() {<br />
        initComponents();</p>
<p>    }</p>
<p>    public void parsetext()<br />
    {<br />
        int Checker = Integer.parseInt(jTextField2.getText());<br />
        checker = Checker;<br />
        int Generator = Integer.parseInt(jTextField1.getText());<br />
        generator = Generator;<br />
    }</p>
<p>   public void isprime()<br />
   {<br />
       parsetext();<br />
       if(checker == 2||checker == 3||checker ==5)<br />
       {<br />
           jLabel5.setText(checker + " is Prime");<br />
       }<br />
       else if(checker%2 !=2 || checker %3 !=0 || checker%5 !=0)<br />
       {<br />
           jLabel5.setText(checker + " is Prime");<br />
       }<br />
       else<br />
       {<br />
           jLabel5.setText("Function not /n yet implimented");<br />
       }<br />
   }</p>
<p>   public void editList()<br />
   {<br />
       Vector data = new Vector();<br />
       data.add(oldnumber);<br />
       JList jList1 = new JList(data);<br />
   }</p>
<p>public void primegen()<br />
{<br />
parsetext();      </p>
<p>while(true)<br />
{<br />
		if(number%2!=0)<br />
		{<br />
			for(i=3;i<br />
    private void initComponents() {</p>
<p>        jLabel1 = new javax.swing.JLabel();<br />
        jLabel2 = new javax.swing.JLabel();<br />
        jTextField1 = new javax.swing.JTextField();<br />
        jButton1 = new javax.swing.JButton();<br />
        jScrollPane1 = new javax.swing.JScrollPane();<br />
        jList1 = new javax.swing.JList();<br />
        jLabel3 = new javax.swing.JLabel();<br />
        jTextField2 = new javax.swing.JTextField();<br />
        jButton2 = new javax.swing.JButton();<br />
        jLabel4 = new javax.swing.JLabel();<br />
        jLabel5 = new javax.swing.JLabel();</p>
<p>        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);</p>
<p>        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));<br />
        jLabel1.setText("Prime Number");</p>
<p>        jLabel2.setText("Generator");</p>
<p>        jButton1.setText("Generate");<br />
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {<br />
            public void mouseClicked(java.awt.event.MouseEvent evt) {<br />
                jButton1MouseClicked(evt);<br />
            }<br />
        });</p>
<p>        jList1.setModel(new javax.swing.AbstractListModel() {<br />
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };<br />
            public int getSize() { return strings.length; }<br />
            public Object getElementAt(int i) { return strings[i]; }<br />
        });<br />
        jScrollPane1.setViewportView(jList1);</p>
<p>        jLabel3.setText("Checker");</p>
<p>        jButton2.setText("Check");<br />
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {<br />
            public void mouseClicked(java.awt.event.MouseEvent evt) {<br />
                jButton2MouseClicked(evt);<br />
            }<br />
        });</p>
<p>        jLabel4.setText("Output:");</p>
<p>        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());<br />
        getContentPane().setLayout(layout);<br />
        layout.setHorizontalGroup(<br />
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
            .addGroup(layout.createSequentialGroup()<br />
                .addContainerGap()<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                    .addComponent(jLabel1)<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                            .addComponent(jLabel2)<br />
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))<br />
                        .addGap(42, 42, 42)<br />
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)<br />
                                .addComponent(jLabel4)<br />
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)<br />
                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)<br />
                                .addComponent(jLabel3)))))<br />
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))<br />
        );</p>
<p>        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jTextField1, jTextField2});</p>
<p>        layout.setVerticalGroup(<br />
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
            .addGroup(layout.createSequentialGroup()<br />
                .addComponent(jLabel1)<br />
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                    .addComponent(jLabel2)<br />
                    .addComponent(jLabel3))<br />
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br />
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))<br />
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br />
                    .addComponent(jButton1)<br />
                    .addComponent(jButton2))<br />
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br />
                    .addGroup(layout.createSequentialGroup()<br />
                        .addComponent(jLabel4)<br />
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)<br />
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))<br />
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE))<br />
                .addContainerGap())<br />
        );</p>
<p>        pack();<br />
    }// </p>
<p>    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {<br />
 primegen();<br />
    }</p>
<p>    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {<br />
isprime();<br />
    }</p>
<p>    public static void main(String args[]) {<br />
        java.awt.EventQueue.invokeLater(new Runnable() {<br />
            public void run() {<br />
                new GUI().setVisible(true);</p>
<p>            }<br />
        });<br />
    }</p>
<p>    // Variables declaration - do not modify<br />
    private javax.swing.JButton jButton1;<br />
    private javax.swing.JButton jButton2;<br />
    private javax.swing.JLabel jLabel1;<br />
    private javax.swing.JLabel jLabel2;<br />
    private javax.swing.JLabel jLabel3;<br />
    private javax.swing.JLabel jLabel4;<br />
    private javax.swing.JLabel jLabel5;<br />
    private javax.swing.JList jList1;<br />
    private javax.swing.JScrollPane jScrollPane1;<br />
    private javax.swing.JTextField jTextField1;<br />
    private javax.swing.JTextField jTextField2;<br />
    // End of variables declaration</p>
<p>}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mbien
Offline
Joined: 2007-04-29
Points: 0

OK, but where is your problem with the String parsing?

convince me that this is no homework ;)

spartan563
Offline
Joined: 2008-03-04
Points: 0

Well, it was homework but I managed to get it working fine now, thanks anyway......