How could i store the below code in an array list using Java? Im stuck. Your help will be greatly appreciated.
System.out.println("Please enter Officer's First Name");
firstName = input.next();
officerObject.setOfficerFirstName(firstName);
System.out.println("Enter Officer's Last Name");
lastName = input.next();
officerObject.setOfficerLastName(lastName);
System.out.println("Enter Officer's Badge Number");
badgeNum = input.next();
officerObject.setOfficerBadgeNum(badgeNum);
System.out.println("Enter Officer's Precint");
precint = input.next();
officerObject.setOfficerPrecint(precint);
Create an array list with List<YOUR OBJECT TYPE> array = new ArrayList<>(); and then use array.add(officerObject); to add it to the array list.
Well as he said use a List look
First it could be use like this:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Officer officerObject = new Officer();
System.out.println("Please enter Officer's First Name");
officerObject.setOfficerFirstName(input.next());
System.out.println("Enter Officer's Last Name");
officerObject.setOfficerLastName(input.next());
System.out.println("Enter Officer's Badge Number");
officerObject.setOfficerBadgeNum(input.next());
System.out.println("Enter Officer's Precint");
officerObject.setOfficerPrecint(input.next());
List<Officer> officer = new ArrayList<Officer>();
officer.add(officerObject);
for(Officer of: officer){
System.out.println(of.getOfficerFirstName());
System.out.println(of.getOfficerLastName());
System.out.println(of.getOfficerBadgeNum());
System.out.println(of.getOfficerPrecint());
}
Related
import java.util.Scanner;
public class MadLibs {
public static void main(String[] args) {
String name, place, college, profession, animal, petName;
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
name = keyboard.nextLine();
System.out.print("Enter a number: ");
number = keyboard.nextInt();
System.out.print("Enter a place: ");
place = keyboard.nextLine();
System.out.print("Enter a college: ");
college = keyboard.nextLine();
System.out.print("Enter a profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
petName = keyboard.nextLine();
keyboard.close();
}
}
console results
Can't figure out why "enter a college" and "enter a place" are printing on the same line and not acting as separate inputs.
Try below snippet it will definitely work :
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
String name = keyboard.nextLine();
System.out.print("Enter a number: ");
int number = keyboard.nextInt();
System.out.print("Enter a place: ");
keyboard.next();
String place = keyboard.nextLine();
System.out.print("Enter a college: ");
keyboard.next();
String college = keyboard.nextLine();
System.out.print("Enter a profession: ");
String profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
String animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
String petName = keyboard.nextLine();
}
The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.
enter code here
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String name, place, college, profession, animal, petName;
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
name = keyboard.nextLine();
System.out.print("Enter a number: ");
number = keyboard.nextInt();
System.out.print("Enter a place: ");
place = keyboard.nextLine();
keyboard.nextLine(); //brings to next line
System.out.print("Enter a college: ");
college = keyboard.nextLine();
System.out.print("Enter a profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
petName = keyboard.nextLine();
keyboard.close();
}
}
I am working on the parking ticket simulator of the starting out with Java book, so the idea is to ask the user to input the officer's name, officer's badge number and some other other information, I have that information in the class ParkingCarSimulator class located on ParkingCarSimulator.java,
All of that is working fine, now in class called PoliceOfficer located in PoliceOfficer.java file, I would like to know if I can access the user input from the main method in the ParkingCarSimulator into the PoliceOfficer class.
Any ideas will be appreciated, here is the code I have:
import java.util.Scanner;
public class ParkingCarSimulator {
public static void main(String[] arsg)
{
String officerName, Make, carModel, carColor, carLicense;
int badgeNumber, minOnCar, minPurchased;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the officer's name");
officerName = keyboard.nextLine();
System.out.println("Enter the officer's badge number");
badgeNumber = keyboard.nextInt();
System.out.println("Enter the car's make");
Make = keyboard.nextLine();
System.out.println("Enter the car's model");
carModel = keyboard.nextLine();
System.out.println("Enter the car's color");
carColor = keyboard.nextLine();
System.out.println("Enter the car's liscence number");
carLicense = keyboard.nextLine();
System.out.println("Enter minutes on car");
minOnCar = keyboard.nextInt();
System.out.println("Enter the number of minutes purchased");
minPurchased = keyboard.nextInt();
}
}
Here is PoliceOfficer.java
public class PoliceOfficer
{
String policeName = ParkingCarSimulator.officerName;(this throws an error)
}
To create a PoliceOfficer, you need to give it a constructor then instanciate it in the main code
public class PoliceOfficer{
String policeName;
int badgeNumber;
public PoliceOfficer(String policeName, int badgeNumber){
this.policeName = policeName;
this.badgeNumber = badgeNumber;
}
}
use
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the officer's name");
String officerName = keyboard.nextLine();
System.out.println("Enter the officer's badge number");
int badgeNumber = keyboard.nextInt();
PoliceOfficer po = new PoliceOfficer(officerName, badgeNumber);
}
Im still learning java and i want to understand more about array. Im still in school and we were asked to create an asean phonebook which can store, edit, delete, and view/search the information that are inputted. Im still doing the storing of information block of code and im struggling how to save multiple input in an array that is limited only to 100 contacts and can be searched later in the code. Can someone help me with my task and make me understand??
import java.util.Scanner;
public class Phonebook
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int i = 0;
String studentNumber, surName, firstName, occuPation, countryCode, areaCode, numBer;
char genDer;
do
{
Menu();
System.out.println(" ");
System.out.print("Go to: ");
String choice = input.next();
if (choice.equals("1") || choice.equals("2") || choice.equals("3") || choice.equals("4") || choice.equals("5"))
{
i++;
switch(choice)
{
case "1":System.out.println("Enter student number: ");
studentNumber = input.next();
System.out.println("Enter surname: ");
surName = input.next();
System.out.println("Enter first name: ");
firstName = input.next();
System.out.println("Enter occupation: ");
occuPation = input.next();
System.out.println("Enter gender (M for male, F for female): ");
genDer = input.next();
System.out.println("Enter counter code: ");
countryCode = input.next();
System.out.println("Enter area code: ");
areaCode = input.next();
System.out.println("Enter number: ");
numBer = input.next();
break;
case "2":System.out.println("2");break;
case "3":System.out.println("3");break;
case "4":System.out.println("4");break;
case "5":System.out.println("5");break;
}
}
else
{
System.out.println("Invalid keyword, Please try again");
}
}
while (i < 1);
}
static void Menu()
{
System.out.println("[1] Store to ASEAN phonebook");
System.out.println("[2] Edit entry in ASEAN phonebook");
System.out.println("[3] Delete entry from ASEAN phonebook");
System.out.println("[4] View\\search ASEAN phonebook");
System.out.println("[5] Exit");
}
}
i am trying to use a scanner to get user input to add a new item to a stock list i have created, the item must possess the attributes itemID, itemDesc, price, quantity, and reorderlevel.
How would i go about reading the user input, and recognising it as one of those variables i've created, and then adding it to my list?
I've had a go, but it doesnt appear to recognise them as my variables
Any help would be much appreciated.
Thanks
MY ATTEMPT:
else if (i==1)
{
StockListInterface.doAddItem(item);
System.out.println("Add New Item");
System.out.println("****************");
System.out.println("Enter ID :>");
Scanner scanner1 = new Scanner(System.in);
String itemID = scanner1.nextLine();
System.out.println("Enter Description :>");
Scanner scanner2 = new Scanner(System.in);
String itemDesc = scanner2.nextLine();
System.out.println("Enter Price :>");
Scanner scanner3 = new Scanner(System.in);
String price = scanner3.nextLine();
System.out.println("Enter Quantity :>");
Scanner scanner4 = new Scanner(System.in);
String quantity = scanner4.nextLine();
System.out.println("Enter Re-Order Level :>");
Scanner scanner5 = new Scanner(System.in);
String reOrderLevel = scanner5.nextLine();
System.out.println("Enter another? (Y/N) :>");
}
You don't need to create a new Scanner every time you take an input. You can use the previously defined scanner as follows:
else if (i==1)
{
StockListInterface.doAddItem(item);
System.out.println("Add New Item");
System.out.println("****************");
System.out.println("Enter ID :>");
Scanner scanner1 = new Scanner(System.in);
String itemID = scanner1.nextLine();
System.out.println("Enter Description :>");
String itemDesc = scanner1.nextLine();
System.out.println("Enter Price :>");
String price = scanner1.nextLine();
System.out.println("Enter Quantity :>");
String quantity = scanner1.nextLine();
System.out.println("Enter Re-Order Level :>");
String reOrderLevel = scanner1.nextLine();
System.out.println("Enter another? (Y/N) :>");
}
Also you should consider adding one more line at the end, to get the input of 'Y' or 'N' as follows:
String addAnother = scanner1.nextLine();
One problem in your code i can see is you are creating Object of scanner every time when you get input.
I Suggest you to use class BufferedReader like this. It works perfectly.
BufferedReader br = null
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String tmp = br.readLine();
} catch(IOException ie){
System.out.println(e)
}
br.readLine() : always returns String
You need to convert String into related to datatype. Let's say i want to convert that string to integer, Then i can use wrapper class like this
Integer num = Integer.parseInt(tmp);
You don't need to create a new scanner every time you want to read input, just use
Scanner scanner = new Scanner(System.in);
and then every time you want to read input use
scanner.nextLine()
but if you know you're reading in an int etc you can use specific methods like
scanner.getInt()
see here for more information
I'm getting several error messages when I try to run my program, the main one which bothers me being "error: variable keyboard is already defined in method main(String [])"
Am I supposed to but main(String []) more than once in my program, or just in the beginning as I have it? What else could be wrong here?
Here is the beginning of my program:
public static void main(String[]args)
{
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard = new Scanner(System.in);
firstName = keyboard.nextString();
System.out.println("Enter your last name:");
Scanner keyboard = new Scanner(System.in);
lastName = keyboard.nextString();
System.out.println("Enter the number of movies downloaded:");
Scanner keyboard = new Scanner(System.in);
moviesDownloaded = keyboard.nextInt();
System.out.println("Enter the cost per movie:");
Scanner keyboard = new Scanner(System.in);
movieCost = keyboard.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
Scanner keyboard = new Scanner(System.in);
stateResidency = keyboard.nextInt();
You should only declare and initialize keyboard once and then use it. So remove all lines of the type: Scanner keyboard = new Scanner(System.in); apart from the first one.
Otherwise you try to declare the same variable multiple times and thus java complains.
I'm guessing this has been long solved by this point but I came across it and I really like closure, because Ivaylo Strandjev answer was not chosen. Also in case anyone else stumbles across this.
The error is saying you defined the variable keyboard in this scope already and you are trying to define it again.
like Ivaylo Strandjev was saying.
You can try the following:
1 remove the declaration portion
public static void main(String[]args) {
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard = new Scanner(System.in);
firstName = keyboard.nextString();
System.out.println("Enter your last name:");
keyboard = new Scanner(System.in); //-----changed
lastName = keyboard.nextString();
System.out.println("Enter the number of movies downloaded:");
keyboard = new Scanner(System.in); //-----changed
moviesDownloaded = keyboard.nextInt();
System.out.println("Enter the cost per movie:");
keyboard = new Scanner(System.in);//-----changed
movieCost = keyboard.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
keyboard = new Scanner(System.in);//-----changed
stateResidency = keyboard.nextInt();`
or this, change the new variable names
public static void main(String[]args){
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;
System.out.println("Enter your first name:");
Scanner keyboard1 = new Scanner(System.in);
firstName = keyboard1.nextString();
System.out.println("Enter your last name:");
Scanner keyboard2 = new Scanner(System.in);//-----changed
lastName = keyboard2.nextString();
System.out.println("Enter the number of movies downloaded:");
Scanner keyboard3 = new Scanner(System.in);//-----changed
moviesDownloaded = keyboard3.nextInt();
System.out.println("Enter the cost per movie:");
Scanner keyboard4 = new Scanner(System.in);//-----changed
movieCost = keyboard4.nextDouble();
System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
Scanner keyboard5 = new Scanner(System.in);//-----changed
stateResidency = keyboard5.nextInt();`
You don't need to initialize Keyboard every time you use it. You can declare it once at the top of the program, and just call keyboard.next() each time you want to get something from it.