如何将字符串存储到扫描仪对象中 [英] how to store string into a scanner object
本文介绍了如何将字符串存储到扫描仪对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已编码
I have coded
Scanner sc=new Scanner(System.in);
String s="";
while(sc.hasNext())
{
s=sc.next()+s;
sc=sc.nextLine();
}
但是第6行给出错误,因为sc不能有字符串类型
请帮帮我搞定?
But the line no 6 gives error because sc can't have string type
Please help me out to do it?
推荐答案
因为sc.nextLine()返回一个String但sc是一个Scanner对象。试试这个:
Because sc.nextLine() returns a String but sc is a Scanner object. try this:
Scanner sc=new Scanner(System.in);
String s="";
while(sc.hasNextLine())
{
s=sc.nextLine() + s;
}
System.out.println(s);
这篇关于如何将字符串存储到扫描仪对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文