Jump to content

Hi, I am trying to make a simple code, which would read characters from console and write them back:

package com.company;

import java.io.IOException;

public class Main {

    public static void main(String[] args)throws IOException {
        // write your code here
        byte[] byteArray = null;
        System.out.println("Write some text:");
        System.in.mark(100);
        int textLenght = 0;
        while (System.in.read() != 10) {
            textLenght++;
        }
        byteArray = new byte[textLenght];
        System.in.reset();
        System.in.read(byteArray);
        String text = new String(byteArray);
        System.out.println("You have written " + text);


    }
}

 

The code above works normally, program ends and doesn't throw any exceptions. When I put this code in while(true) block to reuse the code though, I get this result:

Write some text:
helloworld
You have written helloworld
Write some text:
You have written 
Write some text:
You have written 
Write some text:
You have written 
Write some text:
You have written 
Write some text:
You have written 
Write some text:
You have written

I have no idea why the Thread doesn't just wait for input after one loop. Any ideas why?

Link to comment
https://linustechtips.com/topic/887383-a-problem-with-systeminread/
Share on other sites

Link to post
Share on other sites

4 minutes ago, Lanng said:

I am not quite sure what you want your code to do?

The code you posted does not output what you wrote.

 

What did you change?

I just put it inside while brackets, as I said:

public class Main {

    public static void main(String[] args)throws IOException {
        // write your code here
        while(true){
            byte[] byteArray = null;
            System.out.println("Write some text:");
            System.in.mark(100);
            int textLenght = 0;
            while (System.in.read() != 10) {
                textLenght++;
            }
            byteArray = new byte[textLenght];
            System.in.reset();
            System.in.read(byteArray);
            String text = new String(byteArray);
            System.out.println("You have written " + text);
        }
    }
}

I just want the code to ask for an input again and wait.

Link to post
Share on other sites

The code fails at your while(System.in.read() != 10).

What that code checks for, is if System.in.read() returns a new line (in ASCII code http://www.asciitable.com/).

What System.in is reading is controlled by the system and therefore you can't just call reset, as far as I understand.

 

I would suggest another implementation that I find more readable.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while(true) {
			System.out.println("What is your name?");
			String name = sc.nextLine();
			System.out.println("Your name is " + name);
		}
	}
}

 

Link to post
Share on other sites

11 minutes ago, Lanng said:

The code fails at your while(System.in.read() != 10).

What that code checks for, is if System.in.read() returns a new line (in ASCII code http://www.asciitable.com/).

What System.in is reading is controlled by the system and therefore you can't just call reset, as far as I understand.

 

I would suggest another implementation that I find more readable.


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while(true) {
			System.out.println("What is your name?");
			String name = sc.nextLine();
			System.out.println("Your name is " + name);
		}
	}
}

 

I know the Scanner, but wanted to try the System.in stream. What I don't understand, is why does it run normally for the first time (waits until I write something and after pressing enter starts reading it) but does not wait again?

Link to post
Share on other sites

The problem with your while(System.in.read() != 10). You are checking if System.in.read() is not a new line.

I guess that when you enter your first input and hit enter, you create a new line and therefore your while-loop will not be true.

To solve this, you would have to find another statement that holds true after your first input.

 

The Scanner is using the System.in (new Scanner(System.in);).

If you want to try other methods, you could try to use a buffered reader and an input stream reader?

Link to post
Share on other sites

11 minutes ago, Lanng said:

The problem with your while(System.in.read() != 10). You are checking if System.in.read() is not a new line.

I guess that when you enter your first input and hit enter, you create a new line and therefore your while-loop will not be true.

To solve this, you would have to find another statement that holds true after your first input.

 

The Scanner is using the System.in (new Scanner(System.in);).

If you want to try other methods, you could try to use a buffered reader and an input stream reader?

Ah I see. Thanks, I'll try other methods as well

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×