count occurence of char

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 853 Views - Last Post: 06 September 2019 - 11:24 AM Rate Topic: -----

#1 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

count occurence of char

Posted 03 September 2019 - 01:39 PM

Hi all:

I need support regarding how to count occurence of a last chart in a string from text file ?

I have started with this which count only one occurence of last X char :


String REG1 = "(?<table>TABLE-\\d+)\\s+(?<PLAT>\\w+(?:X|Y|Z|$))"; 


  btntest.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent e) {

           try (Scanner ST = new Scanner(new File("C:\\Test.txt"))) {
                       ST.useDelimiter("MAC");
                       while( ST.hasNext() )
                       {
                           String data = ST.next();


                         matcher  = Pattern.compile(REG1).matcher(data);
                       int count = 0;        
                               if(matcher.find()) {
                                count++;                                 
  textField.setText(textField.getText()+count);       // count last char x

 // textField2.setText(textField2.getText()+count);  // count last char Y

 // textField3.setText(textField3.getText()+count);  // count last char z 

       }

    sc.close();

thanks 












Is This A Good Question/Topic? 0
  • +

Replies To: count occurence of char

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 03 September 2019 - 01:41 PM

Can you explain what the program current does and what needs to be added to the code?
Post the program's current output and add some comments describing what you want the output to be.

I suggest that you write a small, simple program to test the implementation of the algorithm to make it easier to compile and execute for debugging.
When it works, you can add the working code as a method and have the listener method call it.

This post has been edited by NormR: 03 September 2019 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

Re: count occurence of char

Posted 03 September 2019 - 02:22 PM

hi again:

I have in a text file the word which contain RouterX and RouterY and RouterZ .

I want to count how many Router have X at the end and put the result in textfield and the same thing for Y ( put result in

textfiled2) and for Z put the result of counting in textfield3.

NB: the result of my code when I run it display number one which is number of X found in the word RouterX.

I took a portion from my text file ( 3 block) and ( It may have 12 block like this ) see bellow:

Sorry I put the example in code area :

MAC            PLAT  
TABLE          RouterX  


MAC            PLAT  
TABLE-1        RouterY  


MAC            PLAT  
TABLE-2        RouterZ 









thanks
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,727
  • Joined: 20-September 08

Re: count occurence of char

Posted 03 September 2019 - 02:34 PM

It might help were you to attach the actual file
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 03 September 2019 - 03:19 PM

What have you tried?
Post your small, complete program that can be compiled and executed and shows what you have tried so far.

This post has been edited by NormR: 03 September 2019 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

Re: count occurence of char

Posted 04 September 2019 - 05:04 AM

Hi gOOse : in attchement the asked file .


2/ I have listed what I have tried you mean to send you exported jar file ?

private static final String REG1 = "(?<table>TABLE-\\d+)\\s+(?<PLAT>\\w+(?:X|Y|Z|$))";



  btntest.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent e) {

           try (Scanner ST = new Scanner(new File("C:\\File_Template.txt"))) {
                       ST.useDelimiter("MAC");
                       while( ST.hasNext() )
                       {
                           String data = ST.next();


                         matcher  = Pattern.compile(REG1).matcher(data);
                       int count = 0;        
                               if(matcher.find()) {
                                                               
  
//  for( int i = 0; i< data.size();i++ {if(data[i] == ' ' || data[i] == '\n'){ int j = i;if(data[j-1]=='X'||data[j-1]=='Y'||data[j-1]=='Z'{counter++;} }}


  System.out.println(counter);
     
   textField.setText(textField.getText()+counter);  count last char X

// textField2.setText(textField2.getText()+counter);  // count last char Y

 // textField3.setText(textField3.getText()+counter);  // count last char z 


       }

    sc.close();  




thanks

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 04 September 2019 - 05:24 AM

Can you make a small, complete program that compiles and executes for testing?

The code from the listener is useless for testing with.

This post has been edited by NormR: 04 September 2019 - 05:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

Re: count occurence of char

Posted 04 September 2019 - 11:36 AM

I have exported jar file to the link bellow:

https://transfernow.net/83ief5i4c612

I"m using eclipse MARS2
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 04 September 2019 - 12:16 PM

Sorry, I meant for you to make a small, complete program for testing the logic for the specific problem of extracting and counting the data in the file. There shouldn't be anything else in the program. The total size of the program would be about 50 lines +/- 20.
Don't post the full program.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15228
  • View blog
  • Posts: 60,973
  • Joined: 12-June 08

Re: count occurence of char

Posted 04 September 2019 - 12:20 PM

(The code and not the random jar)
Was This Post Helpful? 0
  • +
  • -

#11 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

Re: count occurence of char

Posted 04 September 2019 - 12:32 PM

this is what can I send.


public class countchar {
	
	
	private static final String REG1 = "(?<table>TABLE-\\d+)\\s+(?<PLAT>\\w+(?:X|Y|Z|$))";

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JLabel lblCountX;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					countchar window = new countchar();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public countchar() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnRun = new JButton("  RUN");
		btnRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try (Scanner ST = new Scanner(new File("C:\\File_Template.txt"))) {
					
                    ST.useDelimiter("MAC");
                    
                    while( ST.hasNext() )
                    {
                        String data = ST.next();
                       
                        Matcher      matcher  = Pattern.compile(REG1).matcher(data);
                         
                        int count = 0;        
                           
                        if(matcher.find()) {                                                            

           //for( int i = 0; i< data.size();i++ {if(data[i] == ' ' || data[i] == '\n'){ int j = i;if(data[j-1]=='X'||data[j-1]=='Y'||data[j-1]=='Z'{counter++;} }};
                              count++;

                          System.out.println(count);
  
                       textField.setText(textField.getText()+count); // count last char X

                       //textField2.setText(textField2.getText()+counter);  // count last char Y

                       // textField3.setText(textField3.getText()+counter);  // count last char z 

                            }                            
                    }                    

                  ST.close();  


			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			}
		});
		
	}
}








Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 04 September 2019 - 12:38 PM

Ok, can you change that code by removing the JFrame class and all the GUI components?
For testing the algorithm that extracts and counts the values there only needs to be a main method that contains the code for reading and extracting the data. Nothing more.
Was This Post Helpful? 0
  • +
  • -

#13 Newprog45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-September 19

Re: count occurence of char

Posted 04 September 2019 - 01:17 PM

sorry how to do that this only what I know to do till now .
Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: count occurence of char

Posted 04 September 2019 - 01:24 PM

Copy the code from inside of the action listener method and put it in a main method.
Something like this:
  public class TestClass {
    public static void main(String[] args) {
       //  PUT THE CODE TO TEST HERE
       //  Report at the end
       System.out.println("counts x="+countX + ", y="+countY + ", z="+countZ);
    }
  }

This post has been edited by NormR: 05 September 2019 - 04:05 AM

Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,727
  • Joined: 20-September 08

Re: count occurence of char

Posted 05 September 2019 - 03:23 AM

I would use an entirely different approach, searching each line of the file for a match on RE "Router[XYZ]"
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2