Bランクレベルアップメニュー 指定範囲だけ大文字 Java 解答

# STEP: 18 文字列の分割

## 問題

https://paiza.jp/works/mondai/prob60/substring_upper_0

## 解答

```java:step18.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        doAStringBreak(sc);
        sc.close();
    }
    
    /**
     * 文字列改行をする関数
     * 
     * @param sc 標準入力
     */
    public static void doAStringBreak(Scanner sc){
                
        while(sc.hasNext()){    
            System.out.println(sc.next());
        }
        
    }
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/d09d7111-0fbe-4d06-beaa-8985a19f7ca4.png)

# STEP: 19 整数の足し算


## 問題

https://paiza.jp/works/mondai/prob60/substring_upper_1

## 解答

```java:step19.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(addUp(sc.nextInt(), sc.nextInt()));
        sc.close();
    }
    
    /**
     * 足し算する関数
     * 
     * @param a 足し算する数字
     * @param b 足し算する数字
     * 
     * @return 足し算結果
     **/
    public static int addUp(int a, int b){
        return a + b;
    }
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/c82d7fc5-c5f0-c82a-69f7-291377a4feea.png)

# STEP: 20 文字列の長さ


## 問題
https://paiza.jp/works/mondai/prob60/substring_upper_2

## 解答

```java:step20.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        
        // 文字列の長さ取得
        int length = line.length();
        System.out.println(length);
        
        sc.close();
    }
    
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/76bc647d-4c91-8762-fef4-429f707aea08.png)

# STEP: 21 文字列の1文字目


## 問題
https://paiza.jp/works/mondai/prob60/substring_upper_3

## 解答

```java:step21.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // 文字を""で区切る
        sc.useDelimiter("");
        
        // 1文字目を取得
        String firstLetter = sc.next();
        
        System.out.println(firstLetter);
        
        sc.close();
    }
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/a0ddd9f4-843d-6b9a-701c-821f606c2cea.png)


# STEP: 22 大文字にする


## 問題
https://paiza.jp/works/mondai/prob60/substring_upper_4

## 解答

```java:step22.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        
        // 大文字に変換
        String uppercaseConversion = str.toUpperCase();
        
        System.out.println(uppercaseConversion);
        sc.close();
    }
}
```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/d1b2eec3-7421-f0f5-d2f0-ca6e9b5dead7.png)


# STEP: 23 あいだの整数


## 問題

https://paiza.jp/works/mondai/prob60/substring_upper_5

## 解答

```java:step23.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        printRangeOfNumbers(a, b);
        
        sc.close();
    }
    
    /**
     * a~bの数字を1行ずつ出力する関数
     * 
     * @param a 開始の数字
     * @param b 終了の数字
     * */
    public static void printRangeOfNumbers(int a, int b){
        for(int i = a; i <= b; i++){
            System.out.println(i);
        }
    }
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/a94c62f9-8ab3-9225-8cb9-c56262a18e16.png)


# STEP: FINAL問題 指定範囲だけ大文字


## 問題

https://paiza.jp/works/mondai/prob60/substring_upper_9

## 解答

```java:final.java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        // 次の行へ
        sc.nextLine();
        String line = sc.nextLine();
        
        System.out.println( convert(a, b, line));
        
        // test();
        
        sc.close();
    }

    /**
     * 変換関数
     * 
     * @param a 大文字に変換したい範囲の先頭
     * @param b 大文字に変換したい範囲の終了
     * @param list
     * 
     * @return 変換結果
     * */
    public static String convert(int a, int b, String line){
        
        // 前方
        String previous = "";
        // 後方
        String rear = "";
        
        // 大文字変換部位
        String uppercaseConversion = "";
        // 大文字返還後
        String uppercaseLetter = "";
        
        if(a == 1 && b != line.length( )){
            
            uppercaseConversion = line.substring(0, b);
            uppercaseLetter =  uppercaseConversion.toUpperCase();
            rear = line.substring(b);
            
        } else if (a != 1 && b ==  line.length()){
        
            previous = line.substring(0, a-1);
            uppercaseConversion = line.substring(a-1, b);
            uppercaseLetter =  uppercaseConversion.toUpperCase();
            
        } else if (a == 1 && b ==  line.length()){
        
            uppercaseConversion = line.substring(0, b);
            uppercaseLetter =  uppercaseConversion.toUpperCase();
        
            
        } else {
            
            previous = line.substring(0, a-1);
            uppercaseConversion = line.substring(a-1, b);
            uppercaseLetter =  uppercaseConversion.toUpperCase();
            rear = line.substring(b);
        
        }
        
       return previous + uppercaseLetter + rear;
    }

    // テストパターン
    private static List<List<String>> testLists =  Arrays.asList(
        Arrays.asList("1", "10", "Welcome to the paiza! I`m studying ruby!", "WELCOME TO the paiza! I`m studying ruby!"),
        Arrays.asList("2", "6", "Welcome to the paiza! I`m studying ruby!", "WELCOMe to the paiza! I`m studying ruby!"),
        Arrays.asList("1", "6", "Welcome to the paiza! I`m studying ruby!", "WELCOMe to the paiza! I`m studying ruby!"),
        Arrays.asList("1", "40", "Welcome to the paiza! I`m studying ruby!", "WELCOME TO THE PAIZA! I`M STUDYING RUBY!"),
        Arrays.asList("38", "40", "Welcome to the paiza! I`m studying ruby!", "Welcome to the paiza! I`m studying ruBY!")
        );
        
    /**
     * テストパターンを試すテストコード
     * */
	private static void test() {
	    for(List<String> testList : testLists){
	        int inputA = Integer.valueOf(testList.get(0));
	        int inputB = Integer.valueOf(testList.get(1));
	        String inputLine = testList.get(2);
	        String answer = testList.get(3);
	        
		    String result = convert(inputA, inputB, inputLine);
		   
            if(Objects.equals(result, answer)){
		        System.out.println("OK:"+ result);
		    } else {
		        System.out.println("NG:"+ result);
		    }
	    }
	}
	
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/5ae327fd-06e9-035e-582e-d4c6c08d2d81.png)