Results 1 to 4 of 4

Thread: ProblemJava

  1. #1
    srengvichet is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Talking ProblemJava

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.StringTokenizer;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.table.DefaultTableModel;

    /**
    *
    * @author SRENGVICHET
    */
    public class Book extends JFrame implements ActionListener {

    private JTextField tfid,tftitle,tfprice;
    private JButton bntsave,bntread,bntclear;
    private JTable table;
    private DefaultTableModel model;
    private JScrollPane scroll;


    public Book(){

    JPanel p1=new JPanel();
    p1.setLayout(new GridLayout(3,3));
    p1.add(new JLabel("Id"));
    p1.add(tfid=new JTextField(10));
    p1.add(bntsave =new JButton("Save"));
    bntsave.addActionListener(this);

    p1.add(new JLabel("Title"));
    p1.add(tftitle=new JTextField(10));
    p1.add(bntread=new JButton("Read"));
    bntread.addActionListener(this);

    p1.add(new JLabel("Salary"));
    p1.add(tfprice=new JTextField(10));
    p1.add(bntclear=new JButton("Clear"));
    bntclear.addActionListener(this);

    JPanel p2=new JPanel();
    p2.setLayout(new FlowLayout());
    model=new DefaultTableModel();
    model.addColumn("ID");
    model.addColumn("Title");
    model.addColumn("Salary");
    table=new JTable(model);
    scroll=new JScrollPane(table);
    p2.add(scroll);

    JPanel panel=new JPanel ();
    panel.setLayout(new BorderLayout());
    panel.add(p1,BorderLayout.NORTH);
    panel.add(p2,BorderLayout.CENTER);

    add(panel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(400,500);
    setVisible(true);


    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource() instanceof JButton){
    JButton bnt =(JButton) e.getSource();
    if(bnt ==bntsave){
    getWriteData();
    }else if(bnt ==bntread){
    getReadData();
    }else if(bnt ==bntclear){

    }
    }
    }

    private void getWriteData(){
    int id=Integer.parseInt(tfid.getText().trim());
    String title=tftitle.getText();
    double price=Double.parseDouble(tfprice.getText().trim()) ;
    ClassBook b=new ClassBook(id,title,price);
    try {
    BufferedWriter bw=new BufferedWriter(new FileWriter("F:/Book.txt",true));
    bw.write(b.toString());
    bw.close();

    } catch (Exception e) {
    System.err.println("Erro is "+e.getMessage());
    }
    }

    public ClassBook [] getCutString(){
    String alldata="\n",store="";
    int k=0;
    try {
    BufferedReader br=new BufferedReader(new FileReader("F:/Book.txt"));
    while((store=br.readLine())!=null){
    alldata +=store;
    }
    br.close();
    } catch (Exception e) {
    System.err.println("Error is "+e.getMessage());
    }
    StringTokenizer record=new StringTokenizer (alldata,"$");
    ClassBook b[]=new ClassBook[record.countTokens()];
    while(record.hasMoreTokens()){
    String field=record.nextToken();
    StringTokenizer cutf=new StringTokenizer(field,"|");
    int id=Integer.parseInt(cutf.nextToken());
    String title=cutf.nextToken();
    double price =Double.parseDouble(cutf.nextToken());
    b[k++]=new ClassBook(id,title,price);
    }
    return b;
    }

    public void getReadData(){
    ClassBook book[]=getCutString();
    for(int i=0;i<book.length;i++){
    Object ob[]={book[i].getId(),book[i].getTitle(),book[i].getPrice()};
    model.addRow(ob);
    }
    }

    public static void main(String str[]){
    Book b=new Book();
    }


    }

    //:confused:error message are :
    /*
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1"

    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at guiapplicationyear32.Book.getCutString(Book.java:1 27)
    at guiapplicationyear32.Book.getReadData(Book.java:13 6)
    at guiapplicationyear32.Book.actionPerformed(Book.jav a:88)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6216)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:598 1)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4583)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 13)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4556)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4220)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4150)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475 )
    at java.awt.Component.dispatchEvent(Component.java:44 13)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 12 seconds)

    */

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the exception message. It's telling you that at line 127 of Book.java, you are trying to change a value "1" to an integer. I guess you need to make sure the number doesn't have the quotes around it when you read it from the file.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    java.lang.NumberFormatException
    That is an exception which occures if you try to use your vars with wrong types :)
    Something like a

    Double.parseDouble("ho");
    Last edited by Webuser; 06-20-2010 at 05:41 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Webuser View Post
    That is an exception which occures if you try to init your vars with wrong types :)
    Something like a
    Nonsense; that exception is thrown when you try to parse a String and generate a number, e.g. "123" -> 123. Your example clearly shows that you didn't understand the problem. No rep points for you no matter how much you beg for them.

    kind regards,

    Jos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •