Home > other >  Is there any other way of code for the methods? It asks for user input n times based on a certain nu
Is there any other way of code for the methods? It asks for user input n times based on a certain nu

Time:05-21

I'm making this Study Schedule program and a part of it is where I ask how many Subjects does the user have, and then they say (for example) 5. Based on that number of subjects it will ask "What is the name of the nth subject" for how many subjects there are.

I basically have the code written down already and it works, but is there anyway to simplify this? It feels chunky & repetitive. Also, method names don't matter since this is just for testing.

import java.util.*;

class NumberInput {
    public double NumberInput;
    public String SubjectName1;
    public String SubjectName2;
    public String SubjectName3;
    public String SubjectName4;
    public String SubjectName5;
    public String SubjectName6;

    public static void main(String[] args) {
        NumberInput wow = new NumberInput();
        wow.InputNumber();
        wow.InputString();
        wow.confirmResults();
    }

    public double DoubleInput(String numberQuestion) {
        Scanner input = new Scanner(System.in);
        System.out.println(numberQuestion);
        return input.nextDouble();
    }

    public String StringInput(String stringQuestion) {
        Scanner input = new Scanner(System.in);
        System.out.println(stringQuestion);
        return input.nextLine();
    }

    public double InputNumber() {
    NumberInput = DoubleInput("How many subjects would you like to study?");
    if (NumberInput > 6) {
        System.out.println("Maximum Subjects exceeded. At the most, please answer 6");
    }
    return (NumberInput);
    }

    public void InputString() {
    if (NumberInput == 1) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
    }

    if (NumberInput == 2) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
    }

    if (NumberInput == 3) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
    }

    if (NumberInput == 4) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
    }

    if (NumberInput == 5) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
        SubjectName5 = StringInput("What is the name of the fifth Subject: ");
    }

    if (NumberInput == 6) {
        SubjectName1 = StringInput("What is the name of the Subject: ");
        SubjectName2 = StringInput("What is the name of the second Subject: ");
        SubjectName3 = StringInput("What is the name of the third Subject: ");
        SubjectName4 = StringInput("What is the name of the fourth Subject: ");
        SubjectName5 = StringInput("What is the name of the fifth Subject: ");
        SubjectName6 = StringInput("What is the name of the sixth Subject: ");
    }

    }

    public void confirmResults() {
        System.out.println(SubjectName1);
        System.out.println(SubjectName2);
        System.out.println(SubjectName3);
        System.out.println(SubjectName4);
        System.out.println(SubjectName5);
        System.out.println(SubjectName6);
    }

}

CodePudding user response:

public String SubjectName1;
public String SubjectName2;
public String SubjectName3;
public String SubjectName4;
public String SubjectName5;
public String SubjectName6;

Could be an array of Strings:

String[] SubjectName = new String[6];

Also:

 public double InputNumber()

Is returning a number that is not being assigned when it is called:

//This Line calls a function which returns a double, but you don't store the double
wow.InputNumber();

//You need something like: 
double NumInputs = wow.InputNumber();

also:

public void InputString()

Can be a loop with numInputs:

//You dont really need to capture numInputs and pass it this way.
//You could just use the NumberInput you're setting in the function, but since you return a double I'm showing you the syntax for that.
public static void main(String[] args) {
    NumberInput wow = new NumberInput();
    double numInputs = wow.InputNumber();
    wow.InputString(numInputs);
    wow.confirmResults();
}
public void confirmResults(double numInputs){
    for(int i = 1; i<= numInputs; i  ){
       System.out.println("What is the subject?"); 
       //etc
    }
}

confirmResults(), this can be a loop over an array of SubjectNames.

CodePudding user response:

A Loop and an Enum

You can use an enum to get values like 'first', 'second' etc when you are looping over the number of subjects ...

Here's a rather basic example:

public class Test {

    public static void main(String[] args) {
        int numberOfSubjects = inputNumber();
        String[] subjectNames = new String[numberOfSubjects - 1];
        for (int i = 0; i < numberOfSubjects; i  ) {
            Scanner input = new Scanner(System.in);
            System.out.printf("What is the name of the %s Subject%n", Number.values()[i].getCount());
            subjectNames[i] = input.nextLine();
        }
        confirmResults();
    }

    private static void confirmResults() {
        // Your implementation
    }

    private static Integer inputNumber() {
        // You implementation
        return 5;
    }
}

enum Number {
    FIRST("first"),
    SECOND("second"),
    THIRD("third"),
    FOURTH("fourth"),
    FIFTH("fifth");
    
    private final String count;

    Number(String count) {
        this.count = count;
    }

    public String getCount() {
        return count;
    }
}

You should always care about naming stuff, especially when you want people to review your code. That said, I could have probably found a better name than 'Number' for this enum. XD

Naming stuff is the hardest part of programming, best to start early to make it a habit.

  • Related