Separate street address from a string

2

In my application JAVA , on a screen, I have fields related to the address, I have two ways to register this address, I can automatically pick up the zip code most of the information or put everything "manually" p>

The problem is that many times the zip code will not return you a result, or return only some information, such as where I live (it's a small district). So I decided to leave the option to manually put.

In the Database, I saved Separately Type and Name of Public Address.

To help you understand the fields: Field Type of public place ( comboBox ): Avenue, Street, Road and etc.

Field name of the Backyard : Paraná, Cruzeiro, Rio Grande do Sul and etc;

Looking for the zip code automatically it will fill in the field related to the name of the street. He will fill the field for example with Avenida Paraná. Picking it up automatically does not distinguish type of name. If you save the "caught" data automatically, when the query is performed, it will not be left in the fields referring to each content, for example: Arrowhead in combo and Paraná in TextField (streetname). >

What I want is to be able to separate the type, the name (take the avenue). The purpose of having this comboBox (type of place), is so that when the inclusion is "manual", the user does not begin to register the same type of Place of Publication in different ways, for example: Avenue, Av., Avn , AVENIDA, avenue and etc.

If there is a way that can address these conditions in a simpler or different way, I am accepting suggestions.

I tried to separate using split() , however it does not distinguish the position, maybe adding regular expression would be possible?

Note: In the simple example I put below, I did not put the option to get it automatically, since what I need to know is how to sort the content inside the string .

Obs2: If the question is still "weird", I'll remove it.

A very simple example of what I tried would be:

package testeCombo;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainCombo extends JFrame implements ActionListener
{   
    private final String logradouro[] = {"<Selecione>", "Avenida", "Rua", "Estrada"};

    private JComboBox cmb = new JComboBox();
    private JTextField tf = new JTextField();    
    JPanel painel = new JPanel();
    JPanel painelBotao = new JPanel();
    private final JButton incluir = new JButton("OK");
    private final JButton consultar = new JButton("CONSULTAR");         
    private String salvaBD = "";
    JLabel label = new JLabel();
    JLabel label2 = new JLabel();

    public MainCombo() 
    {     
        setTitle("Combo + Text");
        setSize(550, 150);
        setResizable(false);         

        cmb = new JComboBox(logradouro);
        tf = new JTextField();        
        tf.setPreferredSize(new Dimension(110, 25));
        painel.add(cmb);
        painel.add(tf);   
        getContentPane().add("South", painelBotao);        
        painelBotao.setLayout(new GridLayout(1, 2));
        adicionaBotao(incluir);
        adicionaBotao(consultar);
        painel.add(label);        
        painel.add(label2);        
        add(painel);     
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void acao()
    {
        cmb.getSelectedItem();
        tf.getText();        
        salvaBD = (cmb.getSelectedItem() + " " + tf.getText());
        label.setText(salvaBD);
        cmb.setSelectedIndex(0);
        tf.setText("");        
    }

    public void consultar()
    {
        separa();
    }

    public void separa()//separa para quando consultar, o logradouro ir para o combo e o nome do logradouro pro TextField
    {     
        /*
        String [] separa = salvaBD.split(" ");
        String separado = separa[0].split(" ")[1];              
        label2.setText(" " + separado); 
        */        
        String[] arrayValores = salvaBD.split(" ");
        label2.setText(" " + Arrays.toString(arrayValores)); 
        //cmb.setSelectedIndex(); //setar valor no textField quando consulta   
        //tf.setValor(salvaBD);  //setar valor no textField quando consulta    
        label.setText("");// so para limpar o outro label
    }


    @Override
    public void actionPerformed(ActionEvent ae)
    {

        if (ae.getSource() == incluir) 
        {            
            acao();
        } 

        if (ae.getSource() == consultar) 
        {            
            consultar();
        } 
    }

    private void adicionaBotao(JButton botao) 
    {
        painelBotao.add(botao);
        botao.addActionListener(this);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> 
        { 
            MainCombo combo = new MainCombo();
            combo.setVisible(true);
        });        
    }

    class textF extends JTextField
    {
        public void setValor(Object valor) 
        {
            setText("" + valor);
        }
    }        
}
    
asked by anonymous 27.04.2017 / 19:03

1 answer

2

If it is a default of every address you query (I believe it to be, mail-based APIs are usually always this way), to separate the type of public address you can use the split() ", passing a space as a separator. Then, from the resulting array, get the first index, it will be the type.

Once with the type separated, just compare with those of your array logradouros to select the option in JComboBox .

Based on your example, I made changes to 2 methods only, to illustrate:

public void consultar() {
    String retorno = JOptionPane.showInputDialog("Consulta");
    separa(retorno);
}

public void separa(String retorno) {

    if (retorno != null) {
        retorno = retorno.trim();
        String logTipo = retorno.split(" ")[0];

        for (String log : logradouros) {
            if (log.equalsIgnoreCase(logTipo)) {
                cmb.setSelectedItem(log);
                break;
            }
        }
        label2.setText(retorno.substring(retorno.indexOf(" "), retorno.length()));
        cmb.setEnabled(false);

    }
}

Note that I used equalsIgnoreCase to avoid comparisons like "street" and "street" returning false.

With these changes, the code works like this:

Online:

label2.setText(retorno.substring(retorno.indexOf(" "), retorno.length()));

I am separating the rest of the street without the type, cutting the address string of the first space just after the type to the end. trim() is to remove unneeded spaces at the beginning and end of the string.

    
29.04.2017 / 01:04