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);
}
}
Related
I have a problem with this java script if I enter the input with spacing cant work. without spacing no problem. I have try a lot of edit but still cannot solve this problem, I wrote getloan will show the result, but if I write
get loan with spacing will show error. what I should add on to solve this?
Scanner read = new Scanner(System.in);
{
System.out.println("Enter the phone number: ");
String telLetter = read.next();
telLetter = telLetter.toUpperCase();
String telNumber = " ";
int count = 0;
int i = 0;
while (count <7)
{
switch (telLetter.charAt(i))
{
case 'A':case 'B':case 'C':
telNumber += "2";
count++;
break;
case 'D':case 'E':case 'F':
telNumber += "3";
count++;
break;
case 'G':case 'H':case 'I':
telNumber += "4";
count++;
break;
case 'J':case 'K':case 'L':
telNumber += "5";
count++;
break;
case 'M':case 'N':case 'O':
telNumber += "6";
count++;
break;
case 'P':case 'R':case 'Q':case 'S':
telNumber += "7";
count++;
break;
case 'T':case 'U':case 'V':
telNumber += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z':
telNumber += "9";
count++;
break;
}
if (count == 3)
telNumber += "-";
i++;
}
System.out.println(telNumber);
}
It fails because Scanner.next() will give you get and not get loan and you have hardcoded your loop to process 7 valid letters. Scanner by default delimits on whitespace.
You should probably use nextLine() instead of next() if you want to delimit on the new line.
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;
}
So i have created this code to convert words into phone numbers, but when i try to run this code with letters under 7 word it will display string index out of range. But 7 or more is fine. How do i fix this? If so how do i set the string range?
{
System.out.println("Enter a word to be converted: ");
String telLetter = console.next ();
telLetter = telLetter.toUpperCase();
String telNumber="7";
int count=0;
int i=0;
while(count <7)
{switch(telLetter.charAt(i))
{case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
telNumber += "2";
count++;
break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
telNumber += "3";
count++;
break;
case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
telNumber += "4";
count++;
break;
case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':
telNumber += "5";
count++;
break;
case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
telNumber += "6";
count++;
break;
case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
telNumber += "7";
count++;
break;
case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
telNumber += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
telNumber += "9";
count++;
break;
}
if( count==3) {
telNumber += "-";
}
i++;
}
System.out.println( telNumber );
}
}}
Fixes in code:
Use while(count < telLetter.length()) in place of while(count <7)...
telLetter.charAt(i) can be removed by (telLetter.charAt(count))... By doing this you don't need to create an extra variable int i = 0;... Its a good practice to keep variable minimum.
Use a scanner to get the input... Like.. Scanner sc = new Scanner(System.in); String telLetter = sc.next();...
After you used your resources do close them by using sc.close();....
Also i can see that you are appending 7 at the begining of every number... if that's a requirement then its ok.. other wise you can use... String telNumber="";
I would recommend using StringBuilder because in one object you can do it all.. While appending a character every time in string you are continuously creating a new string and giving it the reference of telNumber.
Also in your code either remove telLetter = telLetter.toUpperCase(); or remove the cases with lowercase alphabets.. because on making toUpperCase() you are just writing extra lines by writing cases for lowercase characters that are not needed...
System.out.println("Enter a word to be converted: ");
Scanner scan=new Scanner(System.in);
String telLetter = scan.next ();
int stringLength=telLetter.length();
telLetter = telLetter.toUpperCase();
String telNumber="7";
while(count <stringLength)
{
switch(telLetter.charAt(count))
Change your code like this.
But as per your code, the 7 is always prefixed in the converted phone number.
String telNumber=""; telNumber should be declared with empty string.
while(count <7)
Your code won't end until it has run through 7 characters. Try using while(count < telLetter.length())
Your while loop condition needs to check for the length of the telLetter string.
while (count <7 && count < telLetter.length())
I have a small metod. It's get string with eng characters and return rus characters string (transliteration); But it's something wrong with it. I have no idea what. It's returned not string on Russian, but some junk like "[C#4057db80";
public String getRussianSting(String engString) {
char[] engCharString = engString.toLowerCase().toCharArray();
char[] rusCharString = new char[30];
for (int i = 0; i <= engCharString.length - 1; i++) {
if (engCharString[i] == ' ')
continue;
if (i + 1 <= engCharString.length - 1) {
if (engCharString[i] == 'c' && engCharString[i + 1] == 'h') {
rusCharString[i] = 'ч';
i++;
continue;
} else if (engCharString[i] == 's' && engCharString[i + 1] == 'h') {
rusCharString[i] = 'ш';
i++;
continue;
} else if (engCharString[i] == 't' && engCharString[i + 1] == 'z') {
rusCharString[i] = 'ц';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'i') {
rusCharString[i] = 'ы';
i++;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'e') {
rusCharString[i] = 'э';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'u') {
rusCharString[i] = 'ю';
i++;
continue;
} else if (engCharString[i] == 'y' && engCharString[i + 1] == 'a') {
rusCharString[i] = 'я';
i++;
continue;
}
}
switch (engCharString[i]) {
case 'a':
rusCharString[i] = 'а';
break;
case 'b':
rusCharString[i] = 'б';
break;
case 'v':
rusCharString[i] = 'в';
break;
case 'g':
rusCharString[i] = 'г';
break;
case 'd':
rusCharString[i] = 'д';
break;
case 'e':
rusCharString[i] = 'е';
break;
case 'j':
rusCharString[i] = 'ж';
break;
case 'z':
rusCharString[i] = 'з';
break;
case 'i':
rusCharString[i] = 'и';
break;
case 'k':
rusCharString[i] = 'к';
break;
case 'l':
rusCharString[i] = 'л';
break;
case 'm':
rusCharString[i] = 'м';
break;
case 'n':
rusCharString[i] = 'н';
break;
case 'o':
rusCharString[i] = 'о';
break;
case 'p':
rusCharString[i] = 'п';
break;
case 'r':
rusCharString[i] = 'р';
break;
case 's':
rusCharString[i] = 'с';
break;
case 't':
rusCharString[i] = 'т';
break;
case 'u':
rusCharString[i] = 'у';
break;
case 'f':
rusCharString[i] = 'ф';
break;
case 'h':
rusCharString[i] = 'х';
break;
case '\'':
rusCharString[i] = 'ь';
break;
default:
break;
}
}
return rusCharString.toString();
}
return rusCharString.toString();
will return the String showing the object representation of the array. That is one major reason char[] is used for sensitive data instead of String.
try:
return new String(rusCharString);
The problem is that when you use the toString of an array, it will use the one that it inherits from Object class.
If you want to get a proper toString of an array, use java.util.Arrays.toString(rusCharString). The junk that you see is the hash code of that array.
If you want to print the hash code separately, use rusCharArray.hashCode()