I'm working on a .xml file generator. It works by asking the user to input 5 different values and then using them for generating a .xml data container. This is the code:
public class Generator {
public static void main(String[] args) throws InterruptedException {
Scanner reader = new Scanner(System.in);
while (true) {
System.out.println("Input the furni ID (input only numbers here).");
String furniID = reader.nextLine();
System.out.println("Input the furni's file name (without '.swf'. You can input numbers, letters and underscores here).");
String furniFileName = reader.nextLine();
System.out.println("Input the furni's revision (input only numbers here).");
String furniRevision = reader.nextLine();
System.out.println("Input the furni's name (this will be the name that it will display when you click on the furni in a room or in your inventory).");
String furniName = reader.nextLine();
System.out.println("Input the furni's description (this will be displayed under the furni's name).");
String furniDescription = reader.nextLine();
System.out.println("Input the furni's furniline. This is just a name for a group of furnis that belong to the same collection. For example you can input 'custom' (without quotation marks).");
String furniLine = reader.nextLine();
System.out.println("Generating your furnidata...");
System.out.println(" ");
TimeUnit.SECONDS.sleep(1);
System.out.println("<furnitype id=\"" + furniID + "\"" + " classname=\"" + furniFileName + "\"" + ">");
System.out.println("<revision>" + furniRevision + "</revision>");
System.out.println("<xdim>1</xdim>");
System.out.println("<ydim>1</ydim>");
System.out.println("</partcolors>");
System.out.println("<name>" + furniName + "</name>");
System.out.println("<description>" + furniDescription + "</description>");
System.out.println("</adurl");
System.out.println("<offerid>-1</offerid>");
System.out.println("<buyout>0</buyout>");
System.out.println("<rentofferid>-1</rentofferid>");
System.out.println("<rentbuyout>0</rentbuyout>");
System.out.println("<bc>0</bc>");
System.out.println("<excludeddynamic>0</excludeddynamic>");
System.out.println("<customparams/>");
System.out.println("<specialtype>1</specialtype>");
System.out.println("<canstandon>0</canstandon>");
System.out.println("<cansiton>0</cansiton>");
System.out.println("<canlayon>0</canlayon>");
System.out.println("<furniline>" + furniLine + "</furniline>");
System.out.println("</furnitype>");
System.out.println(" ");
System.out.println("Do you want to generate another furnidata? (YES/NO)");
String confirmation = reader.nextLine();
if (confirmation.equals("NO")) {
System.out.println("Furnidata generator has been stopped.");
break;
} else if (confirmation.equals("no")) {
System.out.println("Furnidata generator has been stopped.");
break;
} else if (confirmation.equals("No")) {
System.out.println("Furnidata generator has been stopped.");
break;
}
}
}
I want the program to detect when someone inputs an empty string by pressing the enter key with no written text and print a message asking the user to not input empty strings. I tried this with a try-catch statement but after printing the message the program executes the next line instead of re-executing the one where the user input an empty string. How can I achieve this?
Whenever you have a prompt for a User to supply specific data you should validate that data that was entered and if it is incorrect, permit the User to try again so as not to halt the application due to an error of some kind that can be avoided. There is no problem using Regular Expressions (regex) for this sort of thing.
A User entering nothing (by just hitting the ENTER key) is just one problem. At some prompts you are expecting the User to enter only numerical digits. A small regex within the String#matches() method is ideal for this, for example:
if (!userInput.matches("\\d+") {
System.out.println("Invalid input! Numerical digits only!");
}
If the User input does not match all numerical digits then display "Invalid Input!".
Every prompt should be in its own do or while loop so that validation can be done and an opportunity for the User to re-enter the data can be accomplished. If you have lots of prompts then a class method can be created to handle all of them in order to remove duplicate coding. As an example, here is your code modified so that a method named askUser() can handle the prompts to the User:
public class Generator {
public static void main(String[] args) {
java.util.Scanner reader = new java.util.Scanner(System.in);
while (true) {
// Only a String repesentation of numerical digits will be accepted.
// 1 to 20 digits are acceptable)
String furniID = askUser(reader, "Input the furni ID (input only numbers here).",
"\\d{1,20}");
/* Only characters A to Z in either upper or lower case, digits
from 0 to 9, whitespaces, and underscores will be accepted
within the Users input. The period (.) is not acceptable
therefore eliminating the possibility of a file name extension
like .swf to be applied to the supplied file name. File name can
be 1 to 80 characters. */
String furniFileName = askUser(reader, "Input the furni's file name (without "
+ "'.swf'.\nYou can input numbers, letters and underscores here).",
"(?i)[a-z0-9 _]{1,80}");
// Only a String repesentation of numerical digits will be accepted.
// 1 to 20 digits are acceptable)
String furniRevision = askUser(reader, "Input the furni's revision (input "
+ "only numbers here).", "\\d{1,20}");
/* Only characters A to Z in either upper or lower case, digits
from 0 to 9, whitespaces, and underscores will be accepted
within the Users input. The period (.) is not acceptable. The
name can be 1 to 25 characters in length. */
String furniName = askUser(reader, "Input the furni's name (this will be "
+ "the name that will\nbe displayed when you click on the furni in "
+ "a room or in\nyour inventory).", "(?i)[a-z0-9 _]{1,25}");
/* Only characters A to Z in either upper or lower case, digits
from 0 to 9, whitespaces, and periods will be accepted within
the Users input. Description can be a minimum of 5 to a maximum
of 120 characters in length. */
String furniDescription = askUser(reader, "Input the furni's description ("
+ "this will be displayed\nunder the furni's name).", "(?i)[a-z0-9. ]{5,120}");
/* Only characters A to Z in either upper or lower case, digits
from 0 to 9, and whitespaces will be accepted within the Users
input. The period (.) is not acceptable. The furniline name can
be 1 to 25 characters in length. */
String furniLine = askUser(reader, "Input the furni's furniline. This is just "
+ "a name for a\ngroup of furnis that belong to the same collection. "
+ "For\nexample you can input 'custom' (without quotation marks).",
"(?i)[a-z0-9 ]{1,25}");
System.out.println("Generating your furnidata...");
System.out.println(" ");
System.out.println("<furnitype id=\"" + furniID + "\"" + " classname=\"" + furniFileName + "\"" + ">");
System.out.println("<revision>" + furniRevision + "</revision>");
System.out.println("<xdim>1</xdim>");
System.out.println("<ydim>1</ydim>");
System.out.println("</partcolors>");
System.out.println("<name>" + furniName + "</name>");
System.out.println("<description>" + furniDescription + "</description>");
System.out.println("</adurl");
System.out.println("<offerid>-1</offerid>");
System.out.println("<buyout>0</buyout>");
System.out.println("<rentofferid>-1</rentofferid>");
System.out.println("<rentbuyout>0</rentbuyout>");
System.out.println("<bc>0</bc>");
System.out.println("<excludeddynamic>0</excludeddynamic>");
System.out.println("<customparams/>");
System.out.println("<specialtype>1</specialtype>");
System.out.println("<canstandon>0</canstandon>");
System.out.println("<cansiton>0</cansiton>");
System.out.println("<canlayon>0</canlayon>");
System.out.println("<furniline>" + furniLine + "</furniline>");
System.out.println("</furnitype>");
System.out.println(" ");
// Only y or n can be supplied in any letter case.
// Anything else generates an Invalid Input message.
String confirmation = askUser(reader, "Do you want to generate another furnidata? (y/n)",
"(?i)[yn]", "Invalid Choice! Either 'y' for Yes or 'n' for No!");
if (confirmation.equalsIgnoreCase("n")) {
System.out.println("Furnidata generator has been stopped.");
break;
}
}
}
/**
* Displays the supplied prompt within the Console Window and permits the User
* to enter a response. User input is validated based on the regex argument
* supplied to the <b>validationRegEx</b> parameter.<br><br>
*
* #param inputReader (Scanner) Scanner object to use for User Input.<br>
*
* #param promptString (String) The prompt to display within the console window.<br>
*
* #param validationRegEx (String) A regular expression that will be used
* within a <b>String#matches()</b> method to validate the User's input.<br>
*
* #param errorString (Optional - String) Default validation error message is:<pre>
*
* "Invalid input! Try again..."</pre><br>
*
* Here you can optionally supply your own message for non-valid entries.<br>
*
* #return (String) The valid response from User.
*/
public static String askUser(java.util.Scanner inputReader, String promptString, String validationRegEx, String... errorString) {
String userInput = "";
String errString = "Invalid input! Try again...";
if (errorString.length > 0 && !errorString[0].trim().isEmpty()) {
errString = errorString[0];
}
while (userInput.isEmpty()) {
System.out.println();
System.out.println(promptString);
userInput = inputReader.nextLine();
if (!userInput.matches(validationRegEx)) {
System.err.println(errString);
userInput = "";
}
}
return userInput;
}
}
You might want to use.
String furniID = reader.nextLine();
furniID = furniID.trim();
if(furniID.equals("")){
System.out.println("Empty Here. ");
}
As suggested in comments .readLine() returns an empty string if you supply a blankline or simply hit enter.
The above regex expression will take care of empty string "" or whitespaces that user might enter.
edit 1
As suggested in comments regex could have been over-kill here. The string.trim() will take care of tabs, trailing-leading whitespaces and empty string.
Related
I have the following code:
String f_name = "";
System.out.println(ANSI_PURPLE + "What is your first name?");
System.out.print(ANSI_RESET + " Type your name here (use only latin characters) > ");
while(!sc.hasNext("[A-Za-z]*")) {
System.out.println(ANSI_RED + " ERROR: Invalid option! ");
System.out.print(ANSI_RESET + " Type your name here (use only latin characters) > ");
f_name = sc.next().toUpperCase();
}
System.out.println("First Name = " + f_name);
The issue with the above code is that it would store what has previously added.
for example:
What is your first name?
Type your name here (use only latin characters) > 123
ERROR: Invalid option!
Type your name here (use only latin characters) > c
First Name = 123
How to fix so that the validation of latin characters will still work, redirect the user to same question if there is a mistake and store the correct value?
CORRECT ANSWER TO MY QUESTION:
...
while(!sc.hasNext("[A-Za-z]*")) {
System.out.println(ANSI_RED + " ERROR: Invalid option! ");
System.out.print(ANSI_RESET + " Type your name here (use only latin characters) > ");
sc.next();
}
f_name = sc.next().toUpperCase();
System.out.println("First Name = " + f_name);
When sc.hasNext("[A-Za-z]*") returns true, that means the next input you read will be the one you want. So you need to read f_name in after the loop ends.
You still need sc.next() inside the loop to move past bad input; otherwise you will have an infinite loop.
By the way, perhaps you want to use + instead of * in your regular expression. * means "zero or more", and + means "one or more". I assume you want one or more characters to be input.
while (!sc.hasNext("[A-Za-z]+")) {
System.out.println(ANSI_RED + " ERROR: Invalid option!");
System.out.print(ANSI_RESET + " Type your name here (use only latin characters) > ");
sc.next();
}
String f_name = sc.next().toUpperCase();
This question says ask for the 'First Name' and the 'Last Name' from the user and then show the message Welcome with the full name of the user . also make sure that the user does not enter his/her full name in the first Text Field which asks for First Name only
I thought that if the user enters his/her full name in the first text field , we can know that from the fact that he/she entered a space or (' ') or not . If not we can simply show the message Welcome + full name . However it didn't work the way I thought it would ... Can somebody help me with itenter image description here
If I understand you the below will work accomplish what you need by ignoring the data after the space and asking the user for their last name.
code:
public static void main(String[] args) {
// Properties
Scanner keyboard = new Scanner(System.in);
String firstName, lastName
// Ask the user for their first name
System.out.println("What is your first name? ");
System.out.print("--> "); // this is for style and not needed
firstName = keyboard.next();
// Ask the user for their last name
System.out.println("What is your last name? ");
System.out.print("--> "); // this is for style and not needed
lastName = keyboard.next();
// Display the data
System.out.println("Your first name is : " + firstName);
System.out.println("Your last name is : " + lastName);
}
There is actually a few ways you can do this, but if I understand your question correctly a simple way would be below, which is from http://math.hws.edu/javanotes/c2/ex6-ans.html and helped me understand Java more when I was learning it, you just would alter it to your needs.
code:
public class FirstNameLastName {
public static void main(String[] args) {
String input; // The input line entered by the user.
int space; // The location of the space in the input.
String firstName; // The first name, extracted from the input.
String lastName; // The last name, extracted from the input.
System.out.println();
System.out.println("Please enter your first name and last name, separated by a space.");
System.out.print("? ");
input = TextIO.getln();
space = input.indexOf(' ');
firstName = input.substring(0, space);
lastName = input.substring(space+1);
System.out.println("Your first name is " + firstName + ", which has "
+ firstName.length() + " characters.");
System.out.println("Your last name is " + lastName + ", which has "
+ lastName.length() + " characters.");
System.out.println("Your initials are " + firstName.charAt(0) + lastName.charAt(0));
}
}
edit:
If this doesn't make sense I can give a better explanation with a better example with more detail.
More notes on similar problems.
https://www.homeandlearn.co.uk/java/substring.html
The problem with your code is, that you check every single charackter and then do the if/else for every single charackter. which means if the last charackter is not a whitespace it will at the end process the else tree.
The solution is to just check once:
if(fn.contains(' '){
//Do what you want to do, if both names were entered in the first field
}else{
//Everything is fine
}
I want the user to only enter his age. So I did this program :
Scanner keyb = new Scanner(System.in);
int age;
while(!keyb.hasNextInt())
{
keyb.next();
System.out.println("How old are you ?");
}
age = keyb.nextInt();
System.out.println("you are" + age + "years old");
I found how to prevent user from using string by using the while loop with keyb.hasNextInt(), but how to prevent him from using the whitespace or from entering more input than his age ?
For example I want to prevent this kind of typing "12 m" or "12 12"
Also, how can I clear all existing data in the buffer ? I'm facing an infinite loop when I try to use this :
while(keyb.hasNext())
keyb.next();
You want to get the whole line. Use nextLine and check that for digits e.g.
String possibleAge = "";
do {
System.out.println("How old are you ?");
possibleAge = keyb.nextLine();
} while (!possibleAge.matches("\\d+"))
Your problem is that the default behaviour of Scanner is to use any whitespace as the delimiter. This includes spaces. This means that a 3 a is in fact three tokens, not one. You can change the delimiter to a new line so that a 3 a becomes a single token, which will then return false for hasNextInt.
I've also added an initial question, because in your example the first input was taken before asking any questions.
Scanner keyb = new Scanner(System.in);
keyb.useDelimiter("\n"); // You can try System.lineSeparator() but it didn't work in IDEA
int age;
System.out.println("How old are you?");
while(!keyb.hasNextInt())
{
keyb.next();
System.out.println("No really. How old are you?");
}
age = keyb.nextInt();
System.out.println("You are " + age + " years old");
String age = "11";
if (age.matches(".*[^0-9].*")) {
System.out.println("Invalid age");
} else {
System.out.println("valid age");
}
If age contains other then digits then it will print invalid age.
I have a string that a user inputs their name in [Last, First Middle] format and I need to change it to [First Middle Last] format.
I've defined the last name as LFM.substring(0, commaSpace) . commaSpace being the name for the ", " in the input of the LFM (Last, First Middle) user input.
Then I needed to define firstMiddle . My question to you is, how could I define the end of the string LFM so I can have firstMiddle be LFM.substring(commaSpace, (end of string) ); ? That way I can just print firstMiddle + last .
ALL OF MY CURRENT CODE:
(IT'S REALLY MESSY, SORRY)
System.out.println();
System.out.println("This program will separate and convert a name in [Last, First, Middle] format to [First Middle Last].");
System.out.println();
System.out.print("Please enter a name in [Last, First Middle] format. ");
Scanner userInput = new Scanner(System.in);
String lineSeparator = System.getProperty("line.separator");
String LFM, first, middle, last, firstMiddle;
int commaSpace, end, lastLength;
userInput.useDelimiter(lineSeparator);
LFM = userInput.nextLine();
commaSpace = LFM.indexOf(",");
last = LFM.substring(0, commaSpace);
lastLength = last.length();
firstMiddle = LFM.substring(commaSpace, //?);
first = LFM.substring(commaSpace + firstMiddle.length());
System.out.println(firstMiddle + (" ") + last);
Use replaceAll or replaceFirst functions since it accepts regex as first argument.
string.replaceAll("^(\\w+),\\s*(\\w+)\\s+(\\w+)$", "$2 $3 $1");
DEMO
I've been learning Java and for some reason, I'm having a brain fart on how to add two strings together. In the program, I successfully have the program state what the length of the First and Last names are independently. However I would like to have the program also state how many characters there are in the name.
I know that I need to assign the string lengths to an integer variable that can be added together, but I'm just blanking at the moment.
My source code is as follows, thank you for your help!
import java.util.Scanner;
/*
* //Program Name: stringwk7
* //Author's Name: MrShiftyEyes
* //Date: 05-12-2013
* //Description: Prompt for a user name; print initials;
* // print out the reverse name, find the length of the names
*/
/**
*
* #author MrShiftyEyes
*/
public class stringwk7{
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// Create first name and last name StringBuffers
Scanner input = new Scanner(System.in);
System.out.print("What is your first name? ");
StringBuffer firstName = new StringBuffer(input.nextLine());
System.out.print("What is your last name? ");
StringBuffer lastName = new StringBuffer(input.nextLine());
// Declare two string variables using StringBuffer variables
String fName = new String(firstName);
String lName = new String(lastName);
// Display the users initials
System.out.println("Your initials are: " + firstName.charAt(0) + " " + lastName.charAt(0));
// Displays the user's name in reverse
System.out.println("Your name in reverse is: " + lastName.reverse() + " " + firstName.reverse());
// Length of name
System.out.println("The length of my first name is " + firstName.length());
System.out.println("The length of my last name is " + lastName.length());
// Insert a goodbye into the firstName string and display to user
firstName = firstName.reverse(); // Change firstName back to the initial value
System.out.println(firstName.insert(0, "See ya later, "));
}
}
you just mean the first and last name together?
int fullNameLength = lastName.length() + firstName.length();
System.out.println("The length of my full name is " + fullNameLength);
(firstName + lastName).length()