Usuwanie duplikatów spacji.

0

Witam,

mam problem, gdyż nie mogę sobie poradzić z prostą rzeczą. Mianowicie mam podstawkę w której muszę dorobić do klasy metodę, pętle usuwającą duplikaty. Tak wygląda:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class StringTool {

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	private static void createAndShowGUI() {
		// JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("String Tool");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension minimumSize = new Dimension(500, 500);
		frame.setMinimumSize(minimumSize);
		frame.setLocationRelativeTo(null);
		Container contentPane = frame.getContentPane();
		GroupLayout gl = new GroupLayout(contentPane);
		contentPane.setLayout(gl);
		gl.setAutoCreateContainerGaps(true);

		JLabel label = new JLabel("Input");

		JTextArea inputTextArea = new JTextArea();
		// inputTextArea.setTabSize(1);
		JScrollPane scrollInputArea = new JScrollPane(inputTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		JTextArea outputTextArea = new JTextArea();
		JScrollPane scrollOutputArea = new JScrollPane(outputTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

		outputTextArea.setEditable(false);
		JButton executeButton = new JButton("Action");
		executeButton.addActionListener(new OnClickActionListener(inputTextArea, outputTextArea));
		gl.setHorizontalGroup(gl.createParallelGroup().addComponent(label).addComponent(scrollInputArea)
				.addComponent(executeButton).addComponent(scrollOutputArea));
		gl.setVerticalGroup(gl.createSequentialGroup().addComponent(label).addGap(10).addComponent(scrollInputArea)
				.addGap(10).addComponent(executeButton).addGap(10).addComponent(scrollOutputArea));

		frame.pack();
		frame.setVisible(true);
	}

}
class OnClickActionListener implements ActionListener {
	private JTextArea inputTextArea;
	private JTextArea outputTextArea;

	public OnClickActionListener(JTextArea inputTextArea, JTextArea outputTextArea) {
		this.inputTextArea = inputTextArea;
		this.outputTextArea = outputTextArea;
	}

	public void actionPerformed(ActionEvent e) {
		String inputText = inputTextArea.getText();
		String outputText = easer(inputText);
		outputTextArea.setText(inputText);
	}
	
	private String easer(String inputText) {
		
		byte[] nowyinput = inputText.getBytes(); @nie wiem czy to potrzebne
	
			
		return inputText;			
	}

}; 

Chcę aby wszystkie duplikaty spacji znikły, aby została tylko jedna.
Wydaje mi się, że najlepiej będzie to zrobić w pętli ale niestety mam zbyt małe doświadczenie w programowaniu, dopiero zaczynam.
Jak mógłbym to zrobić ? Bo od 2 tygodni stoję w miejscu :( Z góry dziękuje.

1

Użyj metody replaceAll z odpowiednim regex'em

// Zakładam że chodzi Ci o zmienną inputText

private String removeDoubleSpaces(String text) {
	return text.replaceAll("\\s+", " ");
}
// Przezwałem tą metodę aby dokładniej "opisywała" co robi.

Co oznacza \\s+? \s to spacja, tabulatura, nowa linia i parę innych, + oznacza jedno albo więcej (tutaj lista). Więc gdy wyrażenie regularne znajdzie kilka spacji obok siebie to zostaną one zastąpione jedną spacją.

0

Wielki dzięki za metodę i wyjaśnienie, właśnie o to mi chodziło :) Programik po poprawce działa jak trzeba.

1 użytkowników online, w tym zalogowanych: 0, gości: 1