Scanner is not reading all the text it receives

0

I'm creating a program whose idea was to get a text with a few words and break them in "," . Only you're reading only part of the text.

Follow the code and text below:

public static void main(String[] args) {
    String colunas = "";

    System.out.println("Cole o texto a ser quebrado por virgula :");
    Scanner entrada = new Scanner(System.in);
    while (entrada.hasNext()) {
        if(entrada.nextLine() == null || entrada.nextLine().equals(""))
        {
            break;
        }
        colunas += entrada.nextLine();

    }

    //colunas = entrada.nextLine();
    String[] split = colunas.trim().split(",");

    for (String c : split) {

        System.out.println("FIELD " + c + " AS CHAR");

    }
}

"PETROLEO", "PESTANA", "PESTILENTO", "PETELECO", "REBOQUE", "CADARCO",
            "CADEIRA", "COLA", "REBENTO", "DEFUMADO", "DISCURSO", "ELETRODOMESTICO",
            "ELETRONICA", "ENGRENAGEM", "ESFOMEADO", "FERRALHEIRO", "FERROVIA",
            "FERTIL", "FORTALEZA", "FORTIFICANTE", "OFICINA", "ORNAMENTO", "PALAVRA",
            "PREOCUPACAO", "RADIOLOGIA", "RADIOGRAFIA", "GRANJA", "GRANULADO", "INDUZIDO",
            "IMBATIVEL", "INDUSTRIA", "INTERNACIONAL", "LABIRINTO", "LOBISOMEM",
            "LOCOMOTIVA", "TESOURA", "MASSAGISTA", "MATADOURO", "MOCHILA", "NOROESTE",
            "NITROGLICERINA", "HELICOPTERO", "CAPITALISMO", "SOFTWARE", "ENGENHARIA",
            "NOROESTE", "AUTENTICO", "LINUX", "PROCESSADOR", "QUARENTENA", "MEDICINA",
            "HOLOCAUSTO", "RADIOGRAFIA", "XAROPE", "ZAROLHO", "ZOOLOGICO", "HEREDITARIO",
            "EXTASE", "EXTRAVIO", "DUODENO", "ECOLOGISTA", "TURISMO", "TRAFICANTE",
            "CONSELHO", "BAIXISTA", "AVESTRUZ", "QUIMICA", "BOTANICA", "RESPECTIVO",
            "SAXOFONE", "TABERNA", "OCULTO", "TRIGONOMETRIA", "ZODIACO", "JUSTAPOSTO",
            "HIDRAULICO", "HEXAGONO", "MINEIRO", "FRENETICO", "EXPLOSIVEL", "EXORCISTA"
    
asked by anonymous 10.10.2016 / 14:51

2 answers

2

I think the problem is this condition within your loop, because invoking nextLine() within if , you are requesting data entry.

Switch:

while (entrada.hasNext()) {
    if(entrada.nextLine() == null || entrada.nextLine().equals(""))
    {
        break;
    }
    colunas += entrada.nextLine();

}

by:

while (entrada.hasNext()) {

    String str = entrada.nextLine();

    if(str  == null || str.equals(""))
    {
        break;
    }
    colunas += str;

}
    
10.10.2016 / 14:55
0

I got it

public static void main(String[] args) {
    String colunas = " ";

    System.out.println("Cole o texto a ser quebrado por virgula :");
    Scanner entrada = new Scanner(System.in);

    while(entrada.hasNext()) {
        colunas += entrada.nextLine();
        if (colunas.contains(";")) {
            break;
        }
    }

    colunas = colunas.replace("     ","");
    String[] split = colunas.split(",");

    for (String c : split) {


        System.out.println("FIELD " + c + " AS CHAR");

    }
}
    
11.10.2016 / 14:09