简体   繁体   English

如何从扫描仪读取字符串,并将每个字母转换为不同的值?

[英]How can I read a string from scanner, and convert each letter to a different value?

I am working on taking a string that has been imported as a file, taking each character, and "encrypting" it by changing it to the next letter. 我正在研究一个已作为文件导入的字符串,采用每个字符,并通过将其更改为下一个字母来对其进行“加密”。 Basically, a is b, b is c, c is d, and etc. It isn't secure but it isn't meant to be for these purposes. 基本上,a是b,b是c,c是d,依此类推。它不是安全的,但并非出于这些目的。 The scanned string can be anything but I'm using "Orange juice is great! I drank 83,214 cups of it yesterday." 扫描的字符串可以是任何东西,但我使用的是“橙汁很棒!昨天我喝了83214杯。” It is in a file called input.txt. 它位于名为input.txt的文件中。

My code is as follows: 我的代码如下:

import java.util.Scanner;
import java.io.*;

public class ReadFileExample{

   public static void main(String[] args){

      //String fileName = "input.txt";
      //File file = new File(fileName);
      //Scanner in = new Scanner(file);
      try{
         Scanner in = new Scanner(new File("input.txt"));
         while(in.hasNext() == true){
            String input = in.next();
            System.out.println(input);

            }
         }
         in.close();
      }
      catch(FileNotFoundException exception){
         System.out.println("Could not find the file.");
      }

   }

}

I thought about converting the string to an array but I'm not sure how to go about that. 我曾考虑过将字符串转换为数组,但不确定如何去做。 Maybe like this? 也许是这样吗?

char[] charArray = input.toCharArray();
            for (char c : charArray){
               System.out.println(c);
            }

I'm really not sure where to go with this. 我真的不知道该去哪里。 I also wondered about for(i = 0; i < string.length; i++) Not sure how to go about that either. 我也想知道for(i = 0; i <string.length; i ++)也不知道该怎么做。 Once it is done, I need to print it out. 完成后,我需要将其打印出来。 Then, I need to take that same string, encrypt it again. 然后,我需要使用相同的字符串,然后再次对其进行加密。

The second file encryption technique is to replace each letter with the place it is in the alphabet. 第二种文件加密技术是将每个字母替换为字母中的位置。 We will use two digits for every letter. 每个字母我们将使用两位数字。 For example, the letter 'a' is 01, the letter 'b' is 02, the letter 'c' is 03, the letter 'z' is 26. So that we can use capital letters we start 'A' with 27, 'B' with 28, 'C' with 29, and so on. 例如,字母“ a”为01,字母“ b”为02,字母“ c”为03,字母“ z”为26。因此我们可以使用大写字母,以27开头的“ A”, 'B'代表28,'C'代表29,依此类推。

For numbers, we convert each digit into two letters: the first two letters that they stand for. 对于数字,我们将每个数字转换为两个字母:它们代表的前两个字母。 The first letter is capitalized followed by a lower-case letter. 首字母大写,后跟小写字母。 For example, 8 written out is eight. 例如,写出的8是8。 So, 8 would be encrypted to “Ei”, 1 would be encrypted to “On”, 2 would be encrypted to “Tw”, 3 would be “Th” and so on. 因此,将8加密为“ Ei”,将1加密为“ On”,将2加密为“ Tw”,将3加密为“ Th”,依此类推。

Note that “10” is actually two digits, so it would be encrypted as “1” and “0”: “OnZe” 请注意,“ 10”实际上是两位数字,因此将被加密为“ 1”和“ 0”:“ OnZe”

If you come to a non-letter character (spaces, punctuation, numbers, etc.), just print them as is without encrypting. 如果您遇到非字母字符(空格,标点符号,数字等),只需照原样打印而不进行加密。

Any help is appreciated even if it is suggesting what to search for to answer my questions. 任何帮助都将不胜感激,即使它正在建议寻找什么来回答我的问题。

Here is the below code, the code is explained in the comments, if you need more help then comment below, the demo can be found at the below link! 这是下面的代码,代码在注释中进行了说明,如果您需要更多帮助,请在下面的注释中进行演示,可以在下面的链接中找到演示! I have given the demo for the static string that you have given the example for. 我已经给出了示例的静态字符串的演示。

Demo 演示版

import java.util.HashMap;

/**
 *
 * @author Sai-Karan
 */
public class App {

    //Function to return the substring for the second encoding method
    //Example 1 will return On, and 2 will return Tw ....etc
    public static String findstringmap(char c)
    {
        //Create a hashmap and add the key elements and the string definitions
      HashMap <Integer, String> hmap = new HashMap <Integer, String>();

       /*Adding elements to HashMap*/
      hmap.put(1, "One");
      hmap.put(2, "Two");
      hmap.put(3, "Three");
      hmap.put(4, "Four");
      hmap.put(5, "Five");
      hmap.put(6, "Six");
      hmap.put(7, "Seven");
      hmap.put(8, "Eight");
      hmap.put(9, "Nine");
      hmap.put(0, "Zero");

      /* Get values based on key*/
      int num_value = Character.getNumericValue(c);
      //convert to the numeric value
      String var = hmap.get(num_value);
      //get the substring
      String ret_str = var.substring(0, 2);
      return ret_str;
    }
    public static void main(String[] args) {
        // TODO code application logic here

        String l1 = "Orange juice is great! I drank 83,214 cups of it yesterday.";
        //String final contains the result
         String Final = "";
         String next = "";
        for(int i =0 ; i< l1.length(); i++)
        {
           //char c = l1.charAt(i);

            if (l1.charAt(i) == ' ')
            {
             next = " ";
            }
            else if (l1.charAt(i) == '!')
            {
             next = "!";
            }
            else if(l1.charAt(i) == ',')
            {
                next = ",";
            }
            else if(l1.charAt(i) == '.')
            {
                next = ".";
            }
            else if(!(Character.isDigit(l1.charAt(i))))
           {
           int val = l1.charAt(i);
           next = String.valueOf( (char) (val + 1));

           // System.out.println(next);
           }
           else if(Character.isDigit(l1.charAt(i)))
           {
               next = findstringmap(l1.charAt(i));

           }
           Final = Final + next;

        }
        System.out.println(Final);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将每个整数/双精度值从包含嵌套对象和 arrays 的 JSON 数据转换为字符串? - How can I convert each integer/double value to String from JSON data containing nested objects and arrays? 如何从 Java 中的扫描仪读取不同的输入? - How do I read different inputs from scanner in Java? 如何在数组的开关大小写上使用Scanner,以便大小写从字符串b中获取值 - How can I use Scanner on switch case for String of array so that case takes in value from String b 如何从扫描仪库中读取任何用户输入? - How can I read any user input from the scanner library? 如何使用扫描仪从命令行读取文件 - How can I read a file from the command line using scanner 我想从文本字段中读取String句子,并将String中的每个字母与我的列表进行比较 - I want to read a String sentence from a Textfield and compare each letter in the String to my list 如何读取文件中的每个字母? - How can I read every letter from the file? 如何仅使用扫描程序读取文件并将每个句子存储在arrayList中? - How can I read a file and store each sentence in an arrayList using Scanner only? 如何将每个句子的第一个字母转换为大写,将所有其他字母转换为小写? - How can I convert the first letter of each sentence to uppercase and all other letters to lowercase? 如何使用扫描仪类从txt文件读取每个元素? - How to read each element from a txt file using scanner class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM