简体   繁体   中英

How do I cancel a for loop in java?

The goal of my program is to store a default of 100 Employee objects(but should work with less) to an array after prompting the user input of each employee's name, ssn, and salary with a loop.I then need to output the contents of the array. I am trying to end the loop when the user inputs a 0 when asked for a name, but so far my program seems to loop endlessly.

Driver class:

import java.util.Scanner;

public class EmployeeDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner kb = new Scanner(System.in);

        Employee employees[]= new Employee[100];
        String nameTest="";
        for(int i=0;i<100;i++){
            Employee e = new Employee();

        System.out.println("Enter Employee name, Ssn, and salary when prompted");
        System.out.println("When finished type in 0's for each prompt");
        System.out.println("Enter Name: ");
        nameTest=kb.next();
        e.setName(nameTest);
        System.out.println("Enter Ssn: ");
        e.setSsn(kb.next());
        System.out.println("Enter Salary: ");
        e.setSalary(kb.nextDouble());

        if(nameTest=="0"){
            i=100;
        }
        else
            employees[i]=e;
        }

        for(int x=0;x<100;x++){
        employees[x].toString();
        }
    }

}

Methods:

public class Employee extends Person {

    String ssn;
    double salary;
    public String getSsn() {
        return ssn;
    }
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Employee(String initialName,String initialSsn,double initialSalary) {
        super(initialName);
        ssn=initialSsn;
        salary=initialSalary;
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Name: "+getName()+" Ssn: "+getSsn()+" Salary: "+getSalary();
    }
}

You should be using equals() to compare strings rather than == , as in the following line:

if (nameTest == "0")

In order to acheive what you want, you'd use something along the following lines:

System.out.println("Enter Name: ");
nameTest = kb.next();
if (nameTest.equals("0")) break;      // <- add this line here.

This has the advantage of only requiring you to enter 0 for the employee name rather than all three values having to be entered.

Please follow as below

== will test for reference equality only.

.equals() tests for value if equal.

So replace if (nameTest == "0") with if (nameTest.equals("0"))

First, you are checking an int against a String , which will not work. You should change that to if (nameTest.equals("0") ) . Then you can use break to break out of the loop. No need to make i=100 .

There is some problem in this code snippet:

if(nameTest=="0"){
    i=100;
}

You should always use "equals" method instead of "==" when judge the value of String to be equal. Accordingly, write the code like this:

if (nameTest.equals("0")) {
    i = 100;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM