Getting "cannot find symbol" error with reader.nextLine()

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/

Solution:

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));
}

JAVA ERROR : package com.sun.rowset is not visible : com.sun.rowset is declared in module java.sql.rowset, which does not export it

I’m simply try to run this code:

import com.sun.rowset.CachedRowSetImpl;

public class Test {
    public static void main(String[] args) throws Exception{
        CachedRowSetImpl crs = new CachedRowSetImpl();
    }
}

When I run it I get:

Error:(1, 15) java: package com.sun.rowset is not visible (package
com.sun.rowset is declared in module java.sql.rowset, which does not
export it)

I’m using IntelliJ and I tried to import rs2xml.jar, and that still doesnt help.

Solution:

With Java 9 you can not access this class anymore. And in the ideal way you shouldn’t do that. That is because this class’s package is not exported in the module javax.sql.rowset. The proper way to do that in Java-9 will be:

import javax.sql.rowset.*; 

public class Test {
    public static void main(String[] args) throws Exception {

        CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
    }
}

To understand that we can go to the module description (module-info.java) and find a list of exported packages:

exports javax.sql.rowset;
exports javax.sql.rowset.serial;
exports javax.sql.rowset.spi;

Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?

I put an extra semicolon after the semicolon of System.out.println:

System.out.println();;

Which was legal to Java compiler, so I checked for other statements and they were all legal too. So when I searched and found these links:

  1. Why does Java not show an error for double semicolon at the end of a statement?
  2. Compiler doesn’t complain when I ended a line with two semicolons. Why?
  3. When would you put a semicolon after a method closing brace?
  4. Why does code with successive semi-colons compile?
  5. Semicolon at end of ‘if’ statement

I came to understand that an extra semicolon means an extra empty statement.

But when I put an extra semicolon after a return statement, I got a compile time error. I came to the conclusion that the return statement is considered to be the last statement in the flow of execution, so putting an extra statement after the return is illegal.

The same thing happens in this code too:

if(a == b)
    System.out.println();;
else
    System.out.println();

Inside the if statement System.out.println();; gives a compile time error, because the compiler is expecting elseif or else. Am I right or is is there some other reason?

Solution:

Why multiple semicolon is not allowed after the return statement, when
it is allowed for all other statement?

Simply because when you have a statement like

System.out.println();;

This means you have two statements, one is System.out.println(); and other statement is after the first semi colon, it is empty and that’s allowed BUT you can’t have any empty statement or any other statement after the return statement because it will never execute, in other words, its unreachable statement and you can’t have unreachable statements in your code.

Same thing happens in this code too

if(a == b)
    System.out.println();;
else
    System.out.println();

that’s because, when you have an else statement, statement just before it should be if statement which is not the case in above code snippet because statement just before else statement is an empty statement which is not allowed.

If you have parenthesis after the if statement like

if(a == b) {
    System.out.println();;
}
else
   System.out.println();

you will get no errors because now empty statement is inside an if block and the statement just before else is if statement and not the empty statement which was the case when you had no parenthesis after if statement