why netbeans not showing system out put within the code - java

I am creating a booking project in NetBeans, I am first implementing a booking controller that will validate user input using the Java scanner. I would like to test the code and input data in the terminal. When I run the output of the code terminal the terminal just shows " Build successful". And shows none of the systems out print code line. I am not too sure what is wrong with the code please see below
package fitnessclassapp;
import java.util.Scanner;
public class BookingController {
private Scanner input = new Scanner (System.in);
Customer customer = new Customer ();
// customer enter details and the details are validated
private String Customer () {
String customerName = "";
int customerAge = -1 ;
String membership = "";
boolean isName;
System.out.println( "Please enter your name " );
do {
// name of condition HasNext will check the user input
if ( input.hasNext()) {
customerName = input.nextLine();
isName = true;
// add a boolean
}else
System.out.println ( "You have provided incorrect information");
isName = false;
input.next();
}while ( !isName );
System.out.println(customerName);
return customerName;
}
}

Try this code,
package fitnessclassapp;
import java.util.Scanner;
public class BookingController {
private Scanner input = new Scanner(System.in);
// Customer customer = new Customer();
// customer enter details and the details are validated
private String Customer() {
String customerName = "";
int customerAge = -1;
String membership = "";
boolean isName;
System.out.println("Please enter your name ");
do {
// name of condition HasNext will check the user input
if (input.hasNext()) {
customerName = input.nextLine();
isName = true;
// add a boolean
} else
System.out.println("You have provided incorrect information");
isName = false;
input.next();
} while (!isName);
System.out.println(customerName);
return customerName;
}
public static void main(String[] args) {
BookingController con = new BookingController();
con.Customer();
}
}
if any issue inform.

Related

Search and match in Txt file (Java)

Hi guys please is there anyone can help me out with this program?
write a program that asks the user to enter a postcode and returns the city for that
postcode. If the postcode in not in the list then it should return city not found.
The find city code must be in a separate method findCity()
The user should be able to continue entering postcodes until they enter 9999 to indicate they
are complete (9999 should not appear as “city not found”)
================================================
in the txt file:
Dandenong 3175
Frankstone 3199
Berwick 3816
Cranbourne 3977
Rosebud 3939
Thats what i've done so far.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) throws FileNotFoundException
{
try
{
File f = new File("Files\\cities.txt");
Scanner input = new Scanner(f);
String text;
while(input.hasNextLine())
{
text = input.nextLine();
process(text);
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
}
public static void process(String text)
{ String name = null;
int id;
Scanner code = new Scanner(System.in);
System.out.println("enter the postcode");
id = code.nextInt();
Scanner data = new Scanner(text);
if(code.equals(0))System.out.println(name);
name = data.next();
id = data.nextInt();
while(data.hasNextDouble())
{
}
System.out.println(name+ " ");
// System.out.println(id+ " ");
}
}
File f = new File("d:\\cities.txt");
Scanner input = new Scanner(f);
Map<Integer,String> cityCode = new HashMap<Integer,String>();
String text;
while(input.hasNextLine())
{
text = input.nextLine();
Scanner data = new Scanner(text);
String name = data.next();
int id2 = data.nextInt();
cityCode.put(id2, name);
}
System.out.println("enter the postcode");
Scanner code = new Scanner(System.in);
int id = code.nextInt();
if(cityCode.containsKey(id)) {
System.out.println(cityCode.get(id));
} else {
System.out.println("City Not found");
}
Here's a straight forward approach:
First, you want user to enter a passcode. If passcode is lesser than 9999, you want to search the text file to find a city with that passcode. This thing can be implemented as:
int passcode = 5; // Suppose passcode is 5. You may suppose any value lesser than 9999
Scanner input = new Scanner(System.in);
// Ask user to enter a passcode. If user enters 9999 the while loop is exited
while(passcode < 9999)
{
System.out.println("Enter passcode: ");
passcode = input.nextInt();
// donot search if passcode is greater than or equal to 9999
if(passcode < 9999)
searchCity(passcode);
}
searchCity() method works like:
public static String searchCity(int passcode) {
Scanner citiesScanner = new Scanner(new File("Files\\cities.txt"));
while(citiesScanner.hasNext()) {
String city = citiesScanner.next();
String pass = citiesScanner.next();
if(Integer.parseInt(pass) == passcode) {
return city;
}
}
return "City not found";
}
Just try to break your problem into sub problems. Do some paper work before starting typing code. Things become a lot simpler this way.

How to get the user input data that i recently typed in?

I just started to learn Java a few days ago and i wanted to create a program that will create an account,stored it and then if we enter the exact username and password then it will say "login successful".Please don't mind my way of naming the variables and classes.My question is in my "crttacc" class,where i have set up 2 get method to get the un and pa variables.However,in my main method after running the create method in crttacc,the input data seem to gone away and not stored in the un,pa variables as when i try to print them out to check it,it would return "null".I'm a newbie so i'm not quite sure that my question was clear enough and i hope that u guys can help me.You have my gratitude.
Here's my codes:(please don't laugh :D)
import java.util.Scanner;
class crttacc {
String pa;
String un;
void create() {
Scanner unin = new Scanner(System.in);
Scanner passin = new Scanner(System.in);
System.out.println("enter the user name: ");
String l1 = unin.nextLine();
System.out.println("enter the password: ");
String l2 = passin.nextLine();
l1=un;
l2=pa;
}
String getpass(){
return un;
}
String getuser(){
return pa;
}
}
class loginn {
;
void logingin() {
crttacc acc1 = new crttacc();
String pass = acc1.getpass();
String user = acc1.getuser();
System.out.println(pass);
System.out.println(user);
Scanner passwordinput = new Scanner(System.in);
Scanner usernameinput = new Scanner(System.in);
System.out.println("username:");
String line1 = usernameinput.nextLine();
System.out.println("Password:");
String line2 = passwordinput.nextLine();
String d1=line1;
String d2=line2;
if(d1.equals(pass) && d2.equals(user)) {
System.out.println("login successfull");
}
else {
System.out.println("try again");
}
}
}
public class login3 {
public static void main(String[] args) {
Scanner com = new Scanner(System.in);
System.out.println("enter your command: create account or login");
String com1 = com.nextLine();
String l1=com1;
if(com1.equals("create account")) {
crttacc acc1 = new crttacc();
acc1.create();
}
Scanner com2 = new Scanner(System.in);
System.out.println("would you like to login now? y/n");
String cm2 = com2.nextLine();
String l2=cm2;
if(l2.equals("y")) {
loginn log1 = new loginn();
log1.logingin();
}
else{
}
}
}
it's :
un=l1;
pa=l2;
in the Create() method
The problem seems to be, that you store the input data you get through the scanner in l1 and l2. Then you give your l1 and l2 variables the value of un and pa (which is null at this point). At the end of your method the local variables l1 and l2 are thrown away and nothing changed for your un and pa.
You propably wanted something like this:
void create() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter the user name: ");
String l1 = scanner.nextLine();
System.out.println("enter the password: ");
String l2 = scanner.nextLine();
this.un = l1;
this.pa = l2;
}
With this you stored the values of l1 and l2 in your fields un and pa and can access them through your getters.
Hint: you don't need a new Scanner for every input. You can reuse it :)

How to add multiple user-prompts in while loop?

I have to write a program that asks the user for his name, address and phone number. When the data is entered the program shall print the data and ask the user to verify the data by entering yes or no. This process shall be repeated until the user is satisfied and answers yes to the question.
Now, at this moment I am able to pop-up a single prompt (in my case asking only the user's name). But what if I want to add multiple question (i.e. asking address and telephone number) and happen the same thing? How could I do that?
My code:
package userinfo;
import java.util.Scanner;
import sun.security.krb5.SCDynamicStoreConfig;
public class UserInfo {
public static void main(String[] args) {
String varify;
String yes = "yes";
String no = "no";
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = input.next();
System.out.println("Your input was: "+name);
System.out.println("Varify by yes or no: ");
while (true) {
varify = input.next();
if (varify.equalsIgnoreCase(yes)) {
System.out.println("Varified! Your name is: " + name);
} else if (varify.equalsIgnoreCase(no)) {
System.out.println("Type your name again: ");
}
}
}
}
You can extract this code to a method:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userName = readFieldAndVerify(input, "Enter your name: ");
String userAddress = readFieldAndVerify(input, "Enter your address: ");
String userPhoneNumber = readFieldAndVerify(input, "Enter your phone number: ");
}
private static String readFieldAndVerify(Scanner input, String prompt) {
while (true) {
System.out.print(prompt);
String field = input.next();
System.out.println("Are you sure (yes / no)?");
String verify = input.next();
if (verify.equalsIgnoreCase("yes")) {
System.out.println("Verified!");
return field;
} else {
System.out.println("Canceled");
}
}
}
EDIT Added logic for more questions... Expand it in similar fashion for everything you need. You could expand this code into a single method as well so you avoid code replication. Check answer from user alaster for an example.
Try this. It will store the name variable in case you want to use it further.
We use a boolean to keep asking the user to input his name until he validates it.
Of course, you can still use while(true) and then break if the name is valid, but I prefer this method since the code is more clear and easier to understand.
private static boolean isVerified(String verify) {
if (verify.equalsIgnoreCase("yes")) {
return true;
} else if (verify.equalsIgnoreCase("no")) {
return false;
} else
return false;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean validName = false;
boolean validTelephoneNo = false;
boolean validAddress = false;
String name="";
String telephoneNo="";
String address="";
while (!validName) {
System.out.print("Enter your name: ");
name = input.next();
System.out.println("Are you sure your name is " + name + "?");
final String verify = input.next();
if (isVerified(verify)) {
System.out.println("Verified! Your name is: " + name);
validName = true;
} else {
System.out.println("Not verified! Please type your name again.");
}
}
while (!validTelephoneNo) {
System.out.print("Enter your telephone nummber: ");
telephoneNo = input.next();
System.out.println("Are you sure your telephone number is " + telephoneNo + "?");
final String verify = input.next();
if (isVerified(verify)) {
System.out.println("Verified! Your telephone number is: " + telephoneNo);
validTelephoneNo = true;
}
else {
System.out.println("Not verified! Please type your telephone number again.");
}
}
while (!validAddress) {
System.out.print("Enter your address: ");
address = input.next();
System.out.println("Are you sure your address is " + address + "?");
final String verify = input.next();
if (isVerified(verify)) {
System.out.println("Verified! Your address is: " + address);
validAddress = true;
}
else {
System.out.println("Not verified! Please type your address again.");
}
}
System.out.println("Done, here is your info:");
System.out.println("Name: " + name);
System.out.println("Telephone Number: "+telephoneNo);
System.out.println("Address: "+address);
}

