Create a program that gets a number from a user. This number should be from 1 to
ID: 3799045 • Letter: C
Question
Create a program that gets a number from a user. This number should be from 1 to 26. Then have the user input a phrase, with this phrase you want to add the number they entered and print out the new phrase.
Example 1:
User Input: 5
User Input Expected output
A F
B G
C H
Example 2:
User Input: 3
User Input Expected output
A D
B E
C F
Example 3:
User Input: 2
User Input: hello
Expected output: jgnnq
Explanation / Answer
Please use below example. This will work.
package test;
import java.util.Scanner;
public class InputPhrase
{
public static void main(String[] args)
{
int number;
Scanner scanner = new Scanner(System.in);
System.out.println("User Input:");
number = scanner.nextInt();
if (number < 1 && number > 26)
{
System.out.println("Please enter valid input between 1 and 26.");
}
else
{
convertChars(number);
}
}
public static void convertChars(int number)
{
String word;
Scanner scan = new Scanner(System.in);
String newWord;
System.out.println("User input string :");
while(scan.hasNext()){
word = scan.next();
char[] ascii1 = word.toCharArray();
for(char ch:ascii1){
int asciiValue = (int)ch;
int newValue = asciiValue+number;
// System.out.println("character is :"+ch+ " ascii value: "+asciiValue+" New value "+newValue+ " ");
newWord = Character.toString((char)newValue);
System.out.println(newWord);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.