I want to generate a unique term that is one letter followed by two numbers, and then I want to check that it is not in a database. I have tried to use a do, followed by a while, but I cannot get it to work. I get an java: illegal start of expression error. Can you see what I am doing wrong? Alternatively, is there a better way to do this?
Here is my code:
public String setNumber(ReportType type) {
Random rand = new Random();
String number = String.format("%02d", rand.nextInt(100));
String prefix = "";
do {
switch (type) {
case CHILD:
prefix = "A";
break;
case ELDER:
prefix = "B";
break;
case BOOMER:
prefix = "C";
break;
}
String fullNumber = prefix + number;
while (dataBase.findInDataBase(fullNumber)) throws FileNotFoundException);
}
return fullNumber;
}
There are two syntax errors in your code:
The curly brace starting after do must end before while, not after it.
throws FileNotFoundException does not belong where it stands. You may have copied it from the declaration of the findInDataBase method or elsewhere. Just delete it.
Edit:
findInDataBase throws a FileNotFoundException exception when an
item is not found in the database. It does not return a boolean. When
nothing is found, I want to return the confirmed unique term. How do I
complete the while statement?
Use a try-catch construct to see if the exception is thrown. And a boolean variable to control the loop.
public String setNumber(ReportType type) {
Random rand = new Random();
String number = String.format("%02d", rand.nextInt(100));
String prefix = "";
String fullNumber;
boolean found;
do {
switch (type) {
case CHILD:
prefix = "A";
break;
case ELDER:
prefix = "B";
break;
case BOOMER:
prefix = "C";
break;
}
fullNumber = prefix + number;
try {
dataBase.findInDataBase(fullNumber);
found = true;
} catch (FileNotFoundException fnfe) {
found = false;
}
} while (found);
return fullNumber;
}
Alternative: The following might start an intense discussion since some find that it’s bad style. It simplifies a few things since it relieves us of having the two uninitialized variables declared before the loop.
public String setNumber(ReportType type) {
Random rand = new Random();
String number = String.format("%02d", rand.nextInt(100));
String prefix = "";
// This loop will be terminated by a return statement inside of it.
while (true) {
switch (type) {
case CHILD:
prefix = "A";
break;
case ELDER:
prefix = "B";
break;
case BOOMER:
prefix = "C";
break;
}
String fullNumber = prefix + number;
try {
dataBase.findInDataBase(fullNumber);
} catch (FileNotFoundException fnfe) {
// Nothing is found; return the confirmed unique term:
return fullNumber;
}
}
}
You are not out of problems yet. You will need to draw a new random number from rand every time through the loop. Otherwise if the first number drawn is already in the database, you are in an infinite loop. If all 100 numbers from 00 through 99 are in use, you will be anyway.
Note: Since Java SE 14*, you can use the switch expression instead of switch statement.
do {
prefix = switch (type) {
case CHILD -> "A";
case ELDER -> "B";
case BOOMER -> "C";
};
fullNumber = prefix + number;
} while (dataBase.findInDataBase(fullNumber));
* The switch expression was introduced with Java SE 12. However, it remained as a Preview feature in Java SE 12 and 13 and finally got standardized with Java SE 14. You can check this Q/A to learn more about it.
Related
I'm reviewing some code for a college assignment and we've been given examples to assist us.
I'm little confused on what the below is doing as it's using the assignment operator instead of the .equals or == method.
If I replace the code with the == (and create a local variable to compare it to) the code starts infinite looping and displaying out the default value.
int select = 0;
do {
switch (select) {
case 1:
Problem();
break;
default:
System.out.println("Invalid");
break;
}
} while ((select = getSelection()) !=3);
public static int getSelection () {
(Return function here with has.nextInt and scanner class to receive input)
}
From my limited understanding, the above assigns "Select" to the value from the "getSelection" method, it's also stating do not accept inputs that are 3 e.g. System.exit0 at this point.
Have I understood correctly?
(Further example as requested)
I would do something along the lines of:
int select = 0;
int select1 = 0;
do {
switch (select) {
case 1:
Problem();
break;
default:
System.out.println("Invalid");
break;
}
} while (select == select1);
I am attempting to think of a logical equivalent to the lecturers example but seem to be unable to do this without breaking the while loop.
In java, (and other "C like" languages) the result of an assignment is the value assigned, ie this code:
do {
// other code
} while ((select = getSelection()) !=3)
is the same as:
do {
// other code
select = getSelection();
} while (select != 3)
This style, known as in-line conditional, is generally considered a style to be avoided.
There is a checkstyle violation for it - see AvoidInlineConditionals
So Im splitting at " ". Problem is that morsecode is dividing each word by three spaces. " ".
So when Im translating it always prints out each word written together without spaces inbetween.
Heres what I have. Im using StringBuilder for obvious reasons.
public class MorseCodeDecoder {
public static String decode(String morseCode) {
String word = "";
String character = "";
//count how much space is inbetween lines.
StringBuilder codeTwo = new StringBuilder();
String[] output = morseCode.split(" ");
for (int i = 0; i < output.length; i++) {
Inside here I tried to do an seperate if-statement. if morseCode contains three spaces back to back append one space.
//if(morseCode.contains(" ")) codeTwo.append(" "); (or \\s maybe, doesnt matter)
switch (output[i]) {
case " ":
codeTwo.append(" ");
break;
case ".-":
codeTwo.append("A");
break;
case "-...":
codeTwo.append("B");
break;
case "-.-.":
codeTwo.append("C");
break;
case "-..":
codeTwo.append("D");
break;
case ".":
codeTwo.append("E");
break;
case "..-.":
codeTwo.append("F");
break;
case "--.":
codeTwo.append("G");
break;
case "....":
codeTwo.append("H");
break;
case "..":
codeTwo.append("I");
break;
case ".---":
codeTwo.append("J");
break;
case "-.-":
codeTwo.append("K");
break;
case ".-..":
codeTwo.append("L");
break;
case "--":
codeTwo.append("M");
break;
case "-.":
codeTwo.append("N");
break;
case "---":
codeTwo.append("O");
break;
case ".--.":
codeTwo.append("P");
break;
case "--.-":
codeTwo.append("Q");
break;
case ".-.":
codeTwo.append("R");
break;
case "...":
codeTwo.append("S");
break;
case "-":
codeTwo.append("T");
break;
case "..-":
codeTwo.append("U");
break;
case "...-":
codeTwo.append("V");
break;
case ".--":
codeTwo.append("W");
break;
case "-..-":
codeTwo.append("X");
break;
case "-.--":
codeTwo.append("Y");
break;
case "--..":
codeTwo.append("Z");
break;
}
}
return codeTwo.toString();
}
}
For input: .... . -.-- .--- ..- -.. .
Expected output: "HEY JUDE"
You can use regex instead of splitting by spaces...
Something like this pattern:
([._])+\s*
Explanation: We're looking for at least 1 instance of a dot (.) or underscore (_) followed by zero or more spaces...
I've used it over this example:
._ _... _._ .._ ..._ .__
Which is: ABC UVW
I'm not near a computer - but the program should look something like that:
public static String decode(final String morseCode) {
final Matcher matcher = pattern.matcher(morseCode);
final StringBuilder builder = new StringBuilder();
while(matcher.find()) {
builder.append(getLetter(matcher.group().trim()));
}
return builder.toString();
}
Assuming getLetter() is your switch case.
More of a nitpick - instead of using this massive switch case you should use a static map that will contain the mapping between the symbols and letters and initialize it on the static constructor ( https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html )
Hope that helps.
Your problem is that since your letters and your words share a delimiter, when you split by a single space, you are effectively removing your word spacing completely, so the switch statement never gets a chance to read them. There are two solutions I can imagine. The first is the code I have below. Since morse code only uses a specific set of characters, you can simply replace your word delimiter with a character outside of the alphabet and then detect that instead. For the second possible solution, if you would like to keep the integrity of the input, then you could also first split by the three spaces to separate each word, and then use your switch statement on each word, appending a space after each word has been successfully translated. This would require two loops (outer loop for the words and inner loop for the letters).
public class MorseCodeDecoder {
public static String decode(String morseCode) {
String word = "";
String character = "";
//count how much space is inbetween lines.
StringBuilder codeTwo = new StringBuilder();
morseCode.replace(" ", "|");
String[] output = morseCode.split(" ");
for (int i = 0; i < output.length; i++) {
switch (output[i]) {
case "|":
codeTwo.append(" ");
break;
case ".-":
codeTwo.append("A");
break;
//etc.
}
return codeTwo.toString();
}
}
I'm attempting to get this to recognize the leading zeros in my program, and I thought using 'String.format("%03d", code);' would take care of it but I'm still not getting the expected result.
import java.util.Scanner;
import java.io.*;
public class Main{
public static void main(String args[]){
Scanner sc =new Scanner(System.in);
System.out.println("Enter the shipment code :");
int code = sc.nextInt();
String.format("%03d", code);
// fill the code
if( code == 111 ){
System.out.println("All ways");
}
else if( code == 110){
System.out.println("Airway and Waterway");
}
else if( code == 011){
System.out.println("Waterway and Roadway");
}
else if( code == 010){
System.out.println("Waterway");
}
else if( code == 101){
System.out.println("Airway and Roadway");
}
else if(code == 001){
System.out.println("Roadway");
}
}
}
You're doing something wrong here.
011, 010, 001 are octal numbers, as they start with a zero.
Also, using String.format is pointless here, as the resulting value is not used.
This might be why your if branches aren't taken into consideration.
final String formattedValue = String.format("%03d", code);
Now you can use formattedValue as a comparison value for your if statements.
Example
if ("111".equals(formattedValue)) { ... }
Note that maybe transforming the int into a String isn't necessary. But in case you insist on doing so, a good practice is to use a constant String as the operand which calls equals(...).
You're discarding the formatted value. You need to store it in a variable and compare it as string:
String formatted = String.format("%03d", code);
if( formatted.equals("111") ){
System.out.println("All ways");
}
// ...
Well, String.format("%03d", code), returns a string, and you are comparing to integers (octal integers, as LppEdd pointed out).
You should store the formatted string to a variable, e.g.
String formatted = String.format("%03d", code);
and then compare it to Strings in your if/else statements, like so:
if(formatted.equals("011")) {...}
Don't format and remove any leading 0's in the condition and use switch
int code = sc.nextInt();
// fill the code
switch(code) {
case 111:
System.out.println("All ways");
break;
case 110:
System.out.println("Airway and Waterway");
break;
case 11:
System.out.println("Waterway and Roadway");
break;
case 10:
System.out.println("Waterway");
break;
case 101:
System.out.println("Airway and Roadway");
break;
case 1:
System.out.println("Roadway");
break;
default:
System.out.println("Unknown code " + code);
break;
}
I was trying to prepare a flow chart of program for my practice where i encounter that following situation is required for me where a user would enter a string value as input either "Yes"or "Y" and "No" or "N" and based on its input the application would either terminate or restart from a certain point till now i have this as an example in my mind
public class ApplicationName {
public static void main(String args[]) {
String restartOperation;
do {
restartOperation = Confirm_Before_Exit();
} while (!restartOperation.equals("No"));
//Rest of code
}
public static void Some_Operation() {
//Executed when called before closing application
}
public static String Confirm_Before_Exit() {
Scanner inputData = new Scanner(System.in);
String answer;
System.out.println("Do you want to perform another operation ?" + " " + "Y" + " " + "N");
answer = inputData.nextLine();
switch (answer) {
case "Y":
case "Yes":
Some_Operation();
break;
default:
System.out.println("Good Bye !");
}
return answer;
}
}
This works till the user has not given input as "No" but obviously it wont work if entered "N" or perhaps small "n" or even "no" but for timing i am only trying for "No" and "N" as input value.
change your do while to the following :
do {
restartOperation = Confirm_Before_Exit();
} while (!restartOperation.equalsIgnoreCase("No") && !restartOperation.equalsIgnoreCase("n"));
I would do something like this. You make your answer lowercase to take into account cases like YEs, NO, etc. You then specify the n and no.The default should be a catch all.
answer = inputData.nextLine().toLowerCase();
switch (answer) {
case "y":
case "yes":
Some_Operation();
break;
case "n":
case "no":
System.out.println("Good Bye !");
break;
default:
System.out.println("Not a valid reponse");
}
You may want to use Regex for text matching (it is contained in the default Java-JDK, not an external library). It is extremely powerful.
You define a needle to search for in Regex-syntax, for example this:
^no?$
It matches text that starts (^) with n and then eventually (?) followed by o (but it does not need to be there), after that the text ends ($). There's also a flag for case insensitive matching /i. You can try Regex at regex101.com. Try this: regex101.com/r/edrehj
Okay, how to use this stuff in Java? Fairly easy:
String input = ...
Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
// User want's to cancel
...
} else {
// User want's to continue
...
}
More info at Wikipedia#Regex, Java-API#Pattern and Java-API#Matcher.
You can also put this inside an own method, makes the usage more easy and user friendly to read:
private boolean doesUserWantToCancel(final String textInput) {
Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textInput);
return matcher.find();
}
You then just call the method for your input:
...
} while (!doesUserWantToCancel(restartOperation));
...
If I am understanding your question correctly you want to run Confirm_Before_Exit() when you are restarting an operation?
Here is what I would do. Instead of having string restartOperation you should have boolean restartOperation. This way you either deal with true or false.
In the Confirm_Before_Exit() function I would still add the .toLowerCase() to your inputData to take into account weird cases. I added two booleans answer and inValidAnswer.
You want your answer to be true if you want to perform another operation. If it is false it will exit.
If the user mistypes, inValideAnswer will be set to true and they will get an error saying invalid answer and will get reprompted.
public class ApplicationName {
public static void main(String args[]) {
boolean restartOperation;
do {
restartOperation = Confirm_Before_Exit();
} while (restartOperation);
//Rest of code
}
public static void Some_Operation() {
//Executed when called before closing application
}
public static boolean Confirm_Before_Exit() {
Scanner inputData = new Scanner(System.in);
boolean answer;
boolean validAnswer;
while(inValideAnswer){
System.out.println("Do you want to perform another operation ?" + " " + "Y" + " " + "N");
string userInput = inputData.nextLine().toLowerCase();
switch (userInput) {
case "y":
case "yes":
Some_Operation();
answer = true;
inValideAnswer = false;
break;
case "n":
case "no":
answer = false;
inValideAnswer = false;
System.out.println("Good Bye !");
break;
default:
System.out.println("Not a valid reponse");
inValideAnswer = true;
}
}
return answer;
}
}
just to clarify this is hw.
In a project we're doing, a user isn't allowed to enter numbers or special characters (i.e ! # £ etc)
char letter;
String phonetic;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = letter = kb.next().charAt(0);
switch(Character.toUpperCase(letter))
{
case 'A':
{
Dot();
Dash();
Red();
}
break;
case '1,2,3,4,5,6,7,8,9,0':
{
System.out.println('No number input please!');
}
break;
}
The error is on
'1,2,3,4,5,6,7,8,9,0'
Eclipse says
invalid character constant
Isn't it really long winded if I have to enter all the numbers manually?
i.e. case '1': case '2':
even with
case 1,2,3,4,5,6,7,8,9,0:
It won't work.
Is there an shorter way to do this using switch statements?
Thank you!
Its because Case expression should be an int-compatible literal or a String from java 7.
case '1,2,3,4,5,6,7,8,9,0':
character literals are represented using single quotes. c, it should only be of one length, while your case doesn't reflect that, thus the error.
'1,2,3,4,5,6,7,8,9,0' this is not a legal character.
If you just wanna check if the character is only alpha, then use Charcter#isDigit(char) or Charcter#isLetter before the switch starts like in below code:
char ch= (Character.toUpperCase(letter);
if(!Character.isDigit(ch)) {
switch(Character.toUpperCase(letter))
{
case 'A':
{
Dot();
Dash();
Red();
}
break;
}
}
else {
System.out.println("no numbers please")
}
There's no easier way using case, what about?:
if ('0' <= letter && letter <= '9')
System.out.println('No number input please!');
Isn't it really long winded if I have to enter all the numbers manually?
Yes.
Is there an shorter way to do this using switch statements?
No.
Consider an if statement instead...
No, Java in this situation is not smart like C#. You need to write multiple lines for that. If you want to compare strings you need to use if statements. Also remember to use this code for comparision:
if("search".equals(string2)) {...}
You cannot compare by == this would only compare the memory addresses. Also note that I use the equals on the static string and not on the variable string2 because you code would break if string2 is null.
I hope this one enlighten you more.
Consider, your expression generates output as A , B, C, D, E, F, G, H, I, ...till Z. and you want to execute same method/function for all them.
Then, you can check the ascii values of the characters and modify your code to use if and for loop or else use switch as mentioned in the program in following example program.
Play around with code to learn more.
public class SwitchClass
{
public void method1()
{
System.out.println("Menthod 1");
}
public void method2()
{
System.out.println("Menthod 2");
}
public void method3()
{
System.out.println("Menthod 3");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
SwitchClass sw = new SwitchClass();
System.out.println("Enter the String:");
String input = in.next();
for(int i = 0; i<input.length(); i++)
{
switch(Character.toUpperCase(input.charAt(i)))
{
case 'A':
case 'B':
case 'C':
case 'U':
System.out.println(Character.toUppercase(input.charAt(i))+" Case calling");
sw.method1();
sw.method2();
sw.method3();
break;
default:
System.out.println("No number input please!");
break;
}
}
}
}