Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取字符串中的字符并根据输入返回?_Java_String_Java.util.scanner - Fatal编程技术网

Java 如何读取字符串中的字符并根据输入返回?

Java 如何读取字符串中的字符并根据输入返回?,java,string,java.util.scanner,Java,String,Java.util.scanner,这就是我所拥有的:我想制作一个程序,要求用户输入:你的名字 这就是我需要帮助的地方:然后,我想让程序读取名字中的每个字符、字母。最后,我希望它返回一个对应于用户名中每个字符的形容词列表 这就是我到目前为止所做的: public static void main (String [] args) { System.out.println("This program will give meanings to every " + "lette

这就是我所拥有的:我想制作一个程序,要求用户输入:你的名字

这就是我需要帮助的地方:然后,我想让程序读取名字中的每个字符、字母。最后,我希望它返回一个对应于用户名中每个字符的形容词列表

这就是我到目前为止所做的:

public static void main (String [] args) {

    System.out.println("This program will give meanings to every "
                        + "letter in your name.\n");

    Scanner input = new Scanner (System.in);
    System.out.println("Please enter your name:");
    String name = input.next();

    String A = "adventurous";
    String B = "bold";
    String C = "caring";
    String D = "devoted";
    String E = "encouraging";
    String F = "funny";
    String G = "gentle";
    String H = "honest";
    String I = "intelligent";
    String J = "joyful";
    String K = "kind";
    String L = "loving";
    String M = "mature";
    String N = "neat";
    String O = "organized";
    String P = "persistent";
    String Q = "quick";
    String R = "religious";
    String S = "sensitive";
    String T = "thankful";
    String U = "useful";
    String V = "virtuous";
    String W = "witty";
    String X = "this letter isn't in your name";
    String Y = "young";
    String Z = "zany";  
}

将您的形容词放在映射中(以char作为键,形容词作为值),而不是使用这些字符串,然后将名称转换为CharArray,对其进行迭代,对于每个字母,只需从映射中获取项目即可。你可以把你的形容词列成一个列表,然后打印出来

//create the map and the list of adjectives
Map<Character, String> adjectives = new HashMap<>();
List<String> personAdjectives = new ArrayList<>();
//fill the map (although it would be better retrieving data from a database)
adjectives.put('a',"adventurous");
adjectives.put('b',"bold");
// ...
//convert the name to a char array
char[] chars = name.toCharArray();
//iterate over it
for(char c : chars){
  //access the map and fill the list
  personAdjectives.add(adjectives.get(c));
}
//print the list
//创建地图和形容词列表
映射形容词=新HashMap();
List personAdjectives=new ArrayList();
//填充地图(尽管从数据库检索数据会更好)
形容词.put('a',“冒险”);
形容词.put('b','bold');
// ...
//将名称转换为字符数组
char[]chars=name.toCharArray();
//迭代
for(char c:chars){
//访问地图并填写列表
添加(形容词)get(c));
}
//打印列表
#我希望这能有所帮助#

我没有足够的时间,所以总结一下这段代码,你可以自己工作
import java.io.*;
公共类StringRead
{
公共静态void main(字符串[]arg){
String hi=“嗨,所有的男人和性感。”;
对于(int x=0;x
您需要更多的标签。这是什么语言?我想这是JAVA,但其他人都知道吗?你看到了什么输出/错误?@WillSheppard我只得到了两条打印语句:“这个程序将赋予你名字中的每个字母含义。”“请输入你的名字。”当我输入我的名字并按enter键时,由于代码不完整,什么也没有发生。我得到了一个错误:“映射…”-上面说我必须插入“维度”。我搜索并将其更改为:“HashMap…”-你的建议行吗?是的,但你必须颠倒顺序。我已经用正确的顺序更新了我的答案。太棒了,谢谢!显然,我也必须创建一个Char类。不,对不起,我很匆忙地编写了代码,并且犯了一些错误。事实上我指的是char而不是char。。。您也可以使用字符,Java将自动处理转换。不管怎样,我很高兴能帮助你。如果您的程序运行正常,请花点时间接受此答案。您可以通过单击答案分数下的绿色勾号来完成此操作。
java.lang.Character
是原语
char
的对象版本,没有
char
。在
映射
中不能将基元
字符
作为键。欢迎使用堆栈溢出。请花时间写一个合适的答案,以供参考。这个答案没有解释(另请参见),而给定的代码并没有真正为OP的问题提供答案,因为它要求OP猜测他需要做什么或用给定的代码修复什么。
import java.io.*;

public class StringRead
{
    
    public static void main(String[] arg){
        
        String hi = "hi all man and sexy.";
        
        for(int x = 0; x < hi.length(); x++){
            
            System.out.println(hi.charAt(x));
            
        }
        
        
    
    
    }
}