Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

erro java.lang.error System.in e System.out

bom dia, ta acontecendo um error estranho no meu codigo.

package br.com.alura.java.io.teste;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class teste {
    public static void main(String[] args) throws IOException {

        System.out.println();
        InputStream fis = new System.in; //<---aqui
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);    

        OutputStream fos = new System.out; //<----aqui tbm;
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);    

        String linha = br.readLine();

        while (linha != null && !linha.isEmpty()) {
            bw.write(linha);
            bw.newLine();
            bw.flush();
            linha = br.readLine();
        }
        br.close();
        bw.close();
    }

}

o seguinte erro aparece:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    System.in cannot be resolved to a type
    System.out cannot be resolved to a type

    at br.com.alura.java.io.teste.teste.main(teste.java:15)
2 respostas
solução!

Bom dia!

Com relação ao seu problema:

InputStream fis = new System.in; //<---aqui

OutputStream fos = new System.out; //<----aqui tbm;

Você não pode dar um 'new' no System. Não é aceito pelo java você instanciar um 'new' no System. Você utiliza o System da JVM. Se retirar o 'new', seu código deve funcionar.

Além disso, como curiosidade, outras maneiras de fazer a mesma coisa:

Scanner scan = new Scanner(System.in); String s = scan.next();

DataInputStream dis = new DataInputStream(System.in); int i = dis.readInt();

Console console = System.console(); String s = console.readLine();

obrigado, não tinha visto esse erro.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software