This is the Java version of ** [paiza] I made my own utility for answering questions [C #] ** that was released before.
Java seems to have a common name of xxx4j, so I named it PaizaUtil4J.
** Revised memo: **
-[Version 1] Implemented Java version based on C # version. -[Version 2] Bug fix for #readLine of DebugIO proxy -[Version3] Implemented change to file input to substitute for sequential string literals.
The functionality and usage are the same as the C # version, so I will omit it this time.
** (*'▽') For those who need explanation, please refer to the article I wrote earlier. ** **
PaizaUtility.java
public class PaizaUtility
{
public interface ITestIO
{
String readLine();
void writeLine(String line);
}
public static class ConsoleProxy implements ITestIO
{
private final Scanner in = new Scanner( System.in );
@Override
public String readLine()
{
return in.nextLine();
}
@Override
public void writeLine(String line)
{
System.out.println( line );
}
}
static ITestIO io;
// static ctor
static {
io = new ConsoleProxy();
}
public static Iterable<String> readArgs()
{
String s = io.readLine();
int n = Integer.valueOf( s );
return readArgs( n );
}
public static Iterable<String> readArgs(final int n)
{
List<String> args = new ArrayList<String>();
for ( int i = 0; i < n; i++ )
{
String arg = io.readLine();
args.add( arg );
}
return args;
}
public interface IFunction<TResult, TParameter>
{
TResult invoke( TParameter parameter );
}
public static Iterable<String> readArgs( IFunction<Integer, String> parseHeaderRecord ) {
String header = io.readLine();
int n = parseHeaderRecord.invoke( header ).intValue();
return readArgs( n );
}
// static class.
private PaizaUtility() { }
}
Do.java (where to implement the answer logic for paiza questions)
public class Do
{
public static void answer()
{
// //If you write the IFunction delegate in lambda, you will get this code.
// Iterable<String> args = PaizaUtility.readArgs(
// s -> Integer.parseInt( s )
// );
Iterable<String> args = PaizaUtility.readArgs();
for ( String arg : args )
{
PaizaUtility.io.writeLine( arg );
}
}
}
Program.java (main entry point of the program)
public class Program
{
public static void main( String[] args )
{
PaizaUtility.io = new DebugIO();
Do.answer();
}
}
So, the implementation of the essential DebugIO class has not been finalized yet.
Program.DebugIO
private static class DebugIO implements ITestIO
{
//C for java#There is no serial string literal notation like this, so it's dull, what should I do?
private final String[] source = {
"2",
"hello",
"world",
};
private int index = 0;
@Override
public String readLine()
{
return index < source.length ? source[index++] : null;
}
@Override
public void writeLine(String line)
{
System.out.println( line );
}
}
In C #, there is a syntax specification called ** sequential string literal **, but Java doesn't have the equivalent function. Because of this, in the C # version, paiza's question parameters can be used as they are from the Web screen by copying, but in the Java version, the same hand cannot be used. What's wrong?
Sorry for the C # -like code style! !! I'm originally from C #, not Java, and I like C # better than Java linguistically! !!
static class problem.
You can create a static class in C #, but you can't create a static class in Java.
Since you can't create a static class in Java, you don't add the static keyword to the outer class (on the enclosing side in Java). Because of this, specifically, the static keyword is not added to the two classes PaizaUtility and Do, so if you copy and paste as it is, a compile error will occur.
If you want to solve this forcibly, you can wrap the two classes in question with a dummy enclosing class and drop them into inner static members.
It seems that I used the Scanner class incorrectly, and it was normally buggy and could not be used with paiza. (To be exact, there were cases where it could not be used)
ConsoleProxy (before modification)
@Override
public String readLine()
{
return in.next();
}
ConsoleProxy (after modification)
@Override
public String readLine()
{
return in.nextLine();
}
What a mess (´ ・ ω ・ `)
Since standard IO is not done so much in Java, I was struck by implementing the paiza official value acquisition / output sample code as it is. I'm sorry I didn't check it well, but ...
Reference information: It was helpful because there was a person who was addicted to a similar place.
I didn't understand the behavior of Java Scanner and .nextLine () well
DebugIO (excerpt from constructor only)
public DebugIO()
{
super();
this.source = readDataSource();
}
private static String[] readDataSource()
{
//For the time being, I will read from the text file with a relative path.
Path path = Paths.get( "dat/datasource.txt" );
try
{
return Files.lines( path ).toArray( String[]::new );
}
catch ( IOException ex )
{
//It's an absolutely impossible case, so anything is fine, but I'll empty it for the time being.
ex.printStackTrace();
return new String[]{};
}
}
dat/datasource.txt
3
hoge
moge
piyo
Execution result
hoge
moge
piyo
The complete solution can be found on GitHub. In the future, I plan to add comments, improve functions, and play with various things.
https://github.com/sugaryo/PaizaUtil4J
Recommended Posts