Scanner is not reading all the text it receives

Asked

Viewed 150 times

0

I’m creating a program whose idea was to take a text with a few words and break them in ",". Only you are reading only up to a 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");

    }
}

and here text I want to break the words (they have to continue with "" because I will use it in another language only "" and space between them )

"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"

2 answers

2

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

Change:

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

}

for:

while (entrada.hasNext()) {

    String str = entrada.nextLine();

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

}
  • then when I run the program it displays nothing, if I take the while and switch to the columns = input.nextLine(); it displays only up to the Tab of the text

  • @Alefribeiro was not to remove the while, was to make the changes I showed in the answer, the problem was your if.

  • but I switched for your while and even then it didn’t work

  • @Alefribeiro the nexline method only takes one line, if you pass text with multiple lines it will not catch everything. This may be the problem.

  • Yes, but I’ve tried with only Next and it didn’t work either, when this with the while you run and it stops at reading nor gets to display anything, it had never happened that

0

got

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");

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.