Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

java - I want to convert series of characters into a string and compare with another string

Sample Input 1:

Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 1:

RAINBOW

Sample Input 2:

Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 2:

The spelling is wrong

Code:

public class spellcheck {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        String str1;
        String str2 = "rainbow";
        int i = 0;
        String a[] = new String[7];
        System.out.print("Enter the first letter:");
        a[0] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[1] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[2] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[3] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[4] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[5] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[6] = scan.nextLine();
    }
}

What should be the next step?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
String tmp = "";

for(int j = 0; j < a.length; j ++)
tmp += a[j];

System.out.println(tmp);

Output -

Enter the first letter:r
Enter the first letter:a
Enter the first letter:i
Enter the first letter:n
Enter the first letter:b
Enter the first letter:o
Enter the first letter:w
rainbow

To compare you can utilize the built in String function .equals -> tmp.equals("Otherstring")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...