I need to use the method
public static String readString() {
return input.nextLine();
}
In order to use the Scanner method next.Line(), its just a requirement of the project, the thing is that when I use the static method ProjectUtils.readString() to gather the user input as a string it just throws me a lot of errors, I cant understand why, Im using it this way
public static void encryptAString() {
ProjectUtils.println("Enter the phrase and the key");
String p = ProjectUtils.readString();
ProjectUtils.println("Enter key");
int k = ProjectUtils.readInteger();
ProjectUtils.println(EncryptString.encryptString(p,k ));
But always ends up terminating and throwing this
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ProjectUtils.readInteger(ProjectUtils.java:24)
at ProjectUtils.encryptAString(ProjectUtils.java:88)
at StringMenuManager.run(StringMenuManager.java:37)
at ProjectUtils.operationsOnStrings(ProjectUtils.java:69)
at MainMenuManager.run(MainMenuManager.java:47)
at P1Main.main(P1Main.java:9)
Thanks in advance.
MCVE
The main class
public class Main {
public static void main(String[] args) {
MCVE.encryptAString();
}
}
The MCVE method or ProjectUtils class in the original project
import java.util.Scanner;
public class MCVE {
private static final Scanner input = new Scanner(System.in);
public static void println(String s) {
System.out.println(s);
}
public static int readInteger() {
// for the moment, just assume that the input is a
// valid integer.... but eventually we want to be
// more robust and explicitly read and test first if
// the input was really an integer or not....
return input.nextInt();
}
public static String readString() {
// for the moment, just assume that the input is a
// valid String.... but eventually we want to be
// more robust and explicitly read and test first if
// the input was really an integer or not....
return input.nextLine();
}
public static void encryptAString() {
MCVE.println("Enter the phrase and the key");
String p = MCVE.readString();
int k = MCVE.readInteger();
MCVE.println(EncryptString.encryptString(p,k ));
}
}
And the encryption class
public class EncryptString {
private static final int ALENGTH = 26;
/**
* Encrypt a string using a given key value.
* @param s the String to be encrypted
* @param key the key
* @return the encrypted string
*/
public static String encryptString(String s, int key) {
// if the key value is not in the accepted range (-25..25)
// the encrypted string is the same as the input string
if (key < -25 || key > 25)
return s;
// the key is valid, construct the encrypted string as
// described in P1 specs...
String newString = "";
for(int x = 0; x<s.length(); x++)
{
if(((int)s.charAt(x)>64&&(int)s.charAt(x)<123))
newString = newString + encryptChar(s.charAt(x), key);
else
newString = newString + s.charAt(x);
}
return newString;
}
/**
* Encrypt a particular character.
* @param ch the character to encrypt - assumed to be a letter ‘a’..’z’ or ‘A’..’Z’
* @param key the key to be used. It is assumed to be a value in range (-25..25)
* @return the new character after encryption
*/
private static char encryptChar(char ch, int key) {
// PRE: ch is a letter 'A'..'Z' or 'a'..'z'
// PRE: key is an integer in the range -25..25
int base;
if (Character.isUpperCase(ch))
base = (int) 'A';
else
base = (int) 'a';
return (char) (Math.abs((((int) ch - base) + key + ALENGTH)
% ALENGTH) + base);
}
}