英文:
Why does setting a value to a specific subscript of an array not print it out?
问题
在下面的代码中,在底部,由于某种原因,尽管我赋予了```encodingCharacter = encodeArray[j];```的值。之后我尝试打印出该特定下标的值。但由于某种原因,那个语句引起了问题。在运行程序后,我看到它打印了两行空白,我不知道为什么我不能将字符值```'z'```赋予```encodingCharacter```。
import java.util.Scanner;
public class UserInputDemo1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
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 separated 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);
}
}
英文:
In the code below, at the bottom, for some reason although I assign the value of 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.
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);
}
}
答案1
得分: 0
在第 15 行发生了 java.util.InputMismatchException
异常。
您的 input.nextBoolean()
会返回输入中的下一个标记是否为布尔值,而不是是否有另一个字符。
我相信您在寻找 input.hasNext()
,或者类似的内容。
<!-- 这是我的第一个答案!- 2021/4/30 -->
英文:
At line 15 is where the java.util.InputMismatchException occurs.
Your input.nextBoolean()
returns whether the next token of the input is a boolean, rather than whether there is another character.
I believe you are looking for input.hasNext()
, or something of the sort.
<!-- This is my first answer! - 2021/4/30 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论