Reading a string and adding it to an ArrayList [duplicate]

This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 8 years ago.
I wrote a program that has an arraylist of users so it should either create a new user and store the user's name in an arraylist or choose an existing user from an arraylist. The problem is, when I run the code I can choose an option but when I want to create a new user I cannot type anything. Why does that happen and how can I fix this?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
private ArrayList<String> users;
public static void main(String[] args) {
Main m = new Main();
m.menu();
}
public void createNewUser(String name) {
users.add(name);
}
public boolean search(String username) {
if (users.contains(username))
return true;
else
return false;
}
public void displayUsers() {
Iterator<String> itr = users.iterator();
while (itr.hasNext()) {
String name = itr.next();
System.out.println(name);
}
}
public void menu() {
users = new ArrayList<String>();
int choice = 0;
String name;
Scanner input = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1- Create new user");
System.out.println("2- Select current user");
choice = input.nextInt();
if (choice == 1) {
System.out.println("Enter your name: ");
name = input.nextLine();
createNewUser(name);
displayUsers();
} else {
do {
displayUsers();
System.out.println("Enter your username: ");
name = input.nextLine();
if (search(name)) {
System.out.println("User found");
break;
}
else
System.out.println("This username does not exist.");
} while (!search(name));
}
}
}
System.out.println("Choose an option:");
System.out.println("1- Create new user");
System.out.println("2- Select current user");
choice = input.nextInt();
input.nextLine();
Try adding input.nextLine(); after your choice.

incorrect input Scanner

hey i am trying to take inputs from user one by one but its appears to take wrong input... it actually skip the one input to be taken from user ..
e.g..in the code below i want to take name first then address and at last contact but when i do so it skip the name input...
import java.util.ArrayList;
import java.util.Scanner;
public class mainClass {
public static void main(String args[]){
int value = 0;
ArrayList<Data> Contacts = new ArrayList<Data>();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("Enter 1 to add a Contact :: Enter 2 to View all Contact");
value = input.nextInt();
switch(value){
case 1:
System.out.println("Plz enter Name : ");
String name = input.nextLine();
System.out.println("Plz enter Address : ");
String address = input.nextLine();
System.out.println("Plz enter ContactNo : ");
String contact = input.nextLine();
Data objt1 = new Data(name, address, contact);
Contacts.add(objt1);
break;
case 2:
System.out.println("Name\t\tContact\t\tAddress");
for(int i=0; i<Contacts.size(); i++)
{
System.out.println(Contacts.get(i));
}
break;
default:
System.out.println("Sorry wrong input");
}
}
}
}
the data class is here
public class Data {
private String name = "";
private String address = "";
private String cell = "";
public Data(String n, String a, String c){
name = n;
address = a;
cell = c;
}
public String toString()
{
return String.format("%s\t\t%s\t\t%s", name, cell, address);
}
}
try adding input.nextLine(); after getting the value, this will consume the new line character
value = input.nextInt();
input.nextLine();
(or)
int value = Integer.parseInt(input.nextLine());

Categories