- 投稿日:2020-11-17T21:44:41+09:00
【Java】parseIntとvalueOfの違い
プログラミング勉強日記
2020年11月17日
コードを書いていて、parseInt
とvalueOf
の違いがわからなかったので備忘録として残しておく。違い
コードを実行したときに結果が同じになる。調べてみると戻り値が違うと出てきた。具体的には、
parseInt
がint型やchar型などのプリミティブ型でvalueOf
はIntegerクラスを返す。public class Main { public static void main(String[] args) throws Exception { int hoge = Integer.valueOf("12345"); System.out.println(hoge); int fuga = Integer.parseInt("12345"); System.out.println(fuga); } }実行結果12345 12345参考文献
- 投稿日:2020-11-17T07:12:11+09:00
【Java】値/文字列を比較(AOJ14 - カードゲーム)
値や文字列を比較
- compareToメソッド
- 戻り値はメソッドの呼び出し元の値(変数1) - 引数の値(変数2)
- 左の値 - 右の値=正: 1
- 左の値 - 右の値=負: -1
- 左の値 - 右の値=0:0
- 日時も比較可能
- compareToIgnoreCaseメソッド:文字列の大文字と小文字を区別しない
import java.util.Date; public class Main { public static void main(String[] args) { String str1 = "a"; String str2 = "b"; String str3 = "A"; Date date1 = new Date(2017, 3, 28, 16, 20, 22); Date date2 = new Date(2017, 3, 29, 16, 20, 24); System.out.println(str1.compareTo(str2)); //-1 System.out.println(str2.compareTo(str1)); //1 System.out.println(str1.compareTo(str1)); //0 System.out.println(str1.compareTo(str3)); //32 System.out.println("==="); System.out.println(str1.compareToIgnoreCase(str3)); //0 System.out.println("==="); System.out.println(date1.compareTo(date2)); //-1 System.out.println(date2.compareTo(date1)); //1 System.out.println(date1.compareTo(date1)); //0 } }カードゲーム(ITP1-9)
太郎と花子がカードゲームをする。二人はそれぞれn枚のカードを持っており、nターンの勝負を行う。各ターンではそれぞれ1枚ずつカードを出す。カードにはアルファベットからなる動物の名前が書かれており、辞書順で大きいものがそのターンの勝者となる。勝者には3ポイント、引き分けの場合にはそれぞれ1ポイントが加算される。
太郎と花子の手持ちのカードの情報を読み込み、ゲーム終了後のそれぞれの得点を出力するプログラムを作成せよ。
Constraints
* 入力で与えられるnが1000を超えることはない。
* 与えられる文字列の長さは100以下であり、アルファベットの小文字のみを含む。
Input
一行目にカードの数nが与えられる。続くn行に各ターンのカードの情報が与えられる。1つ目の文字列が太郎のカードに書かれている文字列、2つ目の文字列が花子のカードに書かれている文字列である。
Output
1つ目の数字が太郎の得点、2つ目の数字が花子の得点として1行に出力せよ。2つの数字の間に1つの空白を出力せよ。
Sample Input
3
cat dog
fish fish
lion tiger
Sample Output
1 7import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int Taro = 0; int Hanako = 0; for (int i = 0; i < a; i++) { String b = scanner.next(); String c = scanner.next(); if (b.compareTo(c) > 0) Taro += 3; else if (b.compareTo(c) < 0) Hanako += 3; else { Taro += 1; Hanako += 1; } } System.out.println(String.format("%d %d",Taro, Hanako)); } }
- 投稿日:2020-11-17T02:22:54+09:00
JDBI3でInputStreamをBindしたい時
JDBI2ではできたのに
@SqlUpdate("update samples set file = :file where flag = true") int updateFile(@Bind("file") InputStream file);@SqlUpdate("update samples set file = :file where flag = true") fun updateFile(@Bind("file") file: InputStream?): IntJDBI3ではできなくなっている...
org.jdbi.v3.core.statement.UnableToCreateStatementException: No argument factory registered for type ...のようなエラーが出てくる
GitHubでエラーメッセージを検索したところ、↓が怪しい
return new UnableToCreateStatementException("No argument factory registered for '" + value + "' of qualified type " + qualifiedType, ctx);どうやら、Bind対象のオブジェクトは Argument interfaceを実装している必要がある
(気が向いたら詳細追記)
Bind用のInterfaceが用意されていた
解決策としては、InputStreamArgumentを使えば良い
@SqlUpdate("update samples set file = :file where flag = true") int updateFile(@Bind("file") InputStreamArgument file);@SqlUpdate("update samples set file = :file where flag = true") fun updateFile(@Bind("file") file: InputStreamArgument?): Inthttp://jdbi.org/apidocs/org/jdbi/v3/core/argument/InputStreamArgument.html
第一引数に、InputStream
第二引数に、Streamのサイズ
第三引数に、ASCIIか否かを渡すpublic InputStreamArgument(InputStream stream, int length, boolean ascii) Parameters: stream - the stream to bind length - the length of the stream ascii - true if the stream is ASCII