简体   繁体   中英

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];. 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);
    }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM