Here is my code (it is code to reverse a given string)
import java.util.Scanner; public class ReversingName { public static String reverse(String text) { // write your code here int strlenght= text.length(); int i=1; String str= ""; while (i<=strlenght){ char test= text.charAt(strlenght-1); str=str+test; } return str; } public static void main(String[] args) { System.out.print("Type in your text: "); String text = reader.nextLine(); System.out.println("In reverse order: " + reverse(text)); } }
But I cannot take in input because when I try to take the string input I get a “cannot find symbol error” even though I have clearly defined the variable “text”.
This question is from MOOC.fi’s Java OOP course, and can be found here (question 52, if it helps): https://materiaalit.github.io/2013-oo-programming/part1/week-3/
Advertisement
Answer
reader
is never declared. From the looks of things, it seems as though it’s supposed to be a Scanner
instance:
public static void main(String[] args) { Scanner reader = new Scanner(System.in); // Declare and initialize reader System.out.print("Type in your text: "); String text = reader.nextLine(); System.out.println("In reverse order: " + reverse(text)); }