Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1111111111111"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at GUIPackage.MyForm$4.actionPerformed(MyForm.java:138)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Seems like big numbers give the error. Such as 32416188899 throw the error. Smaller numbers such as 10000 work fine.
package BusinessLogicPackage;
import java.math.BigInteger;
/**
* PrimeNumbers class
*/
public class PrimeNumbers{
/**
* isPrime method
* @param num The number to be checked
* @return true if int is prime
*/
public static boolean isPrime(int num) {
// set status to true
boolean status = true;
// ToDo: Write this logic
int i = 2;
while (i < num) {
// If number is not prime return false
if ((num % i) == 0) {return false;}
i++;
}
// Return true if prime
return status;
}
/*
* BigintisPrime Method
* @ Return boolean
*/
public static boolean BigIntisPrime(BigInteger B)/> {
BigInteger i = new BigInteger("2");
while (i.compareTo(B)/> < 0){
BigInteger tmp = b.mod(i);
if(tmp.compareTo(BigInteger.ZERO) == 0) {
//System.out.println("Factor Found: " + i.toString());
return false;}
i = i.add(BigInteger.ONE);
}
return true;
}
}
HERE IS MY FORM Class
import java.awt.Color;
/**
* MyForm JFrame
*/
public class MyForm extends JFrame {
// Declarations
private JPanel contentPane;
private final JLabel lblEnterANumber = new JLabel("Enter a Number:");
private final JTextField textField = new JTextField();
private final JLabel lblTrueOrFalse = new JLabel();
private final JButton btnExit = new JButton("Exit");
private final JButton btnCompute = new JButton("Compute");
private boolean prime;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyForm frame = new MyForm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setTitle("Find the Prime-Ness!");
// MouseListener to erase text in input box when clicked
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
// When clicked set text field to null
textField.setText(null);
}
});
// Set size for textField
textField.setBounds(10, 26, 138, 20);
textField.setColumns(10);
// Close JFRAME when exited
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set size of JFrame
setBounds(100, 100, 424, 248);
contentPane = new JPanel();
// set border
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
// set color
contentPane.setBackground(Color.green);
setContentPane(contentPane);
contentPane.setLayout(null);
// Add label for enter a number and set size
contentPane.add(lblEnterANumber);
// Set size & position of label
lblEnterANumber.setBounds(10, 11, 102, 14);
// Create text field and set size
contentPane.add(textField);
// Set size & position of label
lblTrueOrFalse.setBounds(10, 142, 388, 14);
// Create answer label and Set to false
contentPane.add(lblTrueOrFalse);
// Set label to hide
lblTrueOrFalse.setVisible(false);
// Exit button action
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Exit program
System.exit(0);
}
});
// Set exit button size
btnExit.setBounds(309, 176, 89, 23);
// Add exit button
contentPane.add(btnExit);
/*
* This action listener will perform different actions based on what is entered
* If negative number: on button press the listener will post warning label
* If positive number: on button press the listener will post message and compute number to find primeness
*/
// Create actionlistener for Compute button
btnCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Set primeValue to what is entered in textField/ set to string
String primeValue = textField.getText();
// parse the string to an integer
int num = Integer.parseInt(primeValue);
// If the number is less than 0 set label to tell user to enter positive number
if ((num < 0) || (num > 10000)){
// Set label to true
lblTrueOrFalse.setVisible(true);
// Print label if number is negative
lblTrueOrFalse.setText("Please enter number greater than 0");
//System.exit(0);
// If positive number continue to find primeness
}else {
// Set to BigInteger
BigInteger myBig = new BigInteger(primeValue);
// Assign prime to isPrime num
prime = PrimeNumbers.BigIntisPrime(myBig);
// If prime print following
if (prime){
// If prime set label to is prime
lblTrueOrFalse.setText("The Number " + primeValue + " is prime-ness");
}
// Else print following
else {
// If not prime set label to is not prime
lblTrueOrFalse.setText("The Number " + primeValue + " does not have prime-ness");
}
// Make label visible
lblTrueOrFalse.setVisible(true);
}
}
});
// Set size of button
btnCompute.setBounds(156, 92, 89, 23);
// Add button
contentPane.add(btnCompute);
}
}

New Topic/Question
Reply



MultiQuote








|