7 Replies - 3291 Views - Last Post: 02 March 2011 - 05:25 PM Rate Topic: -----

#1 chicopeemike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

parsing a string using commas as delimiter

Posted 25 February 2011 - 12:49 PM

Hi, I need to parse a string for integers using commas as a delimiter. i.e. joptionpane entry 1,3,5 with .split method and assign them to variables. can't seem to make it work.
Is This A Good Question/Topic? 0
  • +

Replies To: parsing a string using commas as delimiter

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: parsing a string using commas as delimiter

Posted 25 February 2011 - 12:51 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 chicopeemike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: parsing a string using commas as delimiter

Posted 26 February 2011 - 10:10 PM

package largestOfThreeIntegers; still wish I could accept 3 entries separated by commas and parse them?
import javax.swing.JOptionPane;

public class LargestInteger {
   
    public static void main(String[] args )           //main method
    {
          Integer intA = 0;                            //initialize variables
         Integer intB = 0;
         Integer intC = 0;
       
         String outputStr = null;                     //initialize string variables
         String intAString;
         String intBString;
         String intCString;
         
         intAString =  JOptionPane.showInputDialog("This program takes input (three integers) from the user " +
         "and outputs (largest number). Integers must be unique. Enter first integer: ");              //info and first variable entry
 
           intA = Integer.parseInt(intAString);           //parse string variable
       
        intBString = JOptionPane.showInputDialog("Enter second integer:");    //second variable entry
         
        intB = Integer.parseInt(intBString);           //parse string variable
       
        intCString = JOptionPane.showInputDialog("Enter third integer:");      //third variable entry
       
        intC = Integer.parseInt(intCString);           //parse string variable
          
           if((intA > intB) && (intA > intC))              //conditional if statement
           
          outputStr = ("First is the Largest: " + intA);  //initialize output for dialog box 
     
          if((intB > intA) && (intB > intC))              //conditional if statement
       
          outputStr = ("Second is the largest: " + intB); //initialize output for dialog box
       
          if ((intC > intA) && (intC > intB))              //conditional if statement
     
          outputStr = ("Third is the largest: " + intC);   //initialize output for dialog box
       
         JOptionPane.showMessageDialog(null, outputStr,   //format for dialog box and output
          "Largest of Three integers",
          JOptionPane.INFORMATION_MESSAGE);
     
          System.exit(0);                                  // terminates program after user clicks ok
     
     }                                                   //end main method
 
}                                                        //end largest integer class


Student Comments : To do list 1. Learn how to handle exceptions i.e. duplicate entries 2. Learn how to use .split method 3. Learn how to use a parse a string using a delimiter

This post has been edited by macosxnerd101: 26 February 2011 - 10:12 PM
Reason for edit:: Please use code tags!!!

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: parsing a string using commas as delimiter

Posted 26 February 2011 - 10:13 PM

First, use ints over Integers. They are more memory efficient. There is no need to use the Integer wrapper object over the primitive here.

Second, use the String split() method.
String[] splt = "1,2,4".split(","); //split = {"1","2","4"}


Was This Post Helpful? 0
  • +
  • -

#5 chicopeemike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: parsing a string using commas as delimiter

Posted 27 February 2011 - 08:26 AM

First of all thanks. A couple of questions. How can I add line #s to my post and why won't this work?
 /* 2/25/11 This program takes input (three integers)
 * from the user and outputs (largest number). 
 */
package largestOfThreeIntegers; 

import javax.swing.JOptionPane;

public class LargestInteger {
	
