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 20 of 20

Thread: Need Help In Splittin the Java String

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Need Help In Splittin the Java String

    Hi ,

    I have a java string like:
    From: someuser.ha@domain.comDate: 8/7/2012 5:20:39 PMTo: abcd@domain.comCc: abcd.efg@domain.comSubject: This is the suject line Hi,I would like to recover all my money from the bank. Would you be able to send me them?Regards,Farooq.

    Actually above is the email content which is read as a string in a java class.
    Now I would like to email from java application.I'm using the below code.

    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(testcontent, "text/html");
    MimeMultipart mimeMultipart = new MimeMultipart();
    mimeMultipart.addBodyPart(htmlPart);
    message.setContent(mimeMultipart);


    I want help in breaking the above string in to a proper format.The to and CC can contain n number of addressess.
    WILL BE VERY HELPFUL IF YOU COULD WRITE THE CODE, THANKS IN ADVANCE
    Please Help urgently..


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    Also posted at Need Help | DaniWeb

    See the response there.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

    When i write an email like:

    Hi Customer,

    Thank You For calling our bank.

    I see that your acount balance is XXX.
    Blah
    Blah
    Blah

    Thank You,
    Hrithik

    I enter it and store it in DB Field of varchar type.WHEN I retrieve it, its coming as ONE STRING without the mail format.

    Any Suggestions on retrieving in the same format I enter.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    This looks like a new question regarding databases.
    You will get a better response if you ask it in the database section of the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

    Am not worried about storing and retrieving it from DB.
    When I enter input as :
    Hi Customer,

    Thank You For calling our bank.

    I see that your acount balance is XXX.
    Blah
    Blah
    Blah

    Thank You,
    Hrithik

    I want the output as this itself.

    Please Help
    Thx in Advance.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    If you receive the text as a single String, and work with it as a single String, and save it as a single String
    can you explain what is the problem?

    Does the code you are working with break the String up into multiple Strings? Can you keep the code from doing that?
    Leave all of the contents in a single String.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

    Thanks for the prompt Response.!!!

    I have a Swing App which shoots email..
    In the JTextArea I enter:
    Hi Customer,

    Thank You For calling our bank.

    I see that your acount balance is XXX.
    Blah
    Blah
    Blah

    Thank You,
    Hrithik

    I want it in the same format I entered because I'm passing it to message body Content.Plz post ur suggestions on how I should do this..

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    I want it in the same format I entered
    I don't understand how the contents of the String gets changed.
    If there are spaces and line ends in the String, what removes them?

    Can you write a small program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

    Quote Originally Posted by Norm View Post
    I don't understand how the contents of the String gets changed.
    If there are spaces and line ends in the String, what removes them?

    Can you write a small program that compiles, executes and shows the problem?
    Thanks

    I have a question in Swings,

    I have a text Area,beside it I want to have a button on click of Which- open a document-model dialog with TextArea/anything Like that, prepopulated with the content from the beside TextArea.

    When I edit and click OK on the document-modal the content in the TextArea shuold be the same entered in document-modal.

    Any Suggestions on how to do it..

    Thanks in Advance

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    Sorry, I don't know what a "document-modal" is. Can you post a small simple program that compiles, executes and shows the problem?

    What is contained in the String returned by the textarea's getText() method?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

    FIRST:
    String str = queryTextArea.getText();
    final JTextArea textArea = new JTextArea();
    textArea.setFont(new Font("Sans-Serif", Font.PLAIN, 12));
    textArea.setText(str);
    textArea.setCaretPosition(0);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(350, 150));
    int res = JOptionPane.showConfirmDialog(null,scrollPane,"Que ry", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    System.out.println( "showInputDialog: " + res );
    if(res==0)
    queryTextArea.setText(textArea.getText());

    Using this I'm unable to resize the document-modal

    SECOND:

    String str = queryTextArea.getText();
    final JTextArea textArea = new JTextArea();
    textArea.setFont(new Font("Sans-Serif", Font.PLAIN, 12));
    textArea.setText(str);
    textArea.setCaretPosition(0);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(350, 150));
    JOptionPane pane = new JOptionPane(scrollPane, JOptionPane.PLAIN_MESSAGE,JOptionPane.PLAIN_MESSAG E);
    JDialog dialog = pane.createDialog(null, "Query");
    dialog.setResizable(true);
    dialog.setVisible(true);

    Using This I'm unable to get which button is pressed (OK/Cancel/Closed using X)

    Please Help..

    Thanks

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    Can you post a small, complete program that compiles, executes and shows the problem?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need Help In Splittin the Java String

    You can get the user's selection from your pane variable:

     
          JTextArea tArea = new JTextArea(10, 40);
          JScrollPane sPane = new JScrollPane(tArea);
          JOptionPane pane = new JOptionPane(sPane,
                JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
          JDialog dialog = pane.createDialog(null, "Query");
          dialog.setResizable(true);
          dialog.setLocationRelativeTo(null);
          dialog.setVisible(true);
     
          // get the user's selection here:
          Object value = pane.getValue();
          System.out.println("value: " + value);
     
          // first make sure that the user didn't kill the window by pressing on the upper left "x" button
          // which will make the value returned null:
          if (value == null) {
             System.out.println("dialog killed");
          } else if (value.equals(JOptionPane.OK_OPTION)) {
             System.out.println("OK pressed");
          } else if (value.equals(JOptionPane.CANCEL_OPTION)) {
             System.out.println("Cancel pressed");
          }

    This information is all available for you in the JOptionPane API. I suggest you give it a look!
    Last edited by curmudgeon; September 15th, 2012 at 06:53 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts
    Last edited by Norm; September 15th, 2012 at 07:49 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need Help In Splittin the Java String


  16. #16
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Need Help In Splittin the Java String

    This thread has been cross posted here:

    http://www.coderanch.com/t/592613/GUI/java/Help-Swing

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  17. #17
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need Help In Splittin the Java String

    Why all the cross-posts when I answered the question hours ago? One more to add to the do-not-help list?

  18. #18
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: Need Help In Splittin the Java String

    Quote Originally Posted by curmudgeon View Post
    Why all the cross-posts when I answered the question hours ago? One more to add to the do-not-help list?
    Thank You for your prompt response curmudgeon

    Thanks a lot....!!!!!!!!!!!!!!!!!!!!!!!!!

  19. #19
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help In Splittin the Java String

     
    public class classNaan{
    	JTable MyTable = null;
    	JButton detailsbutton = new JButton("Show Details");
    	JPanel MyContainerPanel = new JPanel();
        private static final int CHECK_COL = 0;
     
     
        @SuppressWarnings("unchecked")
    	public JPanel getvaluesfromDB() {
    	 final Vector columnNames = new Vector();
    	 final Vector data = new Vector();
    	try{
    		 ResultSet rs = myservice.getvaluesfromDBforTable();
    		 ResultSetMetaData md = rs.getMetaData();
    		 int columns = md.getColumnCount();
     
    		 columnNames.addElement(new Boolean(false));
    		 for(int i = 1; i <= columns; i++) {
    		 	columnNames.addElement(md.getColumnName(i));
    		 }
    		 columnNames.addElement("Details");
     
    		 while (rs.next()) {
    			Vector row = new Vector(columns);
    			row.addElement(false);
    			for (int i = 1; i <= columns; i++) {
    				row.addElement( rs.getObject(i) );
    			}
    			row.addElement(detailsbutton);
    		    data.addElement(row);
    		 } 
    		}
    		catch(Exception e) {}
    		detailsbutton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	JOptionPane.showMessageDialog(null, "JUBUTTON CLICKED...");
                }
            });
    		JPanel jp = constructTable(data,columnNames);
    		return jp;
        }
        @SuppressWarnings({ "unchecked", "serial" })
    	public JPanel constructTable(Vector data,Vector columnNames) 
        {
            DefaultTableModel dtm = new DefaultTableModel(data,columnNames){
     
            @Override
            public Class getColumnClass(int col) {
                return getValueAt(0, col).getClass();
            }
     
            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return (colIndex == CHECK_COL);
            }
            };
            MyTable = new JTable(dtm);
            MyTable.setAutoscrolls(true);
            MyTable.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            MyTable.setAutoCreateRowSorter(true);
            JTableHeader header = MyTable.getTableHeader();
            header.setBackground(Color.GRAY);
            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(JLabel.CENTER);
            MyTable.setDefaultRenderer(Integer.class, centerRenderer);
            MyTable.setDefaultRenderer(String.class, centerRenderer);
            MyTable.setBorder(BorderFactory.createEmptyBorder(10, 0,0,0));
     
     
            TableColumn tc = MyTable.getColumnModel().getColumn(0);
            tc.setCellEditor(MyTable.getDefaultEditor(Boolean.class));
            tc.setCellRenderer(MyTable.getDefaultRenderer(Boolean.class));
            tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
     
            JScrollPane scrollPane = new JScrollPane(MyTable);
     
            JButton firstbutton= new JButton("FIRST");
            JButton secondButton= new JButton("SECOND");
     
            JPanel buttonpanel = new JPanel();
            buttonpanel.add(followupbutton);
            buttonpanel.add(resolvedbutton);
     
            MyContainerPanel.setLayout(new BoxLayout(MyContainerPanel, BoxLayout.PAGE_AXIS));
            MyContainerPanel.add(scrollPane);
            MyContainerPanel.add(buttonpanel);
                	return MyContainerPanel;
        }
        class MyItemListener implements ItemListener
        {
          public void itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton == false) return;
            boolean checked = e.getStateChange() == ItemEvent.SELECTED;
            for(int x = 0; x< MyTable.getRowCount();x++)
            {
            	MyTable.setValueAt(new Boolean(checked),x,0);
            	MyTable.setValueAt(new JButton(),x,6);
            }
          }
        }
     
    }

    class CheckBoxHeader extends JCheckBox  implements TableCellRenderer, MouseListener {
     private static final long serialVersionUID = 1L;
     protected CheckBoxHeader rendererComponent;
     protected int column;
     protected boolean mousePressed = false;
     public CheckBoxHeader(ItemListener itemListener) {
       rendererComponent = this;
       rendererComponent.addItemListener(itemListener);
     }
     public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
       if (table != null) {
         JTableHeader header = table.getTableHeader();
         if (header != null) {
           rendererComponent.setForeground(Color.black);
           rendererComponent.setBackground(Color.GRAY);
           rendererComponent.setFont(header.getFont());
           header.addMouseListener(rendererComponent);
         }
       }
       setColumn(column);
       rendererComponent.setText("Select All");
       setBorderPaintedFlat(true);
       return rendererComponent;
     }
     protected void setColumn(int column) {
       this.column = column;
     }
     public int getColumn() {
       return column;
     }
     protected void handleClickEvent(MouseEvent e) {
       if (mousePressed) {
         mousePressed=false;
         JTableHeader header = (JTableHeader)(e.getSource());
         JTable tableView = header.getTable();
         TableColumnModel columnModel = tableView.getColumnModel();
         int viewColumn = columnModel.getColumnIndexAtX(e.getX());
         int column = tableView.convertColumnIndexToModel(viewColumn);
         if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
           doClick();
         }
       }
     }
     public void mouseClicked(MouseEvent e) {
       handleClickEvent(e);
       ((JTableHeader)e.getSource()).repaint();
     }
     public void mousePressed(MouseEvent e) {
       mousePressed = true;
     }
     public void mouseReleased(MouseEvent e) {
     }
     public void mouseEntered(MouseEvent e) {
     }
     public void mouseExited(MouseEvent e) {
     }
       }

    For the above code I Have
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5 at java.util.Vector.elementAt(Unknown Source) at

    Content in Table change on time. If I change time of system and call getvaluesfromDB() method I get the above exception

    How do I refresh Jtable (MyTable) every time I call getvaluesfromDB() method..?

    Please tell me I'm wrong anywhere..?

    Also am Unable to JButton in the last column of every row and its listener

    Please Help..

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Help In Splittin the Java String

    ArrayIndexOutOfBoundsException:
    That means the index being used is past the end of the array.
    Remember that array indexes go from 0 to the array length-1.
    Check the value of the index and make sure it does not go past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] How to modify core Java interface java.sql.Statement.execute(String sql)?
    By amughost in forum Java Theory & Questions
    Replies: 6
    Last Post: June 9th, 2012, 04:31 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  4. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM