How can i create Contact List? - java

This is my code so far:
Scanner scanner = new Scanner(System.in); //Scanning all user input
// Dialogue
System.out.println("Welcome to jAddress!");
System.out.println("Please tell me your name and phone number?");
String personalInfo = scanner.nextLine();
System.out.println("So your name & number is " + personalInfo);
//-----------------------------------------------------------------------------
String[] theList; // setting the type and name of the Array
theList = new String[6];// giving the array account of how many
/*Inputting the Name & Number Of Each Individual and storing it*/
personalInfo = theList[0]; // This one stores your personalInfo into theLis[0]
System.out.println("Do you Have a Name & Number You'd Like to Input? y/n");
String yesNo = scanner.nextLine();
String y = "yes"; String n = "no";
if(yesNo == y){
System.out.println("Yay:D");
}else if(yesNo == n){
System.out.println("GoodBye");
//break;
}
System.out.println("Please Enter Name & Phone Number:");
theList[1] = scanner.nextLine(); //Storing Number & Name for theList[1]
System.out.println("Please Enter Name & Phone Number:");
theList[2] = scanner.nextLine(); //Storing Number & Name for theList[2]
System.out.println("Please Enter Name & Phone Number:");
theList[3] = scanner.nextLine(); //Storing Number & Name for theList[3]
System.out.println("Please Enter Name & Phone Number:");
theList[4] = scanner.nextLine(); //Storing Number & Name for theList[4]
System.out.println("Please Enter Name & Phone Number:");
theList[5] = scanner.nextLine(); //Storing Number & Name for theList[5]
System.out.println("If you want to access any name simply Press 0-5.");
int chooseContact = scanner.nextInt();
I managed to link both the array into the user input. So the user can manually input a name and phone number as a String which then will get stored in one of the arrays.
However, I want the user at the end to be able to search the array and have it print the name and phone number. For example, I want the user to input 1 which will then output the array theList[0] number and name.
How can I do this? I'm looking for a simple method of achieving this, such as assigning variables etc.

you can simply use the index input by the user (+/- one depending on the case of 0-5/1-6 numbering) theList[index]

Related

Including spaces in Scanner in java

I am writing a simple code that takes number,name,surname from the user with scanner.
But when user enters name with spaces in it(two or more names) the code thinks string after the first space is surname.
I tried using input.nextLine(); in that case it skipped name completely and took only surname from the user.
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
int num = input.nextInt();
System.out.println("Enter Name");
String name = input.next();
System.out.println("Enter Surname");
String surname = input.next();
Try this:
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
int num=input.nextInt();
input.nextLine(); // add this
System.out.println("Enter Name");
String name=input.nextLine();
System.out.println("Enter Surname");
String surname=input.nextLine();
System.out.println(num + " - " + name + " - " + surname);
Sample input/output:
Enter number
1
Enter Name
user name
Enter Surname
sur name
1 - user name - sur name
Use sc.nextLine() instead of sc.next() to read String with spaces

how to retrieve a line of input of different primitive types

firstly i created a boolean then set it to true so as to enable the first run... then subsequent runs would be based on the user input.
boolean emptyLine = true;
String name; int quantity; double price;
Scanner scan = new Scanner(System.in);
while(emptyLine != false) //repeatedly request user input if input is not empty
{
//ask the user for the quantity of items being purchased.
System.out.println("Enter name of item: Enter quantity of item purchased: Enter price of item being purchased: ");
input = scan.nextLine(); //should read the line of input
item = //name of item from the line of input.
quantity = scan.nextInt(); //should get the quantity from the line of input
price = scan.nextDouble(); //get the price from the line of input
if(input.length() >= 0)
{
emptyLine = true;
}
else
{
emptyLine = false;
}
}
I am trying to write a code to check if user input is empty or not. such that if user input is empty, the user is requested to enter a valid input or exit.
for example, if the user enters a space, I want my program to accept it, but if the user presses the ENTER button then it should be rejected.
One easy way:
while (scann.hasNextLine()) {
String userInput = scan.nextLine();
Now userInput is a String; and you could manually check its content; for example by calling isEmpty(); and if it is not empty, you can try to convert to the next expected type.

Why does the input not print out

I just want it to display my name. It worked in a different code but I cut out the parts that were supposed to say the name.
import java.util.Scanner;
public class ComputePay
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String name;
System.out.print("Please enter your First and Last name >> ");
input.nextLine();
name = input.nextLine();
System.out.println("Thank you, " + name);
}
}
When you do your input.nextLine(); for the first time, you don't save the result to any variable. So you are loosing the value that the user entered before.
If you just remove that line then name = input.nextLine(); will successfully read the value and store it in the variable name.
If you want to read in multiple values just repeat that process:
System.out.print("Please enter your first name >> ");
firstName = input.nextLine();
System.out.print("Please enter your last name >> ");
lastName = input.nextLine();

getting this to repeat using a while loop. credit card validation

I'm almost done with this project for my intro to java programming class. It's a credit card validation program. I know I'm super close to being done. All I need to do is to ask the user if he would like to enter another card number after the first one.
Normally the program says please enter your card number
user inputs card number
program determines if visa, mastercard etc... then determines if valid.
Simple enough.but I need to alter it to say
"please enter a card number"
user inputs card number
"would you like to input another card number?"
user inputs yes or no
If it's yes then the method reruns and gets another number then proceeds to do so until the user says no.
After the user says no, the program determines if the cards are visa, mastercards, etc... and if they are valid.
My code is posted below. I am pretty sure I need a while loop but I'm not quite sure how to set it up. The code below is just the getcardnumber method.
TLDR;
How do i get this to repeat until the user stops inputting card numbers?
public static String getCardNumber() {
String cardNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the credit card number: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
while (!isAllDigits(cardNumber)) {
System.out.println("The credit card number must contain only the"
+ " digits 0-9; please re-enter: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
}
//a number containing only digits has been entered
return cardNumber;
}
You can do something like this:
public static void main(String[] args){
boolean b = false;
do {
System.out.println("Number 2: ");
Item2 item2 = new Item2();
System.out.println("Please enter the credit card number: ");
Scanner keyboard = new Scanner(System.in);
System.out.println(checkNumber(keyboard.nextLine(), keyboard));
System.out.println("Would you like to input another card number? 1=Yes; 2=No");
int decision = Input.nextInt();
if (decision == 1){
b = false;
}else if (decision == 2){
b = true;
}
}while(b == false);
}
public static String checkNumber(String cardNumber, Scanner keyboard) {
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
while (!isAllDigits(cardNumber)) {
System.out.println("The credit card number must contain only the"
+ " digits 0-9; please re-enter: ");
cardNumber = keyboard.nextLine();
//remove spaces from cardNumber
cardNumber = cardNumber.replace(" ", "");
}
//a number containing only digits has been entered
return cardNumber;
}

creating a shopping catalog in netbeans

Create a java file in NetBeans, name it Catalog.java.
Create one String array to store 3 products for a catalogue
Create the appropriate variables to store the product name, product code and product price.
At the start of the program display the catalogue for the user by looping through the array and outputting it to the screen with a number listing for each product, as shown above
Create an infinite loop to enter orders; to stop the loop the user should enter 0.
Keep a running total (accumulator) of all products amount total and sub-total (multiple accumulators)
Write a method to calculate the taxes and return a grand total
Write another method to print out the order as listed above
its supposed to be entered like this
I would like to know how to have the user input like this and be stored in an array.
Enter Order Number (0 to stop): M3487
Enter Quantity: 2
Enter Order Number (0 to stop): W3876
Enter Quantity: 3
Enter Order Number (0 to stop): R9983
Enter Quantity: 3
Enter Order Number (0 to stop): 0
when i enter the code "M3487" its not going to the quantity, its ending the program.
This is the code I have so far. I'm a beginner, so please bear with me.
package catalog;
import java.util.*;
public class Catalog {
static String products[] = new String[3];
static int answer;
public static void main(String[] args) {
System.out.println("------------------");
System.out.println("Shopping Catalog");
System.out.println("------------------");
String[] pCode = new String[3];
float pPrice[] = new float[3];
int orderNum = 0;
int quantity=0;
Scanner s = new Scanner(System.in);
System.out.println("------------------------------------------");
System.out.println("condensed milk [M3487], $9.50 per can.");
System.out.println("");
System.out.println("Distilled Water [W3876], $3.00 a bottle.");
System.out.println("");
System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
System.out.println("------------------------------------------");
do{
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
orderNum++;
if(pCode[orderNum] == ("M3487")){
System.out.println("condensed milk $9.50");
System.out.println("Enter Quantity");
quantity = s.nextInt();
}//close if statement
if(answer == 0){
break;
}//close if
}while(true);//close while loop
}//close main method
}//close class
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
orderNum++; //This is the problem
if(pCode[orderNum] == ("M3487"))//It will not work because you have change the index
Remove
orderNum++;
because You have inserted string at pCode[0] zero index and searching at pCode[1].
Change you code to:
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
if(pCode[orderNum].equals("M3487")){
System.out.println("condensed milk $9.50");
System.out.println("Enter Quantity");
quantity = s.nextInt();
}
orderNum++;

Categories

Resources