	public static void main(String[] args )           //main method
	{
 		 Integer  intA = 0;                           //initialize variables
		 Integer  intB = 0;
		 Integer intC = 0;
		
		 String outputStr = null;                     //initialize string variables
		 String intAString = null;
		 String intBString = null ;
                 String intCString = null ; 
         
          JOptionPane.showInputDialog("This program takes input (three integers) from the user \n" +
		 "and outputs (largest number). Integers must be unique.\n" + "Enter three integers separated by commas: ");  
        //info and first variable entry

         String[] splt = "intA,intB,intC".split(",");//split = {"intA","intB", "intC"} 
        
                intA = Integer.valueOf(intAString);           //parse string variables
		intB = Integer.valueOf(intBString);         
		intC = Integer.valueOf(intCString);            
   		
       
		outputStr = (intA > intB  &&  intA > intC)? "First is the largest " + intA: //shortcut if statement
			        (intB > intA && intB > intC)? "Second is largest " + intB: 
 			        (intC > intA  &&  intC > intB)? "Third is the largest " + intC:"";  
  	  
	 	JOptionPane.showMessageDialog(null, outputStr,   //format for dialog box and output
  		"Largest of Three integers",
  		JOptionPane.INFORMATION_MESSAGE);
  	
  	    System.exit(0);                                  // terminates program after user clicks ok
  	
     }                                                   //end main method
 
}                                                        //end largest integer class
  	  
  	  

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10814
  • View blog
  • Posts: 40,314
  • Joined: 27-December 08

Re: parsing a string using commas as delimiter

Posted 27 February 2011 - 09:35 AM

You need to get user input (from the JOptionPane) and assign it to a String. Then split() that String.

Here, "intA,intB,intC" are String literals. It's like you actually typed or wrote the words down on a sheet of paper. They are not representative of the variables because they are in double quotes.
"intA,intB,intC"


Was This Post Helpful? 0
  • +
  • -

#7 chicopeemike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 11

Re: parsing a string using commas as delimiter

Posted 02 March 2011 - 12:40 PM

View Postmacosxnerd101, on 27 February 2011 - 09:35 AM, said:

You need to get user input (from the JOptionPane) and assign it to a String. Then split() that String.

Here, "intA,intB,intC" are String literals. It's like you actually typed or wrote the words down on a sheet of paper. They are not representative of the variables because they are in double quotes.
"intA,intB,intC"



I had thought that .split was a string manipulation when in fact the output was an array which hadn't been covered yet. Just trying to pick up a few tricks early.Anyway here is what I got out of this. Again Thanks
package largestOfThreeIntegers;
import javax.swing.JOptionPane;

public class LargestInteger {
	
	public static void main(String[] args )           //main method
	{
  		 Integer  intA = 0;                           //initialize variables
  		 Integer  intB = 0;
 		 Integer intC = 0;
		
 		 String outputStr = null;                     //initialize string variables
		 String strInput = null;
		 String intAString = null;
		 String intBString = null;
         String intCString = null; 
         
         strInput = JOptionPane.showInputDialog("This program takes input (three integers) from the user \n" +
		 "and outputs (largest number). Integers must be unique.\n" + "Enter three integers separated by commas: ");  
        //info and first variable entry
     
   		String[] splt = strInput.split(",");             //split = {"intA","intB", "intC"} 
        
  		intA = Integer.parseInt(splt[0]);                //parse string variables
		intB = Integer.parseInt(splt[1]);         
		intC = Integer.parseInt(splt[2]);            
   		
   		outputStr = (intA > intB  &&  intA > intC)? "First is the largest " + intA: //shortcut if statement
			        (intB > intA && intB > intC)? "Second is largest " + intB: 
 			        (intC > intA  &&  intC > intB)? "Third is the largest " + intC:"";  
  	  
	 	JOptionPane.showMessageDialog(null, outputStr,   //format for dialog box and output
  		"Largest of Three integers",
  		JOptionPane.INFORMATION_MESSAGE);
  	
  	    System.exit(0);                                  // terminates program after user clicks ok
  	
     }                                                   //end main method
 
}                                                        //end largest integer class


This post has been edited by macosxnerd101: 02 March 2011 - 02:13 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: parsing a string using commas as delimiter

Posted 02 March 2011 - 05:25 PM

Still do

	public static void main(String[] args )           //main method
	{
  		 int  intA;
  		 int  intB;
 		 int intC;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1