Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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 如何使用Scanner对令牌执行不同的操作_Java_While Loop_Java.util.scanner - Fatal编程技术网

Java 如何使用Scanner对令牌执行不同的操作

Java 如何使用Scanner对令牌执行不同的操作,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,例如: 用户输入以下内容-Fred John Samuel Smith 输出应该是-FJS-SMITH 代码不起作用,因为输出如下: FJSS史密斯 如何将前3个单词从最后一个单词中分割出来?试试我的方法。我在评论中提供了详细信息 String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase(); Scanner l = new Scanner (line);

例如:

用户输入以下内容-Fred John Samuel Smith

输出应该是-FJS-SMITH

代码不起作用,因为输出如下:

FJSS史密斯


如何将前3个单词从最后一个单词中分割出来?

试试我的方法。我在评论中提供了详细信息

    String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();

    Scanner l = new Scanner (line);
    int index = line.lastIndexOf(" ");

    while (l.hasNext())
    {
        String name = l.next();
        char ch = name.charAt(0);
        System.out.print(ch);
    }
    System.out.print(" " + line.substring(index + 1));

这可能是你问题的答案

public static void main(String args[])
            {
                //get the first Three Letter acronym and the last name
                StringBuilder sb = new StringBuilder("");
                String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();
                sb.append(line.charAt(0));
                int spaces=0;
                //get the actual spaces
                for(int i=0;i<line.length();++i)
                {
                    if(line.charAt(i)==' ')
                        ++spaces;
                }

                for(int i=1;i<line.length();++i)
                {
                    //compare each character of inputted String to space
                    if(line.charAt(i)==' '&&spaces>1)
                    {
                        sb.append(line.charAt(i+1));
                        --spaces;
                    }
                    //if space is now equal to one 
                    else if(line.charAt(i)==' '&&spaces==1)
                    {
                        //it will consume all the remaining letter using this loop including the space character
                        while(i<line.length())
                        {
                            sb.append(line.charAt(i));
                            ++i;
                        }
                    }



                }
                //print test of StringBuilder sb
                System.out.println(sb.toString());


            }

注:此目的不需要扫描仪。检查方法。用空格拆分字符串,使用for循环生成的数组,并使用索引检查是否到达最后一个单词。关于您的问题:可能重复的
String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();

    String l[] = line.split(" ");
    int index =0;
while(index<4)
{
if(index<3)
 System.out.print(l[index].charAt(0));
else
  System.out.print(" "+l[3]);

index++;
}