E
E
Elxan Mecidli2021-04-19 11:44:04
Java
Elxan Mecidli, 2021-04-19 11:44:04

Wrote a method that finds the middle character of a number. Why doesn't the method return a.charAt(d)?

package com.company;

import java.util.Scanner;

public class Exercise_3 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Введите число");
        String a=sc.nextLine();
        Number(a);
    }
    public static String Number(String a) {

        if (a.length() % 2 != 0) {
            int d = a.length() / 2 + 1;
return a.charAt(d);
        } else {
            int d = a.length() / 2 - 1;
            return (a.substring(d, d + 2));
        }
         }
}

Error in return a.charAt(d);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Araya, 2021-04-19
@Elxan24-03

The return value does not match.
charAt() - returns char, String is specified in the method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question