Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have coded
C#
Scanner sc=new Scanner(System.in);
String s="";
while(sc.hasNext())
{
s=sc.next()+s;
sc=sc.nextLine();
}

But the line no 6 gives error because sc can't have string type
Please help me out to do it?
Posted
Updated 11-Apr-14 0:42am
v2
Comments
Richard MacCutchan 11-Apr-14 8:51am    
What exactly are you trying to do?
V$ethi 11-Apr-14 17:09pm    
i can explain by an example :
Input : vandana is good girl
Output: girl good is vandana.
and i'll print s after the loop to get the output

1 solution

Because sc.nextLine() returns a String but sc is a Scanner object. try this:
C#
Scanner sc=new Scanner(System.in);
String s="";
while(sc.hasNextLine())
{
    s=sc.nextLine() + s;
}
System.out.println(s);
 
Share this answer
 
v2
Comments
V$ethi 11-Apr-14 17:06pm    
But don't you think so that it'll be an infinite loop.Beause the value of sc will never change while looping
Peter Leow 11-Apr-14 20:37pm    
Definitely, you have to set your own exit condition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900