printing special characters from char - java

I'm writing a program converts letters in a phone number into their corresponding digits, and it's almost complete aside from some formatting:
import java.util.Scanner;
public class PhoneKeypad
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
String number;
int currentChar;
char character;
int finalNumber;
int getNumber;
int start;
String end;
currentChar = 0;
do
{
System.out.print ("\nEnter a phone number with letters: ");
number = scan.nextLine();
number = number.toUpperCase();
while (currentChar < number.length())
{
character = number.charAt(currentChar);
finalNumber = getNumber(character);
System.out.print (finalNumber);
currentChar++;
}
System.out.print ("\n\nContinue? <y/n> ");
end = scan.next();
System.out.flush();
scan.nextLine();
currentChar = 0;
} while (!end.equalsIgnoreCase("n"));
}
public static int getNumber (char uppercaseLetter)
{
int resultChar = 0;
if (Character.isLetter(uppercaseLetter))
{
if (uppercaseLetter == 'A' || uppercaseLetter == 'B' || uppercaseLetter == 'C')
resultChar = 2;
else if (uppercaseLetter == 'D' || uppercaseLetter == 'E' || uppercaseLetter == 'F')
resultChar = 3;
else if (uppercaseLetter == 'G' || uppercaseLetter == 'H' || uppercaseLetter == 'I')
resultChar = 4;
else if (uppercaseLetter == 'J' || uppercaseLetter == 'K' || uppercaseLetter == 'L')
resultChar = 5;
else if (uppercaseLetter == 'M' || uppercaseLetter == 'N' || uppercaseLetter == 'O')
resultChar = 6;
else if (uppercaseLetter == 'P' || uppercaseLetter == 'Q' || uppercaseLetter == 'R' || uppercaseLetter == 'S')
resultChar = 7;
else if (uppercaseLetter == 'T' || uppercaseLetter == 'U' || uppercaseLetter == 'V')
resultChar = 8;
else if (uppercaseLetter == 'W' || uppercaseLetter == 'X' || uppercaseLetter == 'Y' || uppercaseLetter == 'Z')
resultChar = 9;
}
else if (Character.isDigit(uppercaseLetter))
{
resultChar = Character.getNumericValue(uppercaseLetter);
}
return resultChar;
}
}
The issue I'm having is that I need to print the phone number back out exactly as the user entered it, aside the the letters. If the user enters 1-800-flowers, the program needs to print it back out as 1-800-3569377. It's the same for any special characters - hyphens, spaces, parenthesis, etc.
And the simpler the solution, the better, as I haven't yet gotten to arrays or anything more complicated than methods.

Related

How can I make a loop keep going after the first condition returns true

I am trying to write a method that will take a string, convert any letters to an int, and return all the converted ints to main, replacing the letters . I have if statements that convert all the letters to numbers, but I am having trouble making it work with a loop to convert all the letters instead of stopping after the first one. Any help would be appreciated, thanks in advance.
public class PhoneNumberChecker
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Get the phone number
System.out.print("Phone number to convert: ");
String phoneNumber = input.nextLine();
// Process each character in the phone number for display
for (int i = 0; i < phoneNumber.length(); ++i)
{
// Get the character
char ch = phoneNumber.charAt(i);
if (Character.isLetter(ch))
ch = (Character.toUpperCase(ch));
else
System.out.print(ch);
}
System.out.println(getNumber(phoneNumber));
input.close();
// end method
}
public static String getNumber(String phoneNumber)
{
for (int i = 0; i < phoneNumber.length(); ++i)
{
char ch = phoneNumber.charAt(i);
ch = Character.toUpperCase(ch);
if (ch == 'A' || ch == 'B' || ch == 'C')
return "2";
else if
(ch == 'D' || ch == 'E' || ch == 'F')
return "3";
else if
(ch == 'G' || ch == 'H' || ch == 'I')
return "4";
else if
(ch == 'J' || ch == 'K' || ch == 'L')
return "5";
else if
(ch == 'M' || ch == 'N' || ch == 'O')
return "6";
else if
(ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
return "7";
else if
(ch == 'T' || ch == 'U' || ch == 'V')
return "8";
else if
(ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
return "9";
}
return "";
}
}
You want to append the string results to a string that will continue to grow as you iterate over the given phone number.
Create a String variable before your loop, then simply append to that string instead of returning the strings. Then once you're done iterating the phone number you can return the String.
public static String getNumber(String phoneNumber){
String convertedNum = "";
for (int i = 0; i < phoneNumber.length(); ++i)
char ch = phoneNumber.charAt(i);
ch = Character.toUpperCase(ch);
if (ch == 'A' || ch == 'B' || ch == 'C')
convertedNum = convertedNum + "2"; //append to the string
else if(ch == 'D' || ch == 'E' || ch == 'F')
convertedNum = convertedNum + "3";
...
return convertedNum; //then return it at the end
}
You return from the method after the first character was handled. Let's modify your method:
public static String getNumber(String phoneNumber, int i)
{
//for (int i = 0; i < phoneNumber.length(); ++i)
{
char ch = phoneNumber.charAt(i);
ch = Character.toUpperCase(ch);
if (ch == 'A' || ch == 'B' || ch == 'C')
return "2";
else if
(ch == 'D' || ch == 'E' || ch == 'F')
return "3";
else if
(ch == 'G' || ch == 'H' || ch == 'I')
return "4";
else if
(ch == 'J' || ch == 'K' || ch == 'L')
return "5";
else if
(ch == 'M' || ch == 'N' || ch == 'O')
return "6";
else if
(ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
return "7";
else if
(ch == 'T' || ch == 'U' || ch == 'V')
return "8";
else if
(ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
return "9";
}
return "";
}
Note, that it has an int parameter and the cycle was commented out. Now, let's process a String:
public static function parseString(String input) {
String output = "";
for (int i = 0; i < input.length; i++) {
output += getNumber(input, i);
}
return output;
}
Note, that this is very simple to understand. The thing which makes it simple is the fact that a method is doing a single thing. getNumber gets a number from a String at a given index. parseString parses the String in the way your code suggested. Of course you can modify the initial String if that is the purpose, using setChar, but then the getNumber method should return the char representation of the digits.
As an alternative you could use String.relaceAll instead of checking each char in a nested if-else. Example:
public static String getNumber(String phoneNumber){
String result = phoneNumber.toUpperCase()
.replaceAll("[A-C]", "2")
.replaceAll("[D-F]", "3")
.replaceAll("[G-I]", "4")
.replaceAll("[J-L]", "5")
.replaceAll("[M-O]", "6")
.replaceAll("[P-S]", "7")
.replaceAll("[T-V]", "8")
.replaceAll("[X-Z]", "9");
return result;
}
I would suggest you to use StringBuilder as compared to String as it is preferable performance wise compared to String. The reason is String is immutable. So inside the loop the String object will be created again and again. Whereas StringBuilder is mutable so it is declared only once and then can be operated on by it's reference. You can use it as shown below:
public static String getNumber(String phoneNumber){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < phoneNumber.length(); ++i){
char ch = phoneNumber.charAt(i);
ch = Character.toUpperCase(ch);
if (ch == 'A' || ch == 'B' || ch == 'C')
sb.append("2");
else if(ch == 'D' || ch == 'E' || ch == 'F')
sb.append("2");
else if(ch == 'G' || ch == 'H' || ch == 'I')
sb.append("3");
else if(ch == 'J' || ch == 'K' || ch == 'L')
sb.append("4");
else if(ch == 'M' || ch == 'N' || ch == 'O')
sb.append("5");
}
return sb.toString();
}
You can read about performance of String vs StringBuilder here. Pay attention to switch from concatination(+) to Builder.

Need the phone number to compile woth a phrase and not just the number

This is my code
import java.util.*;
public class PhoneKeypad {
public static void main(String[] args){
System.out.print("Enter an uppercase letter ");
Scanner input = new Scanner(System.in);
String phNumber = input.next();
String output = "";
for(int i = 0 ; i < phNumber.length() ; i++){
char ch = Character.toUpperCase(phNumber.charAt(i));
if(Character.isLetter(ch)){
int digit = getNumber(ch);
output = output + digit;
}
else{
output = output + ch;
}
}
System.out.println(output);
}
public static int getNumber(char upperCaseLetter){
if(upperCaseLetter == 'A' || upperCaseLetter == 'B'
|| upperCaseLetter == 'C')
return "The Corresponding number is 2";
else if(upperCaseLetter == 'D' || upperCaseLetter == 'E'
|| upperCaseLetter == 'F')
return "The Corresponding number is 3";
else if(upperCaseLetter == 'G' || upperCaseLetter == 'H'
|| upperCaseLetter == 'I')
return "The Corresponding number is 4";
else if(upperCaseLetter == 'J' || upperCaseLetter == 'K'
|| upperCaseLetter == 'L')
return "The Corresponding number is 5";
else if(upperCaseLetter == 'M' || upperCaseLetter =='N'
|| upperCaseLetter == 'O')
return 6 "The Corresponding number is 6";
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
|| upperCaseLetter == 'R')
return "The Corresponding number is 7";
else if(upperCaseLetter == 'S' || upperCaseLetter =='T'
|| upperCaseLetter == 'U')
return "The Corresponding number is 8";
else if(upperCaseLetter == 'V' || upperCaseLetter == 'W'
|| upperCaseLetter == 'Y' || upperCaseLetter == 'Z')
return "The Corresponding number is 9";
else
return 0;
}
}
This gives me errors. I need the output to say the corresponding number is _
and not just the number
These are the types of errors I am getting
PhoneKeypad.java:67: error: ';' expected
return 6 "The Corresponding number is 6";
^
PhoneKeypad.java:69: error: 'else' without 'if'
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: illegal start of type
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: <identifier> expected
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: ';' expected
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: illegal start of type
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: <identifier> expected
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: ';' expected
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: <identifier> expected
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
PhoneKeypad.java:69: error: illegal start of type
else if(upperCaseLetter == 'P' || upperCaseLetter == 'Q'
^
Look at line 67. You have return 6 "The Corresponding number is 6"; when you should have return "The Corresponding number is 6";
If you want to return Strings instead of ints just change the method signature.
i.e. public static String getNumber(char upperCaseLetter){ instead of public static int getNumber(char upperCaseLetter){
I would recommend downloading an IDE such as "Eclipse" to help you write your code. It will pinpoint a lot of the errors that are present in your code.
public static int getNumber(char upperCaseLetter)
As for your question, remove the intand replace it with either 'String' or 'void'
Your return values for getNumber are Strings, but the method, as well as your main method, expects an int value. For example, the following should work better:
public static int getNumber(char upperCaseLetter) {
if (
upperCaseLetter == 'A'
|| upperCaseLetter == 'B'
|| upperCaseLetter == 'C') {
return 2;
} else if (
upperCaseLetter == 'D'
|| upperCaseLetter == 'E'
|| upperCaseLetter == 'F') {
return 3;
}
//etc.
return 0;
}
However, since you use that many if / else if in this method, you could also consider using a switch case:
public static int getNumber2(char upperCaseLetter) {
switch (upperCaseLetter) {
case 'A':case 'B':case 'C':
return 2;
case 'D':case 'E':case 'F':
return 3;
//etc.
default:
return 0;
}
}

how i print vowels and what they are?

Everytime i input my sentence it prints out the outcome each time it goes through the loop. i assume i have to put the printlines outside the loop?
import java.util.*;
public class homework4{
public static void main(String[] args) {
//Scanner
Scanner keyBd = new Scanner(System.in);
System.out.println("Enter a sentence ");
String userIn = keyBd.nextLine();
int count = 0;
String empty= "";
//Code
for (int i = 0; i < userIn.length(); i++) {
char ch = userIn.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
System.out.println("There are " + count + " vowels in this string");
}
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
empty += ch + " ";
System.out.println("The vowels are: " + empty);
}
}
}
}
import java.util.*;
public class homework4{
public static void main(String[] args) {
//Scanner
Scanner keyBd = new Scanner(System.in);
System.out.println("Enter a sentence ");
String userIn = keyBd.nextLine();
int count = 0;
String empty= "";
//Code
for (int i = 0; i < userIn.length(); i++) {
char ch = userIn.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
empty += ch + " ";
}
}
System.out.println("There are " + count + " vowels in this string");
System.out.println("The vowels are: " + empty);
}
}
No need to check condition two times. As you are updating variables (count & empty) in loop, have to print only once after exiting from loop.
you just need to move the print statement outside and put a check condition if the count is still zero then it means there were no vowels and if count is not zero you can print it.
import java.util.Scanner;
public class homework4 {
public static void main(String[] args) {
//Scanner
Scanner keyBd = new Scanner(System.in);
System.out.println("Enter a sentence ");
String userIn = keyBd.nextLine();
int count = 0;
String empty = "";
//Code
for (int i = 0; i < userIn.length(); i++) {
char ch = userIn.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
count++;
empty += ch + " ";
}
}
if(count == 0){
System.out.println("There are no vowels in the input string");
}else {
System.out.println("There are " + count + " vowels in this string");
System.out.println("The vowels are: " + empty);
}
}
}
You don't need to test for vowels twice? (and add to count twice), only once:-
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
count++;
empty += ch + " ";
}
And your print statement doesn't need to happen every time you find a vowel:-
for (int i = 0; i < userIn.length(); i++) {
// not in here
}
System.out.println("There are " + count + " vowels in this string\n" + "The vowels are: " + empty);
Additionally...
If statements are ugly here, where there are many conditions. A switch would be easier to read and more efficient:-
switch (ch){
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
count++;
empty += ch + " ";
break;
}
Or move the whole condition into a method
public boolean isVowel(char c){
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}
and use
if (isVowel(ch)){
//...
}

