22

I tried every online solution and i still get the same result..i'm trying to get my code to run on a file "distinct.txt" and here is my attempt to run it.

here is my try

i tried different paths and all give same result.

here is my code

package pset2.sol;
import edu.princeton.cs.algs4.StdIn;
public class Permutation {
public static void main(String[] args){
    RandomizedQueue<String> rq = new RandomizedQueue<>();
    while (!StdIn.isEmpty()) {
        String item = StdIn.readString();
        rq.enqueue(item);
    }
    int k = Integer.parseInt(args[0]);
    for(int i = 0; i < k; i++){
        System.out.println(rq.dequeue());
    }
}
}
isapir
  • 21,295
  • 13
  • 115
  • 116
Ahmad Fahmy
  • 327
  • 1
  • 2
  • 11
  • 2
    Add code as text. not as image – Jens Feb 03 '17 at 15:01
  • You have to compile the java file first, And you have to run it from the folder that contains pet2 folder – Jens Feb 03 '17 at 15:03
  • 1
    @Jens i'm not adding the pic for the code i'm adding it to show what i wrote in the terminal but here is my code anyway http://pastebin.com/QznjZ44d.. can u tell me how to compile it first ? and i'm already running it through the folder which contains pset2 which is "production"..i'm beginner so i might miss understand some stuff – Ahmad Fahmy Feb 03 '17 at 15:24
  • to campile code you have to use `javac` – Jens Feb 03 '17 at 15:26
  • it gives me this error javac: file not found: Permutation.java – Ahmad Fahmy Feb 03 '17 at 15:31
  • 1
    Do you see an out folder above the src folder? – Ishnark Feb 03 '17 at 15:58
  • yeah i fixed that error when i changed directory to src/pset2/sol.. but when it compiles it gives me error: cannot find symbol RandomizedQueue rq = new RandomizedQueue<>(); this error deosn't appear when i run it through the green button. "RandomizedQueue" is class in the same directory..any idea whats wrong? – Ahmad Fahmy Feb 03 '17 at 16:02
  • As far as I can tel from the directory, all your files have a green 'play' logo. This means that all of them have a main method. You should only have have 1 main method and instantiate the objects there. Also since this is a code question now, can you also put your code up as well? Thanks – Ishnark Feb 03 '17 at 16:16
  • i added the code and commented the main methods in other files so only Permutation have main method..still same error but i don't know why i can run it normally from the green play button? – Ahmad Fahmy Feb 03 '17 at 16:30
  • Okay could you show me the RandomizedQueue class? – Ishnark Feb 03 '17 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134799/discussion-between-ishnark-and-ahmad-fahmy). – Ishnark Feb 03 '17 at 16:59

5 Answers5

27

My guess is that your source-folder is not set correctly.

Try File -> Project Structure -> Modules -> (tab) Sources -> Mark as: Sources.

That fixed the problem for me after importing an Eclipse Project.

matejetz
  • 520
  • 3
  • 7
18

You have to make sure that the "Project compiler output" value is set, in Project Structure > Project, and that the module's Compiler output points to a valid path:

enter image description here enter image description here

isapir
  • 21,295
  • 13
  • 115
  • 116
1

Ideally, you should always use the green button from IntellIJ. You can set the Edit Configurations to give the path to the file. I don't know about reading in the file from stdin through there, though.


Your problem is not IntelliJ, though, it's the CMD.

First, make sure you are in the right folder.

Run cd /path/to/compiled_files to see that you are at the parent of the pset folder.

pset
   /sol2
      Permutation.class

Then, you'll likely need a classpath to have the other classes within the pset.sol2 package to be resolved.

java -cp . pset.sol2.Permutation

More details: What does "Could not find or load main class" mean?

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

In intellij, choose Run. | Edit Configurations from the main menu.

enter image description here

Davoud
  • 2,576
  • 1
  • 32
  • 53
-1

A bit late to the party here, but I found that my Main file (in the source file), was missing the ".java" extension, even though the file looked fine in the intelliJ project explorer.

However on close inspection, I noticed that the file type in /out/ was not a class file (i.e. did not have the blue circle with a "C" in).

I think this could be because I created the "main" file in my source folder, before I marked that folder as source in the project settings, and the file type created was undefined somehow...

To fix, I renamed the Main file to Main.java (in finder), and rebuilt the project from the build menu.

Jinglesting
  • 509
  • 5
  • 16