java.util.IllegalFormatConversionException: d != java.lang.String - java

I have problem when print output list of people by using ArrayList
package data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Manager {
List<Person> p = new ArrayList();
Scanner sc = new Scanner(System.in);
public void addStudent() {
String id;
String name;
int yob;
double point1;
double point2;
System.out.println("Input student id:");
id = sc.nextLine();
System.out.println("Input student name:");
name = sc.nextLine();
System.out.println("Input yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input point 1:");
point1 = Double.parseDouble(sc.nextLine());
System.out.println("Input point 2:");
point2 = Double.parseDouble(sc.nextLine());
p.add(new Student(id,name,yob,point1,point2));
}
public void addEmployee() {
String id;
String name;
int yob;
double salaryRatio;
double salary;
System.out.println("Input employee id:");
id = sc.nextLine();
System.out.println("Input employee name:");
name = sc.nextLine();
System.out.println("Input employee yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input salary ratio:");
salaryRatio = Double.parseDouble(sc.nextLine());
System.out.println("Input salary:");
salary = Double.parseDouble(sc.nextLine());
p.add(new Employee(id, name, yob,salaryRatio, salary));
}
public void addCustomer() {
String id;
String name;
int yob;
String companyName;
double bill;
System.out.println("Input customer id:");
id = sc.nextLine();
System.out.println("Input customer name:");
name = sc.nextLine();
System.out.println("Input customer yob:");
yob = Integer.parseInt(sc.nextLine());
System.out.println("Input compnay name:");
companyName = sc.nextLine();
System.out.println("Input bill:");
bill = Double.parseDouble(sc.nextLine());
p.add(new Customer(id,name,yob,companyName,bill));
}
public void addWho() {
int choice;
do {
System.out.println("1.Add Student");
System.out.println("2.Add Employee");
System.out.println("3.Add Customer");
System.out.println("4.Back to menu");
System.out.println("==============");
System.out.println("Choice:");
choice = Integer.parseInt(sc.nextLine());
switch(choice) {
case 1:
addStudent();
break;
case 2:
addEmployee();
break;
case 3:
addCustomer();
break;
case 4:
break;
}
}
while(choice != 4);
}
public void printPersonById() {
Collections.sort(p, Comparator.comparing(Person::getId));
for (int i = 0; i < p.size(); i++) {
System.out.println(p.get(i));
}
}
Student,Employee, and Customer are sub classes of class Person.When I try to print list,it has an error:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
How can I fix it?

Try this in your code:
System.out.println("Input yob:");
yob = sc.nextInt();
System.out.println("Input point 1:"); point1 =sc.nextDouble();
System.out.println("Input point 2:"); point2 = sc.nextDouble();
p.add(new Student(id,name,yob,point1,point2));

use System.out.println(p.get(i).someMethodOrVariableOfPerson); instead of System.out.println(p.get(i));
like below
public void printPersonById() {
Collections.sort(p, Comparator.comparing(Person::getId));
for (int i = 0; i < p.size(); i++) {
System.out.println("id :"+p.get(i).id);
System.out.println("name :"+p.get(i).name);
}
}
you can not directly print Person class object in out.println() method.
out.println() allow only string value

Related

cannot resolve or is not a field error

I am trying to search an arraylist of objects for an ID code, but I am stuck.
import java.util.Scanner;
import java.util.ArrayList;
public class Homework01{
public static void main(String[] args){
ArrayList<Transaction> argList = new ArrayList<Transaction>();
Scanner input = new Scanner(System.in);
System.out.println("Transaction List Menu");
System.out.println("=====================");
System.out.println("1) Add Transaction.");
System.out.println("2) Search Transactions.");
System.out.println("3) Filter.");
System.out.println("4) Display All Transactions.");
System.out.println("5) Exit.");
int menu = input.nextInt();
while (menu != 5) {
switch (menu) {
case 1:
addTransaction(argList);
break;
case 2:
;// Search Transaction
break;
case 3:
;// Filter Withdraws and Deposits
break;
case 4:
;// Display transactions
break;
case 5:
System.out.println("End");
break;
default:
System.out.println("Invalid response");
break;
}
menu = input.nextInt();
}
}
public static void addTransaction(ArrayList<Transaction> argList) {
Scanner input = new Scanner(System.in);
int tempId;
double tempAmount;
char tempType;
String tempDescription;
System.out.println("Enter in an ID for the transaction: ");
tempId = input.nextInt();
System.out.println("Enter in the amount of money: ");
tempAmount = input.nextDouble();
System.out.println("W for withdraw, D for deposit: ");
tempType = input.next(".").charAt(0);
System.out.println("Give transaction a description: ");
tempDescription = input.next();
//add transaction
argList.add(new Transaction(tempId, tempAmount, tempType, tempDescription) ); }
public static void searchTransactions(ArrayList<Transaction> argList){
Scanner input = new Scanner(System.in);
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for(int i=0;i<argList.size();i++){
if(argList.argId.get(i).contains(searchId)){
System.out.println("Yes");
}
}
}
}
My second file contains this
public class Transaction {
int id;
char type;
double amount;
String description;
public Transaction(int argId, double argAmount,char argType, String
argDescription){
id = argId;
type = argType;
amount = argAmount;
description = argDescription;
}
public void getId(int id){
}
public void getAmount(double amount){
}
public void getType(char type){
}
public void getDescription(String description){
}
}
And i get the error message: argId cannot be resolved or is not a field on line 58. I think my error is that argId is not part of the ArrayList, and i need to find the right tern to search the ID codes in the ArrayList.
Thanks
Earlier, before you edited your question, you had wrong getter methods.
Instead of
public void getId(int id){
}
you should write this:
public int getId() {
return id;
}
Declare your fields in Transaction class as private.
Then change your other getters in the similar way.
About your actual question, you can use for-each loop:
public static void searchTransactions(ArrayList<Transaction> argList) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for (Transaction transaction : argList) {
if (transaction.getId() == searchId) {
System.out.println("Yes");
break;
}
}
}
}
If you insist for i loop, change it this way:
for (int i = 0; i < argList.size(); i++) {
if(argList.get(i).getId() == searchId){
System.out.println("Yes");
break;
}
}
ArgId is a property of the objects in the list, not a property of the list itself which is why the compiler is giving you an error.
Looks like a typo:
You have:
argList.argId.get(i).contains(searchId)
try using:
argList.get(i).argId.contains(searchId)
argList is a collection, then you get object, read argId and check if it contains searchId
I would suggest to encapsulate class Transaction:
public class Transaction {
private int id;
private char type;
private double amount;
private String description;
public Transaction(int argId, double argAmount, char argType, String argDescription) {
id = argId;
type = argType;
amount = argAmount;
description = argDescription;
}
public int getId() {
return id;
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public String getDescription() {
return description;
}
}
Then main class will look in this way:
import java.util.Scanner;
import java.util.ArrayList;
public class Homework01{
public static void main(String[] args){
ArrayList<Transaction> argList = new ArrayList<Transaction>();
Scanner input = new Scanner(System.in);
System.out.println("Transaction List Menu");
System.out.println("=====================");
System.out.println("1) Add Transaction.");
System.out.println("2) Search Transactions.");
System.out.println("3) Filter.");
System.out.println("4) Display All Transactions.");
System.out.println("5) Exit.");
int menu = input.nextInt();
while (menu != 5) {
switch (menu) {
case 1: ; addTransaction(argList);
break;
case 2: ;// Search Transaction
break;
case 3: ;// Filter Withdraws and Deposits
break;
case 4: ;// Display transactions
break;
case 5: System.out.println("End");
break;
default: System.out.println("Invalid response");
break;
}
menu = input.nextInt();
}
}
public static void addTransaction(ArrayList<Transaction> argList) {
Scanner input = new Scanner(System.in);
int tempId;
double tempAmount;
char tempType;
String tempDescription;
System.out.println("Enter in an ID for the transaction: ");
tempId = input.nextInt();
System.out.println("Enter in the amount of money: ");
tempAmount = input.nextDouble();
System.out.println("W for withdraw, D for deposit: ");
tempType = input.next(".").charAt(0);
System.out.println("Give transaction a description: ");
tempDescription = input.next();
//add transaction
argList.add(new Transaction(tempId, tempAmount, tempType, tempDescription)
);
}
public static void searchTransactions(ArrayList<Transaction> argList){
Scanner input = new Scanner(System.in);
System.out.println("Please type in transaction ID: ");
int searchId = input.nextInt();
for(int i=0;i<argList.size();i++){
if(argList.get(i).getId()==searchId){
System.out.println("Yes");
}
}
}
}

Use of Private static

Can you find the source of error in this?
package calc;
import java.util.Scanner;
public class Calc {
Scanner scan = new Scanner(System.in);
public void add() {
System.out.println("Enter 1st number");
int s1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int s2 = scan.nextInt();
scan.nextLine();
int sum = s1 + s2;
System.out.println("The sum is: " + sum);
}
public void diff() {
System.out.println("Enter 1st number");
int d1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int d2 = scan.nextInt();
scan.nextLine();
int diff = d1 - d2;
System.out.println("The difference is: " + diff);
}
public void prod() {
System.out.println("Enter 1st number");
int p1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int p2 = scan.nextInt();
scan.nextLine();
int prod = p1 + p2;
System.out.println("The product is: " + prod);
}
public void quo() {
System.out.println("Enter 1st number");
int q1 = scan.nextInt();
scan.nextLine();
System.out.println("Enter 2nd number");
int q2 = scan.nextInt();
scan.nextLine();
int quo = q1 + q2;
System.out.println("The quotient is: " + quo);
}
public static void main(String[] args) {
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
char ans = 0;
System.out.println("Calculator");
System.out.println("1.Addition\n" + "2.Subtraction\n" + "3.Multiplication\n" + "4.Division\n" + "Enter operation number:");
int n1 = scan.nextInt();
scan.nextLine();
switch (n1) {
case 1:
op.add();
break;
case 2:
op.diff();
break;
case 3:
op.prod();
break;
case 4:
op.quo();
break;
default:
System.out.println("Invalid input");
break;
}
System.out.println("Try again? [Y/N]");
ans = scan.nextLine().charAt(0);
} while (ans == 'Y' || ans == 'y');
}
}
and then netbeans has this auto correct that resulted into this:
package calc;
import java.util.Scanner;
public class Calc {
private static char ans;
it added a "private static char ans;" and I would like to understand more how did that fix my code. Thanks
ans is defined within the do{ ... } while() loop but it must be defined outside, to make it available for condition in the while.
So do:
char ans = 0;
do {
Calc op = new Calc();
Scanner scan = new Scanner(System.in);
ans = 0;

the program is not executing as it should be?

this is my code
"the full code"
import java.util.Scanner;
import java.util.ArrayList;
public class RestTestGhada {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
RestGhada obj2 = new RestGhada("null",0,0,"null");
RestGhada obj3 = new RestGhada();
RestGhada obj4 = new RestGhada();
DateGhada obj5 = new DateGhada(19,8,1998);
DateGhada obj6 = new DateGhada(19,8,2011);
WorkerGhada obj7 = new WorkerGhada("Ghada","Alghamdi",obj5,obj6,"Female");
RestGhada obj8 = new RestGhada(12,"null",20,obj7);
obj2.setID(123456789);
System.out.print("Enter the number of employee: ");
int noemp=input.nextInt();
String empname[] = new String[5];
System.out.print("Enter the employee names: ");
for(int i=0;i<empname.length;i++)
empname[i]=input.nextLine();
obj2.setEmployeeName(empname);
System.out.print("**Menu** \n"
+ "1.Create three objects of restaurant.\n"
+ "2.Display all restaurants information.\n"
+ "3.Display the checkRestaurant result. \n"
+ "4.Exit.\n"
+ "Your choice is: ");
int choice =input.nextInt();
while(choice!=4){
switch(choice){
case 1: obj3.setName("null");obj3.setID(choice);obj4.setNoEmployee(noemp); break;
case 2: obj2.lastprint(); break;
case 3: obj2.CheckRestaurant(noemp); break;
}
System.out.print("\n Your choice: ");
choice=input.nextInt();
}
CorporateGhada[] ArrGroups=new CorporateGhada[4];
RestGhada obj1 = new RestGhada("KFC");
ArrayList<RestGhada>ALRestaurant=new ArrayList();
System.out.println("do you want to enter information?");
String c = input.nextLine();
while (true){
if(c.equals("no")||c.equals("No"))
break;
System.out.print("Menu\n"
+ "1.enter ID,name,number of employee,owner\n"
+ "2.enter ID,adress,name,owner,number of employee\n"
+ "your choice : ");
int choice1 =input.nextInt();
if(choice1 == 1){
System.out.println("Enter ID") ;
int ID=input.nextInt();
System.out.println("Enter name ") ;
String name=input.next();
System.out.println("nbemployee:") ;
int nbemployee =input.nextInt();
System.out.println("Enter owner:") ;
String owner=input.next();
ALRestaurant.add(new RestGhada(name,ID,nbemployee,owner));
}
else if (choice1 == 2){
System.out.println("Enter ID:");
int ID=input.nextInt();
System.out.println("Enter adrees:") ;
String adrees=input.next();
System.out.println("Enter name :");
String name=input.next();
System.out.println("Enter owner:") ;
String owner=input.next();
System.out.println("Enter nbemployee:");
int nbemployee=input.nextInt();
ALRestaurant.add(new RestGhada(ID,adrees,name,owner,nbemployee));
}
else
ALRestaurant.add(new RestGhada());
}
ALRestaurant.remove(1);
for(int i=0; i<ALRestaurant.size();i++ ){
System.out.print(ALRestaurant.get(i));}
System.out.print(ALRestaurant.contains(obj1));
}
}
when i try to run the first quistion doesn't take an answer and it get into the while loop directly.. i already take an information from the user before and it worked but now it's now..
when i run it it turns out like this :
do you want to enter information?
Menu
1.enter ID,name,number of employee,owner
2.enter ID,adress,name,owner,number of employee
your choice :
Did you type anything in the input before the first question was asked?
When I just run this (tried to turn your code into a working example) it waits for me to input:
public static void main(String[] args) {
ArrayList<RestGhada> ALRestaurant = new ArrayList();
Scanner input = new Scanner(System.in);
System.out.println("do you want to enter information?");
String c = input.nextLine();
while (true) {
if (c.equals("no") || c.equals("No"))
break;
System.out.print("Menu\n"
+ "1.enter ID,name,number of employee,owner\n"
+ "2.enter ID,adress,name,owner,number of employee\n"
+ "your choice : ");
int choice1 = input.nextInt();
if (choice1 == 1) {
System.out.println("Enter ID");
int ID = input.nextInt();
System.out.println("Enter name ");
String name = input.next();
System.out.println("nbemployee:");
int nbemployee = input.nextInt();
System.out.println("Enter owner:");
String owner = input.next();
ALRestaurant.add(new RestGhada(name, ID, nbemployee, owner));
} else if (choice1 == 2) {
System.out.println("Enter ID:");
int ID = input.nextInt();
System.out.println("Enter adrees:");
String adrees = input.next();
System.out.println("Enter name :");
String name = input.next();
System.out.println("Enter owner:");
String owner = input.next();
System.out.println("Enter nbemployee:");
int nbemployee = input.nextInt();
ALRestaurant.add(new RestGhada(ID, adrees, name, owner, nbemployee));
} else
ALRestaurant.add(new RestGhada());
}
}
private static class RestGhada {
public RestGhada(int id, String adrees, String name, String owner, int nbemployee) {
}
public RestGhada() {
}
public RestGhada(String name, int id, int nbemployee, String owner) {
}
}
In your code you haven't included your initialisation of the input object.
If you add the line
Scanner input = new Scanner(System.in);
In the beginning of your program it should work.
I ran your program with that line added and it worked.
Besides that you should really look up on correct indentation.
change to this code.
System.out.println("do you want to enter information (yse/no) ? :");
String c = input.nextLine();
while (true) {
if (c.equals("no") || c.equals("No")){
break;
}
else{
System.out.println("Enter Your Choice(1 or 2) :");
int choice1 = input.nextInt();
if (choice1 == 1) {
System.out.println("Enter ID");
int ID = input.nextInt();
System.out.println("Enter name ");
String name = input.next();
System.out.println("nbemployee:");
int nbemployee = input.nextInt();
System.out.println("Enter owner:");
String owner = input.next();
ALRestaurant.add(new RestGhada(name, ID, nbemployee, owner));
} else if (choice1 == 2) {
System.out.println("Enter ID:");
int ID = input.nextInt();
System.out.println("Enter adrees:");
String adrees = input.next();
System.out.println("Enter name :");
String name = input.next();
System.out.println("Enter owner:");
String owner = input.next();
System.out.println("Enter nbemployee:");
int nbemployee = input.nextInt();
ALRestaurant.add(new RestGhada(ID, adrees, name, owner, nbemployee));
} else{
ALRestaurant.add(new RestGhada());
}
}
}

How to connect Sequencial Search to an array and Main Menu

I am trying to connect the sequential search function to the menu so that it accepts user input and search the tp arrays in apartA() and apartB(). And also how can we connect the report function to the values in Functions apartA() and apartB().
package jachi;
import java.util.Scanner;
public class Jachi {
public static void main(String[] args) {
menu();
}
public static void menu(){
int j,k,choice;
System.out.println("Menu");
System.out.println("1. Register");
System.out.println("2. Update");
System.out.println("3. Report");
System.out.println("4. Search");
Scanner scan=new Scanner(System.in);
choice=scan.nextInt();
switch(choice) {
case 1:
System.out.println("Select Apartment type");
System.out.println("1. Apartment A");
System.out.println("2. Apartment B.");
choice=scan.nextInt();
if (choice==1)
{
apartA();
}
else if (choice==2)
{
apartB();
}
break;
case 2:
System.out.println("Update the student information");
break;
case 3:
System.out.println("Rooms Available");
report();
break;
case 4:
System.out.print("Search for student. Please enter the TP number:");
break;
default:
System.out.println("Incorrect input");
}
}
public static int apartA()
{
final int a = 9;
//Normal Rooms Arrays
int[] tp;
tp = new int[a];
String[] stname;
stname = new String[a];
String[] mob;
mob = new String[a];
int ttt = get.nextInt();
int test = sequencialSearch(ttt);
Scanner get = new Scanner(System.in);
//Data Entry
for (int index = 0; index < a; index++)
{
System.out.println("Student Information");
System.out.println("Enter the student TP NUMBER:");
tp[index] = get.nextInt();
System.out.println("Enter the STUDENT NAME:");
stname[index] = get.nextLine();
System.out.println("Enter the MOBILE NUMBER:");
mob[index] = get.nextLine();
rent();
}
System.out.println("Rooms Full");
/**Report Data, Test for Data presence
*for(a=0;a<=2;a++)
*{
*System.out.println("TP NUMBER: "+tp[a]+"\t NAME: "+stname[a]+"\t MOBILE: "+mob[a]);
*}*/
return a;
}
public static void apartB(){
System.out.println("Select Room Type");
System.out.println("\t1. Normal Room");
System.out.println("\t2. Master Room");
int choice;
final int j = 6;
final int k = 3;
Scanner get = new Scanner(System.in);
choice = get.nextInt();
Scanner get1 = new Scanner(System.in);
if (choice==1)
{
System.out.println("1. Normal Bedroom");
// Normal Bedroom Arrays
String[] tp;
tp = new String[j];
String[] stname;
stname = new String[j];
String[] mob;
mob = new String[j];
//Data Entry
for (int index = 0; index < j; index++)
{
System.out.println("Student Information");
System.out.println("Enter the student TP NUMBER:");
tp[index] = get1.nextLine();
System.out.println("Enter the STUDENT NAME:");
stname[index] = get1.nextLine();
System.out.println("Enter the MOBILE NUMBER:");
mob[index] = get1.nextLine();
rent();
}
if(j<=5)
{
menu();
}
else
{
System.out.println("All Houses Full");
}
}
else if (choice==2)
{
System.out.println("Master bedroon");
//Master Bedroom Arrays
String[] mastertp;
mastertp = new String[k];
String[] mastername;
mastername = new String[k];
String[] mastermob;
mastermob = new String[k];
//Data Entry
for (int index = 0; index < k; index++)
{
System.out.println("Student Information");
System.out.println("Enter the student TP NUMBER:");
mastertp[index] = get1.nextLine();
System.out.println("Enter the STUDENT NAME:");
mastername[index] = get1.nextLine();
System.out.println("Enter the MOBILE NUMBER:");
mastermob[index] = get1.nextLine();
int x,y,z,pay;
x=100;
pay=300;
y=3*pay;
z=x+y;
System.out.println("Charges to be Paid");
System.out.println("Charges Amount");
System.out.println("Utilities charge: rm100");
System.out.println("Three month rental: rm"+y);
System.out.println("Total: rm"+z);
menu();
}
System.out.println("Full House Man");
}
}
public static void rent(){
int x,y,z,pay;
x=100;
pay=300;
y=3*pay;
z=x+y;
System.out.println("Charges to be Paid");
System.out.println("Charges Amount");
System.out.println("Utilities charge: rm100");
System.out.println("Three month rental: rm"+y);
System.out.println("Total: rm"+z);
menu();
}
public static void report(){
int x,y,z;
x=0;
y=x+1;
z=6-y;
System.out.println("Rooms occupied; "+y);
System.out.println("Rooms available; "+z);
menu();
}
public static int sequencialSearch(int Tnumbr){
int index,
element;
boolean found;
index = 0;
element = 0;
found = false;
while(!found && index == Tnumbr){
/* if(array[index] == 9{
* found = true;
* element = index;
* }
* index++;
*/
}
return element;
}
}
Ok that is a lot of code, lets try breaking it into simpler classes.
first would be Apartment. Lets have a class for apartment and put all the utility methods like report() and search() into it.
The apartment class can be something like:
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
class Apartment{
/** Integer: roomid, String StrudentId **/
HashMap<Integer, String> rooms = new HashMap<Integer, String>();
public Apartment(){
}
public Integer sequentialSearch(String tpNumber){
/** Returns null if student is absent **/
for(Integer roomId : rooms.keySet())
{
if(tpNumber.equals(rooms.get(roomId)))
return roomId;
}
return null;
}
public void report(){
System.out.println("ROOM_NUM\tSTUDENT_ID");
for(Integer roomId : rooms.keySet())
{
System.out.println(roomId +"\t" + rooms.get(roomId));
}
}
}
Now in your Main class Jachi you can create and use the apartments like:
Apartment apartA = new Apartment();
apartA.sequentialSearch("SOME_STUDENTID_HERE");
apartA.report();
Apartment apartB = new Apartment();
apartB.sequentialSearch("SOME_STUDENTID_HERE");
apartB.report();
You can also create Arrays of apartments now. Rest is left for you to explore.

ArrayList java .. I have an error

import java.util.ArrayList;
import java.util.Scanner;
public class StudentList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
int choice;
printMenu();
do {
Scanner input = new Scanner(System.in);
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("\nAdd a student\n");
students.add(addStudent());
printMenu();
break;
case 2:
System.out.println("\nFind a student\n");
findStudent(students);
printMenu();
break;
case 3:
System.out.println("\nDelete a student\n");
displayAllStudents(students);
printMenu();
break;
case 4:
System.out.println("\nDispay all students\n");
displayAllStudents(students);
printMenu();
break;
case 5:
System.out.println("\nDisplay the total number of students\n");
studentSize(students);
printMenu();
break;
case 6:
System.out.println("\nGoodbye!\n");
break;
default:
System.out
.println("\nYour choice," + choice + ", is invalid\n");
break;
}
} while (choice != 6);
}
public static void printMenu() {
System.out
.println("\nPlease select from the following menu:\n"
+ "\t1. Add a student\n" + "\t2. Find a student\n"
+ "\t3. Delete a student\n"
+ "\t4. Display all students\n"
+ "\t5. Display the total number of students\n"
+ "\t6. Exit\n");
System.out.print("Your choice: ");
}
public static Student addStudent() {
Scanner inputS = new Scanner(System.in);
System.out.println("First Name:");
String fName = inputS.nextLine();
System.out.println("Last Name:");
String lName = inputS.nextLine();
System.out.println("Major Name:");
String major = inputS.nextLine();
System.out.println("Student Name:");
Integer sNumber = inputS.nextInt();
System.out.println("gpa:");
double grade = inputS.nextDouble();
return new Student(fName, lName, Major, sNumber, grade);
}
public static void displayAllStudents(ArrayList<Student> students) {
for (Student s : students) {
System.out.print(s.toString());
}
}
public static void findStudent(ArrayList<Student> students) {
Scanner inputN = new Scanner(System.in);
String name = inputN.nextLine();
for (Student s : students) {
if (s.getFName().equals(name)) {
System.out.println(s);
break;
}
}
}
public static void deleteStudent(ArrayList<Student> students) {
Scanner inputS = new Scanner(System.in);
String sNum = inputS.next();
for (Student s : students) {
if (s.FName().equals(sNum)) {
students.remove(s);
break;
}
}
}
public static void studentSize(ArrayList<Student> students) {
System.out.printf("Size: %\n", students.size());
}
}
i cannot figure out what is wrong with my code
it gives me an error on these three things when i try to compile
return new Student(fName, lName, Major, sNumber, grade);
if(s.FName().equals(sNum))
if(s.getFName().equals(name))
First:
return new Student(fName, lName, Major, sNumber, grade);
Major is not a variable, its major.
Second:
if(s.FName().equals(sNum))
it should be
if(s.getFName().equals(sNum))
UPDATE
public static Student addStudent() {
Scanner inputS = new Scanner(System.in);
System.out.println("First Name:");
String fName = inputS.nextLine();
System.out.println("Last Name:");
String lName = inputS.nextLine();
System.out.println("Major Name:");
String major = inputS.nextLine(); //variable name: major
System.out.println("Student Name:");
Integer sNumber = inputS.nextInt();
System.out.println("gpa:");
double grade = inputS.nextDouble();
//variable sent: Major, it should be major
return new Student(fName, lName, Major, sNumber, grade);
}
at one place you are
s.FName()
and at another
s.getFName()
Also following would give you error at rumtime
for(Student s: students)
{
if(s.FName().equals(sNum))
{
students.remove(s);
break;
}
}
You can't delete an object from array, while looking at it.
You should use iterator.
Like this:
for (Iterator<Student> it = students.iterator(); it.hasNext(); )
{
Student student = (Student)it.next();
if (!s.getFName().equals(sNum))
{
tmp.add(student);
}
lines.clear();
for (Student t: tmp)
students.add(t);
tmp.clear();
}

Categories