[Java] I want to make it easier because it is troublesome to input System.out.println.

Introduction

While writing a rock-paper-scissors RPG in Java, writing `` `System.out.println``` every time was quite troublesome, so I tried to find out how to make it easier.

I think it will be easier with extensions, but this time I will try from a different direction.

** The general flow is **

--Create a method for console output --Replace the previously described System.out.println with the method name.

The main subject from here

In the commentary, I will try to adapt to only one of the three classes.

Code so far

Judge.java


public class Judge {
	
	public void startJanken(Player player1, Player player2) {
		
		System.out.println("[Start rock-paper-scissors]\n");
		
		for (int cnt = 0;cnt < 3; cnt++) {
			System.out.println("["+(cnt+1)+"Round]");
			
			//Determine which one won
			Player winner = judgeJanken(player1,player2);
			
			if(winner != null) {
				System.out.println("\n"+winner.getName()+"Won");
				winner.notifyResult(true);
			}else {
				System.out.println("It's a draw");
			}
		}
		System.out.println("Rock-paper-scissors finished");
		
		Player finalWinner = judgeFinalWinner(player1,player2);
		System.out.print(player1.getWinCount()+"versus"+player2.getWinCount()+"so");
		
		if (finalWinner != null) {
			System.out.println(finalWinner.getName()+"Is the winner");
		}else {
			System.out.println("It's a draw");
		}
		
		
	}
	private Player judgeJanken(Player player1,Player player2) {
		Player winner = null;
		
		int player1hand = player1.showHand();
		int player2hand = player2.showHand();
		
		printHand(player1hand);
		System.out.print("vs.");
		printHand(player2hand);
		System.out.print("\n");
		
		if((player1hand == Player.stone && player2hand == Player.scissors)||
				(player1hand == Player.scissors && player2hand == Player.paper)||
				(player1hand == Player.paper && player2hand == Player.stone)) {
			winner = player1;
		}else if((player1hand == Player.stone && player2hand == Player.paper)||
				(player1hand == Player.scissors && player2hand == Player.stone)||
				(player1hand == Player.paper && player2hand == Player.scissors)) {
			winner = player2;
		}
		return winner;
	}
	private Player judgeFinalWinner(Player player1,Player player2) {
		Player winner = null;
		
		int player1WinCount = player1.getWinCount();
		int player2WinCount = player2.getWinCount();
		
		
		if(player1WinCount > player2WinCount) {
			winner = player1;
			
		}else if(player1WinCount < player2WinCount) {
			winner = player2;
			
		}
		return winner;
	}
	
	private void printHand(int hand) {
		
		switch(hand) {
		case Player.stone:
		System.out.print("Goo");
		break;
		
		case Player.scissors:
		System.out.print("Choki");
		break;
		
		case Player.paper:
		System.out.print("Par");
		break;
		
		default:
		break;
		}
	}

}

Try adding a method like the one below

       //here! !! !!
	public static void put(String str) {
		System.out.println(str);
	}

