-2

I am coding a BufferedReader in Unity3D with a C# file. When ever I use this line of code it says there is an error of: "Parser Error:Identifier expectied, "in" is a keyword" , but when this code is run in Java there is not an error. Why doesn't System.in exist in C# and how do I fix it?

My code:

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  • 1
    This looks like java, not C#... – Willem Van Onsem May 31 '15 at 00:59
  • 1
    In C# it's `System.Console.In`. There's also a [`Console.OpenStandardInput`](https://msdn.microsoft.com/en-us/library/tx55zca2%28v=vs.110%29.aspx) method; not sure though if that appies to what you're trying to do. – Ed Gibbs May 31 '15 at 01:01
  • 1
    In C# the equivalent is `Console.In`, but there is no `BufferedReader` or `InputStreamReader` either. C# and Java are different languages. I'm not sure why you'd expect code from one to compile in the other. – Mike Zboray May 31 '15 at 01:01
  • 1
    Because `System.in` is Java and not C#. You can fix it by not trying to use `System.in` in C# code, and not trying to compile Java code in a C# compiler. – Ken White May 31 '15 at 01:01
  • Maybe you could find useful the answers for this question: [.NET equivalent of Java's BufferedReader](http://stackoverflow.com/questions/12009695/net-equivalent-of-javas-bufferedreader) – Blas Soriano May 31 '15 at 01:06

2 Answers2

2

System.in is a part of the Java standard library, it is not part of the .NET standard library and thus will not compile.

For C# you have to use Console.In. This is a TextReader. There is not a real equivalent of BufferedReader in C# (that supports all calls). But the TextReader has Read and ReadLine method, which seems to behave more or less the same. You should thus replace it with:

TextReader tr = Console.In;
//use TextReader as inFromUser

As @BlasSoriano says, this question aims to find a behavioral equivalent of BufferedReader.

Furthermore as @KenWhite says, simply copy pasting source code without understanding the underlying idea is a very bad idea. The fact that you think this code is valid C# code, is not very promising.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • The code posted is not C# code (C# doesn't have `BufferedReader`). Therefore, changing the code to use `Console.In` won't fix anything; you can't pretend Java code is C# code. The question posted is simply based on a misconception that a Java file is C# code. Your answer isn't really relevant (or correct). – Ken White May 31 '15 at 01:04
  • @KenWhite: well that's the error. I think the OP simply copied code from somewhere else. This answer provides an (approximate) alternative in C#. I don't pretend this is C# code. I say that this is an (approximate) equivalent. – Willem Van Onsem May 31 '15 at 01:06
  • (Just to be clear, I'm not downvoting this answer). The error is that the poster made a terrible mistake in trying to compile code in the wrong language. Simply correcting that single line of code doesn't fix or correct the underlying issue, which is that the poster doesn't understand code they are copy/pasting well enough to know it's the wrong language. – Ken White May 31 '15 at 02:22
  • @KenWhite: well of course I don't know the details. But I agree that this is not the way to implement applications. I've added a note to the answer the (hopefully) reflects your comments? – Willem Van Onsem May 31 '15 at 02:26
0

If you're using IKVM then it's:

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(java.lang.System.@in));