简体   繁体   English

无法用Java合并两个句子

[英]Not able to concat two sentences in Java

I wanted to concat two two strings. 我想连接两个两个字符串。

This is my code: 这是我的代码:

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

public class Solution { 

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";       
        Scanner scan = new Scanner(System.in);    
        int a; double b;  String c;
        a = scan.nextInt();
        b = scan.nextDouble();
        c = scan.next();
        System.out.println(i + a);
        System.out.println(d + b);
        String res = s.concat(c);
        System.out.println(res);       
        scan.close();
    }
} 

Input: 输入:

12
4.0
is the best place to learn and practice coding!

This output: 此输出:

16
8.0
HackerRank is

Output 输出量

I tried everything I can think of. 我尝试了所有我能想到的。

Edit: 编辑:

If you change to nextLine() , the previous inputs don't consume the newline character, so you'll have to add in an extra scan.nextLine(); 如果更改为nextLine() ,则先前的输入不会占用换行符,因此您必须添加额外的scan.nextLine(); .

See demo online 在线观看演示


First, declare c (ie String c ), and also a and b (or maybe you did it but forgot to put it in your post?) 首先,声明c (即String c ),以及ab (或者也许您做到了,但是忘记了将其放在您的帖子中?)

But then, your problem is using Scanner::next instead of Scanner::nextLine . 但是然后,您的问题是使用Scanner::next而不是Scanner::nextLine

.next() from the docs : .next()来自文档

Finds and returns the next complete token from this scanner. 查找并返回此扫描仪的下一个完整令牌。

.next() takes the first token, using whitespace as the delimiter, so that's why your output is HackerRank is , assuming your c is holding something like is a great site . .next()使用第一个令牌,使用空格作为分隔符,因此这就是为什么您的输出为HackerRank is的原因,假设您的c拥有is a great site

Solution

Change: 更改:

c = scan.next(); to c = scan.nextLine(); c = scan.nextLine();

Here you go.. 干得好..

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        double b = scan.nextDouble();
        scan.nextLine();
        String c = scan.nextLine();
        System.out.println(i + a);
        System.out.println(d + b);
        String res = s + c;
        System.out.println(res);
        scan.close();


    }
}

and see below result: 并查看以下结果: 成功接受的结果:

That's because the Scanner.nextInt or nextDouble method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner.nextLine 这是因为Scanner.nextInt或nextDouble方法不会占用您输入的最后一个换行符,因此在下次调用Scanner.nextLine时会占用换行符。

import java.util.*;
import java.lang.*;
import java.io.*;

class ScannerDemo
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        double b = scan.nextDouble();
        scan.nextLine();
        String c = scan.nextLine();
        System.out.println(i + a);
        System.out.println(d + b);
        String res = s.concat(c);
        System.out.println(res);
        scan.close();
    }
}

Your Input needs to be 您的输入必须是

12
4.0
is the best place to learn and practice coding!

Output will be: 输出将是:

16 8.0 HackerRank is the best place to learn and practice coding!

Scan an extra line before scanning the c. 在扫描c之前,扫描额外的一行。 Like 喜欢

scan.nextLine();
c = scan.nextLine();

Because before entering c you press enter, there takes a line input. 因为在输入c之前,请按Enter,所以需要进行线路输入。

int a = scan.nextInt();
double b = scan.nextDouble();
String c = scan.next();

you have to define this variable like I do 你必须像我一样定义这个变量

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

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