Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Using scanner method with Split

  1. #1
    ubuntu_jdk is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Question Using scanner method with Split

    Hi guys,

    I am trying to make a GUI program which opens a specific TXT (JFileChooser) and I am using Scanner to Read the TXT file. So far so good, now I want to implement the Split(" "); method to split lines with space and throw them in a Array. Here is where my problem begins.

    The reason I want to use Split is because I want to manipulate the numbers, like add them all together to a SUM.

    The TXT files looks like this:

    Java Code:
    JHJHDFJ 2621646
    KJSKFSd 4564123
    JHSDGdf 1546545
    SDFKBN  156545
    NAME     NUMBER
    ..........   ........

    The Code:

    Java Code:
    btnFile.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				
    	            JFileChooser chooser = new JFileChooser("C:\\");
    	            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    
    	            FileNameExtensionFilter filter = new FileNameExtensionFilter(
    	            " Only .txt", "txt");
    	            chooser.setFileFilter(filter);
    
    
    	            int code = chooser.showOpenDialog(null);
    	            if (code == JFileChooser.APPROVE_OPTION) {
    	            File selectedFile = chooser.getSelectedFile();
    	            Scanner input;
    				try {
    					input = new Scanner(selectedFile);
    					
    					
    					String line = null;
    					String name = null;
    					String number = null;
    				
    					
    			|||||||->		while (input.hasNextLine()){
    						
    						line = input.nextLine();
    						
    						String[] splits = line.split(" ");
    						
    						name = splits[0];
    						number = splits[1];
    						
    						System.out.println(name);
    						System.out.println(number);		
    					}                                                 <-|||||||
    					
    					
    					
    					input.close();
    		            
    					} 
    					 catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}              	
    
    	}
    	            }
    I am not sure about my while loop and how to implement Split so it displays NAME \n NUMBER succesfully in the CONSOLE, and how I can use NUMBER and calculate the sum of all numbers and display it in CONSOLE.

    Its my first time working with Scanner and Split method so maybe I didnt get the basics right. Can anyone please guide me in the right direction?

    Thanks.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Using scanner method with Split

    Is your text file contents formatted in a fixed-length column? I see in the example you post that the number is aligned at a specific column. If that you can use substring instead of split.

  3. #3
    ubuntu_jdk is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Using scanner method with Split

    No it can vary. Its also an assignment so it has to be the Split method.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default Re: Using scanner method with Split

    For summing the numbers you need to have somewhere to hold your ongoing total as you loop over the file.
    Then you need to add each number to it, which means you need to use parse the String holding the number.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ubuntu_jdk is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Using scanner method with Split

    Tolls, thanks for replying.

    I am new to Java, so if you can give me code example that would be very helpfull.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default Re: Using scanner method with Split

    I suggest you try it based on my description.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. String split method
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 02:01 PM
  2. String split method
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 11:06 PM
  3. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 06:15 PM
  4. split method question
    By Chasingxsuns in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 07:19 PM
  5. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 01:54 PM

Posting Permissions

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