Java: How to refactor my code to reduce redundancy?

Student

//Here's my code, I'm asking if I could repeat steps for m without having to rewrite.

Scanner input = new Scanner(System.in);
        System.out.print("First 3-digit number: ");
        int n = input.nextInt();
        System.out.print("Second 3-digit number: ");
        int m = input.nextInt();
        
        
        int n3 = n % 10;
        int n2 = n / 10 % 10;
        int n1 = n / 100 % 10;

        if (n1 == n3) {
            System.out.println( "yes");
        } else {
            System.out.println( "No");
Gursewak Singh

As Locke mentioned in the comment, you can use function to refactor your code to reduce the redundancy. For example, I would create a function like so:

public static int getInput(String msg)
{
     System.out.print(msg);
     int m = input.nextInt();
     return m;
}

and use that function in your code like so:

Scanner input = new Scanner(System.in);
    int n = getInput("First 3-digit number: ");
    int m = getInput("Second 3-digit number: ");
    
    
    int n3 = n % 10;
    int n2 = n / 10 % 10;
    int n1 = n / 100 % 10;

    if (n1 == n3) {
        System.out.println( "yes");
    } else {
        System.out.println( "No");

I hope, you got the idea.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to reduce the verbose redundancy of java thread local

From Dev

How to reduce the verbose redundancy of java thread local

From Dev

Reduce code redundancy

From Dev

How to refactor this java code

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

Reduce the code redundancy in angularjs function

From Dev

How to reduce repeated code in my lists of elements to hide (Java/webDriver)

From Dev

How to refactor a repetitive line of code java

From Dev

Unnecessary unboxing in java - how to refactor the code?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How to remove redundancy in Lisp code?

From Dev

How to measure redundancy in code bases?

From Dev

How to refactor this code in Haskell

From Dev

How to refactor this code in Haskell

From Dev

How to refactor this messy code

From Dev

Reduce code redundancy when conditionally loading Reactjs components

From Dev

How to refactor this HashSet in Java

From Dev

How to avoid redundancy in my database model?

From Dev

Database design, How to reduce the data redundancy for different column

From Dev

How to refactor this Redux connect code?

From Dev

How to refactor this clojure / riemann code

From Dev

How to refactor this clojure / riemann code

From Dev

How do I remove redundancy in this clojure code?

From Dev

How do I remove redundancy in this clojure code?

From Dev

Exceptions in java: how reduce repeated code

From Dev

How can ViewModels reduce code and logic within my view?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How can I improve my code to reduce the synthesis time?

From Dev

How to refactor this code OOP code for an interview?

Related Related

HotTag

Archive