Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
<pre>package com.company;

import java.sql.SQLOutput;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        char ch,yn;
        String word = null,rev=" ";
        int i,x;
        Scanner sc =new Scanner(System.in);
        do {
            System.out.println("enter word");
            word=sc.next();
            x=word.length();
            for(i=0;i<x;i++)
            {
                ch=word.charAt(i);
                rev=ch+rev;
            }
            
            
            
            System.out.println("do you wish to continue(y/n");
            yn =sc.next();
        }while (yn=='Y'||yn=='y');
    }
}





What I have tried:

i am getting error in sedcond last line yn=sc.next();  I don't know why.
Posted
Updated 8-Oct-21 9:25am
Comments
Patrice T 8-Oct-21 15:35pm    
And plan to give us the error message ?

1 solution

Your call to sc.next() returns a string but you are trying store it into char.

You need to change your declaration of yn to String and modify you while loop statement to reflect the fact it is testing a string.

See below

Java
public static void main(String[] args) {
        char ch;
        String yn;
        String word = null;
        String rev = " ";
        int i,x;
        Scanner sc =new Scanner(System.in);
        do {
            System.out.println("enter word");
            word=sc.next();
            x=word.length();
            for(i=0;i<x;i++)
            {
                ch=word.charAt(i);
                rev=ch+rev;
            }         
           
            
            System.out.println("do you wish to continue(y/n");
            yn = sc.next();
        }while (yn.compareTo("y") == 0 ||yn.compareTo("Y") == 0);
    }
 
Share this answer
 
v2

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