Then, instead of typing System.out.println, I could only do put```

Judge.java


public class Judge {
	
	public void startJanken(Player player1, Player player2) {
		
		put("[Start rock-paper-scissors]\n");
		
		for (int cnt = 0;cnt < 3; cnt++) {
			put("["+(cnt+1)+"Round]");
			
			//Determine which one won
			Player winner = judgeJanken(player1,player2);
			
			if(winner != null) {
				put("\n"+winner.getName()+"Won");
				winner.notifyResult(true);
			}else {
				put("It's a draw");
			}
		}
		put("Rock-paper-scissors finished");
		
		Player finalWinner = judgeFinalWinner(player1,player2);
		System.out.print(player1.getWinCount()+"versus"+player2.getWinCount()+"so");
		
		if (finalWinner != null) {
			put(finalWinner.getName()+"Is the winner");
		}else {
			put("It's a draw");
		}
		
		
	}
	private Player judgeJanken(Player player1,Player player2) {
		Player winner = null;
		
		int player1hand = player1.showHand();
		int player2hand = player2.showHand();
		
		printHand(player1hand);
		System.out.print("vs.");
		printHand(player2hand);
		System.out.print("\n");
		
		if((player1hand == Player.stone && player2hand == Player.scissors)||
				(player1hand == Player.scissors && player2hand == Player.paper)||
				(player1hand == Player.paper && player2hand == Player.stone)) {
			winner = player1;
		}else if((player1hand == Player.stone && player2hand == Player.paper)||
				(player1hand == Player.scissors && player2hand == Player.stone)||
				(player1hand == Player.paper && player2hand == Player.scissors)) {
			winner = player2;
		}
		return winner;
	}
	private Player judgeFinalWinner(Player player1,Player player2) {
		Player winner = null;
		
		int player1WinCount = player1.getWinCount();
		int player2WinCount = player2.getWinCount();
		
		
		if(player1WinCount > player2WinCount) {
			winner = player1;
			
		}else if(player1WinCount < player2WinCount) {
			winner = player2;
			
		}
		return winner;
	}
	
	private void printHand(int hand) {
		
		switch(hand) {
		case Player.stone:
		System.out.print("Goo");
		break;
		
		case Player.scissors:
		System.out.print("Choki");
		break;
		
		case Player.paper:
		System.out.print("Par");
		break;
		
		default:
		break;
		}
	}

       //here! !! !!
	public static void put(String str) {
		System.out.println(str);
	}
}


It's a method, so it may be a little troublesome when crossing classes, but it's a lot easier lol

About replacement

In my case, I knew it on the way, so I had to rewrite everything I had written so far to `` `put```.

I was just studying Linux, so try using the `sed command`

Basic syntax of the sed command

~$ sed -e s/Character before replacement/Character after replacement/g [file name]

―― “-e” may or may not be present --If g is not entered, replace only the first character

There are various things such as, but I will omit them here.

Now, replace it at the terminal!

tarminal.



~$ sed -e s/System.out.println/put/g Judge.java 

public class Judge {
	
	public void startJanken(Player player1, Player player2) {
		
		put("[Start rock-paper-scissors]\n");
		
		for (int cnt = 0;cnt < 3; cnt++) {
			put("["+(cnt+1)+"Round]");
			
			//Determine which one won
			Player winner = judgeJanken(player1,player2);
			
			if(winner != null) {
				put("\n"+winner.getName()+"Won");
				winner.notifyResult(true);
			}else {
				put("It's a draw");
			}
		}
		put("Rock-paper-scissors finished");
		
		Player finalWinner = judgeFinalWinner(player1,player2);

		:
                :
                :
                :

	public static void put(String str) {
		put(str);
	}
}

Check the file

tarminal.


~$ cat Judge.java 

public class Judge {
	
	public void startJanken(Player player1, Player player2) {
		
		System.out.println("[Start rock-paper-scissors]\n");
		
		for (int cnt = 0;cnt < 3; cnt++) {
			System.out.println("["+(cnt+1)+"Round]");
			
			//Determine which one won
			Player winner = judgeJanken(player1,player2);
			
			if(winner != null) {
				System.out.println("\n"+winner.getName()+"Won");
				winner.notifyResult(true);
			}else {
				System.out.println("It's a draw");
			}
		}
		System.out.println("Rock-paper-scissors finished");
		
		Player finalWinner = judgeFinalWinner(player1,player2);

		:
                :
                :
                :

	public static void put(String str) {
		System.out.println(str);
	}
}

I thought that the replacement was completed here, but when I checked it with an editor, it was not replaced. ..

When I looked it up here, the sed command was only seen in the preview and was not actually reflected.

I want you to actually reflect it, so if you look it up Optional `` `-i``` (reflected directly in the file)

So I will try again

~$ sed -i -e s/System.out.println/put/g Judge.java





#### **`tarminal.`**

~$ cat Judge.java public class Judge {

public void startJanken(Player player1, Player player2) {
	
	put("[Start rock-paper-scissors]\n");
	
	for (int cnt = 0;cnt < 3; cnt++) {
		put("["+(cnt+1)+"Round]");
		
		//Determine which one won
		Player winner = judgeJanken(player1,player2);
		
		if(winner != null) {
			put("\n"+winner.getName()+"Won");
			winner.notifyResult(true);
		}else {
			put("It's a draw");
		}
	}
	put("Rock-paper-scissors finished");
	
	Player finalWinner = judgeFinalWinner(player1,player2);

	:
            :
            :
            :

public static void put(String str) {
	put(str);
}

}


 It is reflected firmly.


 After that, the method has also been replaced, so I wonder if I should fix it.


### result
 It became easier (^ ∀ ^)


Recommended Posts

[Java] I want to make it easier because it is troublesome to input System.out.println.
I want to summarize Apache Wicket 8 because it is a good idea
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
[Java] I want to test standard input & standard output with JUnit
If you want to make a Java application a Docker image, it is convenient to use jib.
Since the Rspec command is troublesome, I tried to make it possible to execute Rspec with one Rake command
I want to make the frame of the text box red when there is an input error
I want to make an ios.android app
[Java] Beginners want to make dating. 1st
I want to stop Java updates altogether
Setting an alias because it is troublesome to enter "docker-compose" every time
Run R from Java I want to run rJava
I want to send an email in Java.
I did Java to make (a == 1 && a == 2 && a == 3) always true
I want to use java8 forEach with index
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
rsync4j --I want to touch rsync in Java.
I want to write quickly from java to sqlite
[Android] I want to make QA easier ... That's right! Let's make a debug menu!
I want to implement it additionally while using kotlin on a site running Java
I want to do something like "cls" in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I tried to make FizzBuzz that is uselessly flexible
I used jnr-ffi (made it easier to use, etc.)
I want to transition screens with kotlin and java!
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
I want to find out which version of java the jar file I have is available
I want to return a type different from the input element with Java8 StreamAPI reduce ()
[Java] I want to convert a byte array to a hexadecimal number
I want to build Java Applet without using an IDE
I want to make a specific model of ActiveRecord ReadOnly
Make Docker confusing with Pokemon and make it easier to attach
I just wanted to make a Reactive Property in Java
I want to use the Java 8 DateTime API slowly (now)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to make a client of RESAS-API in Java
I want to implement various functions with kotlin and java!
I want to return multiple return values for the input argument
I want to simplify the conditional if-else statement in Java
Java --How to make JTable
I want to convert characters ...
[Java] Make it a constant
Input to the Java console
Is it possible to automatically generate Getters / Setters with Java Interface?
I want to make a button with a line break with link_to [Note]
I just want to write Java using Eclipse on my Mac
I want to return to the previous screen with kotlin and java!
I tried to make an Android application with MVC now (Java)
I want to limit the input by narrowing the range of numbers
I wanted to make JavaFX programming easier with the Spring Framework
I tried to make it an arbitrary URL using routing nesting
[Java] I tried to make a maze by the digging method ♪
[Note] Java: Is it necessary to override equals for equality judgment?
[Java] I want to perform distinct with the key in the object
I tried to make Numeron which is not good in Ruby