0

Background: I'm taking a beginner Java coding class.

Here is a simple problem I'm looking to solve:

"Write a program that prompts the user to enter the minutes, and displays the number of years and days for the minutes."

This is what I have written so far:

import java.util.Scanner;


public class Main {

public static final int MINUTES_PER_YEAR = 525600;
public static final int MINUTES_PER_DAY = 1440;

public static void main(String[] args) {

    // CH2.7 SOLUTION

    // declare variables 
    int minutes;
    int years;
    int days;

    // create scanner object to prompt user for minutes
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of minutes: ");
    minutes = input.nextInt();
    input.close();

    years = minutes / MINUTES_PER_YEAR;
    days = minutes % MINUTES_PER_DAY;

    System.out.println(minutes + " minutes is approximately " + years + " years and " + days + " days.");
    }
}

This is the error message I am receiving in the Codenvy cloud-based IDE:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Main.main(Main.java:21)

Any guidance would be appreciated.

Fox715
  • 11
  • 1
  • 2
    Does your program work if you run it locally? – khelwood Feb 02 '17 at 15:49
  • 1
    Is line 21 the minutes = input.nextInt() line? What did you type in? – rajah9 Feb 02 '17 at 15:51
  • 1
    can you tell us how you run your code and what input you are giving. Because this is caused when you don't give required input. – jack jay Feb 02 '17 at 15:52
  • have a look at "How to use standard Java IO in Codenvy" -> http://stackoverflow.com/a/22341424/1560584 – Balaji Krishnan Feb 02 '17 at 15:54
  • @rajah9 - Yes, line 21 is that line. I'm getting the error message before the program will allow me to provide an int. – Fox715 Feb 02 '17 at 15:56
  • @khelwood No it does not work locally. – Fox715 Feb 02 '17 at 15:57
  • @jackjay I'm using the following command-line input for runtime: "cd ${current.project.path} && javac -classpath ${project.java.classpath} -sourcepath ${project.java.sourcepath} -d ${project.java.output.dir} src/Main.java && java -classpath ${project.java.classpath}${project.java.output.dir} Main" – Fox715 Feb 02 '17 at 15:59
  • @Mercury715 where is your input i.e `minutes` if you dont provide it how would your code knows about it – jack jay Feb 02 '17 at 16:09
  • http://stackoverflow.com/questions/29593166/why-is-this-code-getting-a-java-nosuchelement-exception?rq=1 this link contains the same problem and I believe the problem was fixed here. – cheesey Feb 02 '17 at 16:15
  • It's working locally in my machine with any IDE i think you comman-line is wrong, – Gatusko Feb 02 '17 at 16:16
  • @jackjay i'm not being provided the opportunity to input the minutes. Could it be that this problem is related to the IDE i'm using and not the code itself? – Fox715 Feb 02 '17 at 16:30
  • i dont think its a problem with IDE or your code. But you can try this to check what your code does. [codechef](https://www.codechef.com/ide) – jack jay Feb 02 '17 at 16:39
  • I agree with @jackjay. Show us what you typed in for minutes. Remember that `"2000"` and `2000` and `2ooo` are all read differently, and only the middle one is an int. – rajah9 Feb 02 '17 at 21:17
  • @jackjay IT IS a problem with the IDE or to be precise, it is a problem with using an online IDE and that OP doesn't know that he need to setup the online IDE using predefined values as input, since it won't open an user input on `Scanner#nextInt()`. The same applies to codechef. Run OPs code with and without "Custom Input". – Tom Feb 03 '17 at 16:11
  • @Tom i tried his code on codechef with custom input and it successfully runs. – jack jay Feb 03 '17 at 16:49
  • @jackjay And now test it without that custom input (as I said, test it with ***AND*** without). – Tom Feb 03 '17 at 17:28
  • @Tom i know without custom input it will throw error and in know it already . what point do you wanna make. – jack jay Feb 03 '17 at 18:06
  • @jackjay That your sentence *"i dont think its a problem with IDE"* is wrong. You can think it isn't a problem with the IDE, but in fact it is (and that OP doesn't know how to use his online IDE correctly). – Tom Feb 03 '17 at 18:28
  • @Tom problem is not with IDE problem is with OP being unaware of how to give input in cloud IDE. – jack jay Feb 03 '17 at 19:17
  • Code ended up running properly when run in Eclipse. As others stated, it was a problem with my runtime code in the cloud IDE. Compiling my program via the command-line with "javac Main.java" then running it with "java Main" once the .class file was created made it work correctly. Thank you for those that contributed. I still have much to learn, but am glad to be here. – Fox715 Feb 08 '17 at 03:37

0 Answers0