-1

I need to create a program of which one of the options should be a project deadline. I have a class called NewProject then in the ProjectOptions file there is an option to make changes to the due date. Here is where the problem lies. I need to take a project number (projNum) input, then search for it in the array list then allow the user to change the due date. How do I go about doing this?

The class is programmed as follows:

package poisedProjects;

public class NewProject {
    
//Atributes
    
    String projNum;
    String projName;
    String projType;
    String projAddress;
    String erfNum;
    String projCost;
    String projPaid;
    String projDeadLine;
    
    // Methods
    public NewProject(String projNum, String projName, String projType, String projAddress, String erfNum, String projCost, String projPaid, String projDeadLine) {
      this.projNum = projNum;
      this.projName = projName;
      this.projType = projType;
      this.projAddress = projAddress;
      this.erfNum = erfNum;
      this.projCost = projCost;
      this.projPaid = projPaid;
      this.projDeadLine = projDeadLine;
    }
    
    //Methods(toString)
    public String toString() {
        String output = "Project Number: " + projNum;
        output += "\nProject Name: " + projName;
        output += "\nProject Type: " + projType;
        output += "\nProject Address: " + projAddress;
        output += "\nERF Number: " + erfNum;
        output += "\nProject Total Cost: " + projCost;
        output += "\nProject Cost Paid: " + projPaid;
        output += "\nProject Deadline: " + projDeadLine;
        return output;
    }
    
    public boolean contains(String search) {
         return this.projNum.equals(search) || this.projName.equals(search) || this.projType.equals(search) || this.projAddress.equals(search) || this.erfNum.equals(search) || this.projCost.equals(search) || this.projPaid.equals(search) || this.projDeadLine.equals(search);
    }

}

The ProjectOptions file contains the following:

...
import java.util.ArrayList;
import java.util.Scanner;

public class ProjectOptions {
    public static void main(String[] args){
        
        final Scanner scan = new Scanner(System.in);
        ArrayList<NewProject> newProjects = new ArrayList<NewProject>();
...

        else if(answer == 2) {  
            
            System.out.println("Enter the Project Number: \n");
            String projNum = scan.toString();
            
            boolean projfound = false;
            
            while(projfound == false) {
                
                boolean currentProj = newProjects.contains(projNum);
                String currentProj2 = String.valueOf(currentProj);
                
                if(currentProj2 == projNum) {
                    
                    System.out.println("Enter new Due Date: \n");
                    String projDeadLine = scan.toString();
                    
                    System.out.println("The new Due Date is: \t" + projDeadLine );
                    
                    }
                }
            }

Thank you in advance for the help, I really appreciate it.

  • What issue are you currently facing right now? FYI for string comparison you will want to use .equals(). – Albert Jan 05 '21 at 16:35
  • The `projNum` is boolean so the following condition would be always false: ``` if(currentProj2 == projNum) ... ``` I don't think you would even need any condition there at all, just remove it and it should just work. – Soroosh Sarabadani Jan 05 '21 at 16:55

1 Answers1

0

So i think the .equals() - method for String comparison should work.

simple example:

    List<String> stringList = new ArrayList<>();
    stringList.add("20");
    stringList.add("33");
    stringList.add("50");



    public static void findStringInStringList(String str) {
    for (int i = 0; i < stringList.size(); i++) {
        if (stringList.get(i).equals(str)) {
            // do something
            System.out.println("Match");
        } else {
            System.out.println("No Match");
         }
       }
     }    
    
Hubi
  • 440
  • 1
  • 11
  • 25