[英]Why does setting a value to a specific subscript of an array not print it out?
In the code below, at the bottom, for some reason although I assign the value of encodingCharacter = encodeArray[j];.在下面的代码中,在底部,出于某种原因,虽然我分配了 encodingCharacter = encodeArray[j]; 的值。 After that I try to print that specific subscript value out.
之后,我尝试打印出该特定下标值。 But for some reason that statement is causing issues.
但出于某种原因,该声明引起了问题。 After I run the program I see that it ran 2 empty lines, I don't know why I can't assign the char value 'z' to encodingCharacter.
在我运行程序后,我看到它运行了 2 个空行,我不知道为什么我不能将字符值 'z' 分配给 encodingCharacter。
import java.util.Scanner;
public class UserInputDemo1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//creating scanner named input.
System.out.println ("Would you like to encode or decode a message.");
System.out.println ("If you would like to encode a message enter true. If you would like to decode a message enter false");
System.out.println("Note that blank spaces are seperated by a space and the message terminates with a period.");
boolean encodeOrDecode = input.nextBoolean();
if (encodeOrDecode)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the message you would like to be encoded.");
String encodeMessage = scan.nextLine();
char[] encodeCharArray = encodeMessage.toCharArray();
encodeMessage(encodeCharArray);
}
if (!encodeOrDecode)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the message you would like to be decoded.");
String decodeMessage = scan.nextLine();
char[] decodeCharArray = decodeMessage.toCharArray();
}
}
public static void encodeMessage (char encoding [])
{
char encodeArray [] = new char [encoding.length];
for (int j = 0; j < encoding.length; j++ )
{
char encodingCharacter = encoding[j];
if (encodingCharacter == 'a')
{
encodingCharacter = 'z';
System.out.println(encodingCharacter);
encodingCharacter = encodeArray[j];
System.out.println(encodeArray[j]);
}
if (encodingCharacter == 'b')
{
encodingCharacter = 'y';
System.out.println(encodingCharacter);
encodingCharacter = encodeArray[j];
}
}
String encodedArray = new String(encodeArray);
System.out.println (encodeArray);
}
}
At line 15 is where the java.util.InputMismatchException occurs.第15行是 java.util.InputMismatchException 发生的地方。
Your input.nextBoolean()
returns whether the next token of the input is a boolean, rather than whether there is another character.您的
input.nextBoolean()
返回输入的下一个标记是否是布尔值,而不是是否有另一个字符。
I believe you are looking for input.hasNext()
, or something of the sort .我相信您正在寻找
input.hasNext()
或 类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.