Convert Uppercase Letter Input to a specific Number

I'm super new to programming so I would love to keep this simple. The compiler accepts my code, but when I run the program and type in for example the letter A I just get a ton of errors. I tried earlier using String letter instead of int letter, but I just got compiler errors stating I couldn't convert Strings to characters or something. I'm really confused and could use a quick explanation and fix so I can get a number back. Here's my code:
import java.util.Scanner;
import java.lang.String;
public class PhoneAlgorithm {
public static void main(String[] args){
int digit = -1;
Scanner in;
in = new Scanner(System.in);
System.out.print("Enter an uppercase letter to find out the corresponding digit on a telephone: ");
int letter;
letter = Integer.parseInt(in.next());
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = 2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = 3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = 4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = 5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = 6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = 7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = 8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = 9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
if (digit != -1) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}
When you use parseInt(str), you will get an Exception if the parameter str cannot be converted to an integer.
You must use char, since you are comparing the input with single characters:
char letter;
letter = in.nextLine().charAt(0);
str.charAt(index) Returns the char value at the specified index.
I have modified your code, I guess this is what you are looking for..
import java.util.Scanner;
public class Try {
public static void main(String[] args) {
//declarations
char letter;
int digit=0;
// Asking the user to enterstring
System.out.println("Enter the string");
String enterString;
//creating a scanner object and reading the string
Scanner input = new Scanner(System.in);
enterString= input.next();
System.out.println("Entered string is "+enterString);
int temp=0;
for(int i=0;i<enterString.length();i++){
letter=(char)enterString.codePointAt(i);
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = digit*10+2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = digit*10+3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = digit*10+4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = digit*10+5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = digit*10+6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = digit*10+7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = digit*10+8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = digit*10+9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
/*if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}*/
}
if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}

Not getting any output when trying to convert letters in a message to numbers

I am trying to make a program where the user inputs a message and the program converts it into numbers (eg a=1, b=2, etc). However, I don't get any output! Any help would be appreciated. My code is below.
import java.io.PrintStream;
import java.util.*;
public class AllRand25 {
public static void main(String[] args) {
int[] letterArray;
int[] wordArray;
int l = 0;
String messageWord;
char messageChar;
letterArray = new int[26];
int[] messageArray = new int[10];
int y = 0;
System.out.print("Enter your message");
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();
while (scanner.hasNext()) {
messageWord = scanner.next(message);
l = messageWord.length();
Scanner charScan = new Scanner(messageWord);
for(int i = 0; i < messageWord.length(); i++){
messageChar = messageWord.charAt(i);
if (messageChar == 'a' || messageChar == 'A' || messageChar == 'A') {
messageArray[y] = 1;
} else if (messageChar == 'b' || messageChar == 'B') {
messageArray[y] = 2;
} else if (messageChar == 'c' || messageChar == 'C') {
messageArray[y] = 3;
} else if (messageChar == 'd' || messageChar == 'D') {
messageArray[y] = 4;
} else if (messageChar == 'e' || messageChar == 'E') {
messageArray[y] = 5;
} else if (messageChar == 'f' || messageChar == 'F') {
messageArray[y] = 6;
} else if (messageChar == 'g' || messageChar == 'G') {
messageArray[y] = 7;
} else if (messageChar == 'h' || messageChar == 'H') {
messageArray[y] = 8;
} else if (messageChar == 'i' || messageChar == 'I') {
messageArray[y] = 9;
} else if (messageChar == 'j' || messageChar == 'J') {
messageArray[y] = 10;
} else if (messageChar == 'k' || messageChar == 'K') {
messageArray[y] = 11;
} else if (messageChar == 'l' || messageChar == 'L') {
messageArray[y] = 12;
} else if (messageChar == 'm' || messageChar == 'M') {
messageArray[y] = 13;
} else if (messageChar == 'n' || messageChar == 'N') {
messageArray[y] = 14;
} else if (messageChar == 'o' || messageChar == 'O') {
messageArray[y] = 15;
} else if (messageChar == 'p' || messageChar == 'P') {
messageArray[y] = 16;
} else if (messageChar == 'q' || messageChar == 'Q') {
messageArray[y] = 17;
} else if (messageChar == 'r' || messageChar == 'R') {
messageArray[y] = 18;
} else if (messageChar == 's' || messageChar == 'S') {
messageArray[y] = 19;
} else if (messageChar == 't' || messageChar == 'T') {
messageArray[y] = 20;
} else if (messageChar == 'u' || messageChar == 'U') {
messageArray[y] = 21;
} else if (messageChar == 'v' || messageChar == 'V') {
messageArray[y] = 22;
} else if (messageChar == 'w' || messageChar == 'W') {
messageArray[y] = 23;
} else if (messageChar == 'x' || messageChar == 'X') {
messageArray[y] = 24;
} else if (messageChar == 'y' || messageChar == 'Y') {
messageArray[y] = 25;
} else if (messageChar == 'z' || messageChar == 'Z') {
messageArray[y] = 26;
}
else if (messageChar == ' ') {
messageArray[y] = 27;
}
y++;
}
}
for(int i = 0; i < messageArray.length; i++){
System.out.println(messageArray[i]);
}
}
}
You have many errors. Too many to point out, it's simpler if I show the correct code with comments:
public static void main(String[] args) throws Exception {
int[] wordArray;
String messageWord;
char messageChar;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your message: ");
messageWord = scanner.nextLine(); // read once before entering loop
while (!messageWord.equals("exit")) { // notice the correct condition
// these two variables should be reset for each word
int y = 0;
// and each messageArray should be of the right size
int[] messageArray = new int[messageWord.length()];
for (int i = 0; i < messageWord.length(); i++) {
messageChar = messageWord.charAt(i);
// *** insert all those if conditions here ***
y++;
}
// this goes inside the main loop
for (int j = 0; j < messageArray.length; j++)
System.out.print(messageArray[j] + " ");
// we have to ask for the next word inside the main loop
System.out.print("\nEnter your message: ");
messageWord = scanner.nextLine();
}
}
Notice that to exit the loop, it's better to have a special string that can be used as a condition - I'm using the word "exit" for this.
The (or at least 'an') error is in this line:
messageWord = scanner.next(message);
You're specifying message as argument. This seems to be wrong as next(String pattern) requires a regular expression that must match the next input sequence. Just call the no-arg version of this method!
For one thing, the while loop ends even before the for-loop that loops through the messageArray.
Right now the problem as I see it isn't that your code doesn't give results, it is that it doesn't ever end. It infinitely runs.
You are going to want to rewrite your entire code for this. I don't think that there are any quick fixes to make this work how you want.
Might I suggest looking into the StringBuilder class to help you this this project?
It is a way to use Strings as indexed Arrays instead of as one object. It could make what you are trying to do much easier.
The reason because you don't see anything is because you retrieved the input outside the outer while() loop, and completely forget to do anything useful with that.
Try to comment out this line
String message = scanner.nextLine();
and your program will run (but there still is an ArrayIndexOutOfBoundsException, so you need to fix that one too).

Categories

Resources