我需要使用方法
public static String readString() {
return input.nextLine();
}
为了使用Scanner方法next.Line(),它只是项目的一个要求,那就是当我使用静态方法ProjectUtils.readString()将用户输入收集为字符串时它只是抛出我犯了很多错误,我无法理解为什么,我这样使用它
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 ));
但总是会终止并抛出这个
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)
提前致谢。
MCVE
主要课程
public class Main {
public static void main(String[] args) {
MCVE.encryptAString();
}
}
原始项目中的MCVE方法或ProjectUtils类
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 ));
}
}
加密类
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);
}
}
答案 0 :(得分:1)
虽然我不确定确切的潜在问题,但您的输入中似乎有一个新行不应该存在。它导致第一个typedef void(^blockTakingInt)(int);
// Returns a block
blockTakingInt f()
{
return ^(int i) {
printf("i = %d\n", i);
};
}
// Accepts a block as a parameter
void g(int i, blockTakingInt b)
{
b(i);
}
// Store the block returned by the function f in b, and pass it to g
void (^b)(int) = f();
g(4, b);
读取(大概)空行。
临时解决方法是通过调用input.nextLine()
并忽略返回值来使用此换行符,然后通过另一个readString()
调用读取实际的String输入。