Run a Java program : NoSuchElementException


(To Céline) #1

Hi everyone,

System.out.println("Enter a word \n");
   str=sc.nextLine();

Why Gradle doesn’t let me time to enter a string and tell me “no line found” at java.util.Scanner.nextLine?

Thank you!


(Benjamin Muschko) #2

Try this task implementation:

task scanInput << {
    println "Enter a word \n"
    def result = new Scanner(System.in).next()
    println "Entered word: $result"
}

(To Céline) #3

Thank you for your answer! So I have to put this new task in the build.gradle right? Sorry I’m not sure that I undetstand well as I started learning gradle last month


(Benjamin Muschko) #4

Correct. You can execute it with “gradle scanInput”.


(To Céline) #5

Thank you very much, it works well.

I understand creating a new task allows me to do a scanInput, but how can I put it into my java program?

It asks the user to enter several words, and the program prints a sorted list of words. Should I rewrite my program in tasks in build.gradle?


(Benjamin Muschko) #6

If this logic should be part of your application, then no. Gradle can help you build and run this application though. Have a look at the application plugin in the Gradle online documentation.


(To Céline) #7

I was at the beginning asked to build my java project in Gradle to have a jar with my class in it. I use the gradle build command in the shell and I obtain two files : “build” and “.gradle”.

Is trying to run it by gradle run command necessary?

I thought I can check it was sucessfully build by testing if the program runs, is it right?

Sorry to ask these questions, I am quite lost, and thank you for your asnwers.

Use gradle to build this project. Gradle is a commonly used build tool in java world. You can google it. Using gradle build a jar with your class in it.


(Benjamin Muschko) #8

I’d recommend you read up on the Java plugin in the online documentation. It gives an answer to pretty much all of your questions.


(To Céline) #9

Okay thank you, I read up the java plugin, but I still have comprehension problem because of a lack of English vocabulary and I’m am not really good at programming, I entered Computer Science Department last september.

I understand that Gradle provides one instance of test and one of compile, but I don’t understand if there are commands to execute it :confused:


(Peter Niederwieser) #10

You can do ‘gradle test’ or ‘gradle compileJava’ or ‘gradle jar’. Have a look at the many samples in the full Gradle distribution.