Recorded because I was addicted to the standard input of the Scanner class

About the hasNext () method

I tried a quiz site called Code Nachara. Below is the answer I made at that time. I got stuck in the hasNext () method in the Scanner class. When executing standard input as a scan target, the hasNext () function does not return false unless the input is properly terminated with Ctrl + z at the end of the standard input. Therefore, if you do not finish it properly, you will not get out of the loop.

It's been a long time without noticing this ...

that's all

package questions;

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		int count = 1;
		int member = 0;
		String input = null;
		double resultAvgs[] = new double[12];

		try{
			Scanner scanner = new Scanner(System.in);

			// resultAvgs[]Store in
			while(scanner.hasNext()){
				input = scanner.nextLine();
				resultAvgs[member] = calcAvrg(input);
				member += 1;
			}

			for(int i=0; i<member; i++){
				for(int j=0; j<member; j++){
					if(i==j){
						continue;
					}else{
						if(resultAvgs[i] >= resultAvgs[j]){
							//If there is a small value, drop one rank
							count += 1;
						}
					}
				}
				System.out.println(count);
				//Initialization
				count = 1;
			}

			scanner.close();

		}catch(NullPointerException e){
			e.printStackTrace();
		}
	}

	//Read the race result and calculate the harmonic mean
	static double calcAvrg(String in) {
		double total = 0;
		String[] results = in.split(" ", 0);

		for (String result : results) {
			double buff = 1 / Double.parseDouble(result);
			total += buff;
		}

		return results.length / total;
	}
}


Recommended Posts

Recorded because I was addicted to the standard input of the Scanner class
I was addicted to the setting of laradock + VSCode + xdebug
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
I was addicted to the roll method
I was addicted to the Spring-Batch test
I was addicted to the API version min23 setting of registerTorchCallback
I was addicted to scrollview because I couldn't tap the variable size UIView
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the NoSuchMethodError in Cloud Endpoints
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
What I was addicted to when introducing the JNI library
I was addicted to looping the Update statement on MyBatis
What I was addicted to with the Redmine REST API
The story I was addicted to when setting up STS
I was addicted to starting sbt
How to display 0 on the left side of the standard input value
I want to limit the input by narrowing the range of numbers
About the matter that I was addicted to how to use hashmap
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
A memo that was soberly addicted to the request of multipart / form-data
Memorandum: What I was addicted to when I hit the accounting freee API
What I thought when passing the user input value to the Service class
[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
I was addicted to a simple test of Jedis (Java-> Redis library)
Problems I was addicted to when building the digdag environment with docker
I was addicted to unit testing with the buffer operator in RxJava
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I was addicted to using RXTX on Sierra
I want to output the day of the week
I want to var_dump the contents of the intent
I was addicted to installing Ruby/Tk on MacOS
I was addicted to doing onActivityResult () with DialogFragment
I was addicted to not being able to connect to AWS-S3 from the Docker container
I was a little addicted to the S3 Checksum comparison, so I made a note.
About next () and nextLine () of the Scanner class
How to display the result of form input
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I want to recursively get the superclass and interface of a certain class
I want to call a method of another class
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
GetXxxx of ResultSet was addicted to primitive type (Java)
Addicted to the webpacker that comes standard with Rails 6
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
I didn't understand the behavior of Java Scanner and .nextLine ().
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I tried to summarize the basics of kotlin and java
What I was addicted to while using rspec on rails
I was addicted to setting default_url_options with Rails devise introduction
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
[Swift] I tried to implement the function of the vending machine
Technical causes and countermeasures for the points that I was addicted to with the Android app & Kotlin (2. Processing related to the camera function of Android *)
A story I was addicted to in Rails validation settings