Hello StackOverflow community, I've just begun to work with arrays and I was wondering how I can make the switch statement work with the values below. It's really irritating that it won't work considering the only error is constant expression required:
import java.lang.Character;
public class Ass11a {
public static char[] vowel = new char[4];
public static char[] consonant = new char[20];
public static char[] number = new char[9];
public static char[] punctuation = new char[10];
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter a character: ");
char input = console.readChar();
Character.toLowerCase(input);
vowel[0] = 'a';
vowel[1] = 'e';
vowel[2] = 'i';
vowel[3] = 'o';
vowel[4] = 'u';
consonant[0] = 'q';
consonant[1] = 'w';
consonant[2] = 'r';
consonant[3] = 't';
consonant[4] = 'y';
consonant[5] = 'p';
consonant[6] = 's';
consonant[7] = 'd';
consonant[8] = 'f';
consonant[9] = 'g';
consonant[10] = 'h';
consonant[11] = 'j';
consonant[12] = 'k';
consonant[13] = 'l';
consonant[14] = 'z';
consonant[15] = 'x';
consonant[16] = 'c';
consonant[17] = 'v';
consonant[18] = 'b';
consonant[19] = 'n';
consonant[20] = 'm';
number[0] = '1';
number[1] = '2';
number[2] = '3';
number[3] = '4';
number[4] = '5';
number[5] = '6';
number[6] = '7';
number[7] = '8';
number[8] = '9';
number[9] = '0';
punctuation[0] = '.';
punctuation[1] = ',';
punctuation[2] = '?';
punctuation[3] = '!';
punctuation[4] = ';';
punctuation[5] = ':';
punctuation[6] = '"';
punctuation[7] = '\'';
punctuation[8] = '(';
punctuation[9] = ')';
punctuation[10] = '-';
switch(input)
{
case vowel[0]:case vowel[1]:case vowel[2]:case vowel[3]:case vowel[4]:
System.out.println("Vowel");
break;
case consonant[0]:case consonant[1]:case consonant[2]:case consonant[3]:
case consonant[4]:case consonant[5]:case consonant[6]:case consonant[7]:
case consonant[8]:case consonant[9]:case consonant[10]:case consonant[11]:
case consonant[12]:case consonant[13]:case consonant[14]:case consonant[15]:
case consonant[16]:case consonant[17]:case consonant[18]:case consonant[19]:
case consonant[20]:
System.out.println("Consonant");
break;
case number[0]:case number[1]:case number[2]:case number[3]:case number[4]:
case number[5]:case number[6]:case number[7]:case number[8]:case number[9]:
System.out.println("Number");
break;
case punctuation[0]:case punctuation[1]:case punctuation[2]:case punctuation[3]:
case punctuation[4]:case punctuation[5]:case punctuation[6]:case punctuation[7]:
case punctuation[8]:case punctuation[9]:case punctuation[10]:
System.out.println("Punctuation");
break;
default:
System.out.println("Other");
break;
}
}
}
You could use an enum like this
public enum Vowels {
A,E,I,O,U;
static boolean isVowel(char c) {
switch (c) {
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u': return true;
}
return false;
}
static Vowels getVowel(char c) {
switch (c) {
case 'A': case 'a': return A;
case 'E': case 'e': return E;
case 'I': case 'i': return I;
case 'O': case 'o': return O;
case 'U': case 'u': return U;
}
return null;
}
}
and use it like
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while (console.hasNextLine()) {
String input = console.nextLine();
for (Character ch : input.toCharArray()) {
if (Vowels.isVowel(ch)) {
System.out.println(ch + " is vowel");
}
}
}
}
Related
I don't know how to parse this.
{
"code": 1000,
"result_msg": "Successful",
"result": "{\"birthDateAsText\":\"1999-12-3100:00:00.0\",\"birthPlace\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423,\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"civilId\":\"123\",\"firstname\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"gender\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"lastname\":\"\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\\u0423\",\"nationality\":\"\\u0423\\u0423\\u0423\\u0423\"}"
}
I tried this
if (resultObject.getInt("code") == 1000) {
JSONParse jsonParse = new JSONParse();
JSONObject object = resultObject.getJSONObject("result");
event.onSuccess(jsonParse.convertJson(object));
}
Actually I don't know how to parse and decode jsonobject. Please help me
Try this:
private static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
Uses:
JSONObject object = resultObject.getJSONObject("result");
System.out.println("Birthday: "+object.getString("birthPlace"))
Decode your response using UTF-8
try {
resp = new String(Base64.decode(response.getBytes("UTF-8"), Base64.NO_WRAP), "UTF-8");
} catch (UnsupportedEncodingException e) {
// Suppress exception (unlikely to happen in prod)
PbLogger.e("Encoding exception", "");
}
Im trying to replace each letter with a digit using the international standard letter/number mapping. I got my output to run correctly however, how do get the dashes in the phone number to appear automatically in the output? For example, if I enter 1800Flowers it prints out as 18003569377. How do I get it to print out as 1-800-3569377 without using regular expressions?
import java.util.Scanner;
public class PhoneKeypad {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//while loop keeps the program running until the user enters quit
while (true) {
System.out.println("\nEnter a phone number or quit to exit:");
String phoneNumber = input.next();
if (phoneNumber.equalsIgnoreCase("quit")) {
System.out.print("\nProgrammed by me");
return;
}
//checks if the phone number entered is at least 8 digits
if (phoneNumber.length() < 8) {
System.out.println("Invalid Phone Number");
} else {
System.out.println(getNumber(phoneNumber));
}
}
}
//method converts all letters in the phone number to digits
public static String getNumber(String phoneNumber) {
int keypadNum = 0;
for (int i = 0; i < phoneNumber.length(); i++) {
char letter = phoneNumber.charAt(i);
if (Character.isAlphabetic(letter)) {
letter = Character.toUpperCase(letter);
switch (letter) {
case 'A':
case 'B':
case 'C':
keypadNum = 2;
break;
case 'D':
case 'E':
case 'F':
keypadNum = 3;
break;
case 'G':
case 'H':
case 'I':
keypadNum = 4;
break;
case 'J':
case 'K':
case 'L':
keypadNum = 5;
break;
case 'M':
case 'N':
case 'O':
keypadNum = 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
keypadNum = 7;
break;
case 'T':
case 'U':
case 'V':
keypadNum = 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
keypadNum = 9;
break;
default:
System.out.println("Invalid phone number");
}
phoneNumber = phoneNumber.substring(0, i) + keypadNum + phoneNumber.substring(i + 1);
}
}
return phoneNumber;
}
}
Expected Output:
You could use a regular expression with String.replaceAll. Remove the leading one, group the first three digits, the second three digits and the final group of digits. Something like
public static String formatNumber(String phoneNumber) {
if (phoneNumber.startsWith("1")) {
phoneNumber = phoneNumber.substring(1);
}
return phoneNumber.replaceAll("(\\d{3})(\\d{3})(\\d+)", "1-$1-$2-$3");
}
or
public static String formatNumber(String phoneNumber) {
return phoneNumber.replaceAll("1(\\d{3})(\\d{3})(\\d+)", "1-$1-$2-$3");
}
And then call it like
System.out.println(formatNumber(getNumber(phoneNumber)));
I ran it with 1800flowers and got (as expected)
1-800-356-9377
or without regular expressions like
public static String formatNumber(String phoneNumber) {
if (phoneNumber.startsWith("1")) {
phoneNumber = phoneNumber.substring(1);
}
return "1-".concat(phoneNumber.substring(0, 3)) //
.concat("-").concat(phoneNumber.substring(3, 6)) //
.concat("-").concat(phoneNumber.substring(6));
}
Before calling formatNumber, you can remove the dashes to normalize it with something like
public static String removeDashes(String phoneNumber) {
StringBuilder sb = new StringBuilder();
for (char ch : phoneNumber.toCharArray()) {
if (ch != '-') {
sb.append(ch);
}
}
return sb.toString();
}
Then
System.out.println(formatNumber(removeDashes(getNumber(phoneNumber))));
I have this program which can make some singular nouns plural (I know I'm missing a lot, but that's aside the point). When I enter in a word such as "man-of-war" it returns as "mans-of-war" instead of "men-of-war". How do I fix this? My code can already make man to men, just not in the case I mentioned. Also for this program every compound word will have the dash, it is simply for practice.
public class LanguageUtils {
static boolean checkException(String noun){
String[] exceptions = {"fish", "fox", "deer", "moose", "sheep", "cattle","pants","scissors"};
for(int i=0;i<exceptions.length;i++) {
if(exceptions[i].equals(noun))
return true;
}
return false;
}
static boolean EnglishConsonant(char ch) {
switch (Character.toLowerCase(ch)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
return false;
default:
return true;
}
}
static String makePlural (String noun){
String pluralWord = "";
int length = noun.length();
String strippedWord = noun.substring(0, noun.length()-1);
char lastLetter = noun.charAt(noun.length()-1);
if(noun.contains("-")){
String nounsaver = noun.substring(noun.indexOf('-'), noun.length());
pluralWord = noun.substring(0,noun.indexOf('-')) + "s" + nounsaver;
}
else{
switch (lastLetter){
case 's':
case 'x':
case 'z':
if(noun.equals("ox")){
pluralWord = noun + "en";
break;
}
else{
pluralWord = noun + "es";
break;
}
case 'o':
if(EnglishConsonant(noun.charAt(noun.length()-2))){
pluralWord = strippedWord + "oes";
break;
}
case 'e':
char f = noun.charAt(noun.length()-2);
String prec = noun.substring(0, noun.length()-2);
if(f == 'f'){
pluralWord = prec + "ves";
break;
}
if(noun.equals("goose")){
pluralWord = "geese";
break;
}
else{
pluralWord = noun + "s";
break;
}
case 'h':
if ((noun.charAt(noun.length()-2)== 'c') || (noun.charAt(noun.length()-2)== 's')) {
pluralWord = noun + "es";
break;
}
case 'f':
if (EnglishConsonant(noun.charAt(noun.length()-2))) {
pluralWord = strippedWord + "ves";
break;
}
case 'y':
if (EnglishConsonant(noun.charAt(noun.length()-2))) {
pluralWord = strippedWord + "ies";
break;
}
default:
if(noun.equals("foot")){
pluralWord = "feet";
break;
}
if(noun.endsWith("man")){
pluralWord = noun.substring(0, noun.length()-3)+"men";
break;
}
else{
pluralWord = noun + "s";
break;
}
}
}
if (length == 1){
pluralWord = noun + "'s";
}
if(checkException(noun)){
pluralWord = noun;
}
return pluralWord;
}
}
When dealing with a compound word, if you need to pluralize a part, call a function designed to do just that: makePlural.
If you have - then break the words and call the makePlural for those words again.
You can use String#replace:
if (noun.contains("man")) {
pluralWord = noun.replace("man", "men");
break;
}
I am kind of relatively new to java and am working on a project for class. This is a portion of the system and i have been getting errors in this piece.
public class NumberTest
{
private static void main( String[] args )
{
}
static private char GenerateNumber()
{
int gennumber = 0;
while(gennumber < 3)
{
char returnChar = ' ';
switch(randInt(0,9))
{
case 0:
returnChar = '0';
break;
case 1:
returnChar = '1';
break;
case 2:
returnChar = '2';
break;
case 3:
returnChar = '3';
break;
case 4:
returnChar = '4';
break;
case 5:
returnChar = '5';
break;
case 6:
returnChar = '6';
break;
case 7:
returnChar = '7';
break;
case 8:
returnChar = '8';
break;
case 9:
returnChar = '9';
break;
default:
System.out.println("Error Random int outside expected values");
}
return returnChar;
System.out.println(GenerateNumber());
gennumber++;
}
}
}
The error I am receiving is:
Test.Again.java:12: error: cannot find symbol
switch(randInt(0,9)
symbol: method randInt(int,int)
location: class NumberTest
You are trying to call undeclared function randInt. It means you must first write this function in your class and then use it.
However you may use java.util.Random:
Random rand = new Random();
switch(rand.nextInt() % 10)
...
I am trying to change this from an input box into a Scanner class, but i am having trouble doing so.
Its a program that takes words and makes them into a phone number here is the code that does so. Any help would be greatly appreciated and if there is something that i can do in return i would gladly do so.
// declare imports
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
String inputString = JOptionPane.showInputDialog(inputMessage);
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++)
System.out.print(inputString.charAt(x)); {
while (inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
if (letter >= 'a' && letter <= 'z') if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
inputMessage = "Enter another set of telephone letters";
}
JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
Modify the line where you show the JOptionPane as follows,
//String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
so you could do the following,
package test;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
// ask for the phone number (in letters)
char letter;
String inputMessage = "Please enter the number in Letters " + "or enter '#' to stop the program ";
// String inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
for (int i = 0; i < inputString.length(); i++) {
System.out.print(inputString.charAt(x));
}
while (inputString != null && inputString.trim().length() > 0 && inputString.charAt(x) != '#') {
letter = Character.toUpperCase(inputString.charAt(x));
x++;
// make sure its not a number
// if (letter >= 'a' && letter <= 'z') {
if (x >= inputString.length()) {
x = 0;
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
// inputString = JOptionPane.showInputDialog(inputMessage);
System.out.println(inputMessage);
inputString = sc.nextLine();
} else if (letter >= 'A' && letter <= 'Z') {
digit++;
switch (letter) {
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7) {
break;
}
if (digit == 3) {
outputString += "-";
}
}
// }
inputMessage = "Enter another set of telephone letters";
}
System.out.println("\n" + outputString);
// JOptionPane.showMessageDialog(null, outputString, "Telephone Program", JOptionPane.PLAIN_MESSAGE);
}
}