I need help with my Java program below. Works on desktop but not on mobile devic
ID: 3572437 • Letter: I
Question
I need help with my Java program below.
Works on desktop but not on mobile device, was told it was because nextInt() is still waiting for input.
ERROR MESSAGE: NoSuchElementException at java.util.Scanner.throwfor(Unknown Source)
at java.util.Scanner.nextINt(Unknown Source)
at Inspiration.main(Inspiration.java:43)
COMPLETE CODE BELOW:
/*
Find out your lucky number and motivation today.
CHOOSE A NUMBER FROM 1 to 20.
Chance will take care of the rest...
*/
import java.util.Scanner;
import java.util.Random;
import static java.lang.Math.*;
public class Inspiration
{
private int theNum;
public Inspiration( int num )
{
theNum = num;
}
public void setTheNum( int num )
{
theNum = num;
}
public int getTheNum( )
{
return theNum;
}
public static void main( String[] args )
{
try
{
Scanner keyboard = new Scanner( System.in );
/* System.out.print ( "Pick a number from 1 to 20: " );
*/
int theNum = keyboard.nextInt( ); /* this user input assures that the final number is atleast one. */
Inspiration i = new Inspiration( theNum );
if (i.isValid( )) {
i.serendipidy();
i.inspirationMachine();
}
else {
System.out.println("Invalid user input! Please enter an integer from 1 to 20.");
}
}
catch (Exception e) {
e.printStackTrace();
// System.out.println("An error occurred");
}
}
/* serendipidy means the occurrence and development of events by chance in a beneficial way. */
public void serendipidy()
{
/* there is an if statement here because of the additional 9 digits that may be added to our value from the 'current time'. this matter is addressed where you find the following 3 $ ($$$) */
long chance; /* long to work with the System.currentTimeMillis() */
if( getTheNum() < 10 )
{
System.out.println(" You chose number " + getTheNum() );
System.out.println(System.currentTimeMillis()+ " is the current time in milliseconds. ");
long ten = 10; /* long to work with the
System.currentTimeMillis() */
chance = (System.currentTimeMillis() % ten); /* long's calculating */
int>
theNum += oneChance; /* this line makes it so that at the end. */
/*** System.out.println(theNum + " is the last digit of the previous number plus your number."); */
System.out.println(theNum + " is NOW your lucky number today." );
}
else
{
System.out.println(" You chose number " + getTheNum());
System.out.println(System.currentTimeMillis()+ " is the current time in milliseconds. ");
long ten = 10; /* long type used to work with the System.currentTimeMillis() data */
chance = (System.currentTimeMillis() % ten); /* long's calculating*/
int>
theNum += oneChance;
/* System.out.println(theNum + " is the last digit of the previous number plus your lucky number."); */
System.out.println(theNum + " is NOW your lucky number today." );
theNum -= 9; /* $$$ 9 becaue between the 'current time' and your favorite number, the highest number possible is 29
/* System.out.println(getTheNum() + " is the previous number minus nine (makes up for the milliseconds used from the current time.)"); */
}
}
public boolean isValid()
{
if(( theNum >= 1 && theNum <= 20 ))
{
return true;
}
else
{
return false;
}
}
/* the Inspiration Machine output is the perfect balance; half your choice, half by chance. */
public void inspirationMachine( ) /* Inspiration Machine. */
{
switch( getTheNum( ) )
{
case 1: System.out.println(" "Intelligence is the ability to avoid doing work, yet getting the work done." -Linus Torvalds");
break;
case 2: System.out.println(" "Let us train our minds to desire what the situation demands." -Lucius Annaeus Seneca");
break;
case 3: System.out.println(" "There's no reason to have a plan b, because it distracts from plan a." -Will Smith");
break;
case 4: System.out.println(" "Action is the real measure of intelligence." -Napoleon Hill");
break;
case 5: System.out.println(" "A goal is a dream with a deadline." -Napoleon Hill");
break;
case 6: System.out.println(" "You gravitate to that which you secretly love most." -Funkadelic");
break;
case 7: System.out.println(" "If you do not conquer self, you will be conquered by self." -Napoleon Hill");
break;
case 8: System.out.println(" "The best preparation for tomorrow, is doing your best today." -H. Jackson Brown, Jr.");
break;
case 9: System.out.println(" "What we think, we become." -Buddha");
break;
case 10: System.out.println(" "If we chase perfection, we can catch excellence." -Vince Lombardi");
break;
case 11: System.out.println(" "If you spend to much time thinking about a thing, you'll never get it done." -Bruce Lee");
break;
case 12: System.out.println(" "To hell with circumstances; I create opportunities." -Bruce Lee");
break;
case 13: System.out.println(" "To hell with circumstances; I create opportunities." -Bruce Lee");
break;
case 14: System.out.println(" "No matter what people tell you, words and ideas can change the world." -Robin Williams");
break;
case 15: System.out.println(" "The measure of who we are, is what we do with what we have." -Vince Lombardi");
break;
case 16: System.out.println(" "History repeats itself, so why not be on the winning side." -bernblend");
break;
case 17: System.out.println(" "The best way out is always through." -Robert Frost");
break;
case 18: System.out.println(" "Thinking will not overcome fear but action will." -W C Stone");
break;
case 19: System.out.println(" "Success is achieved and maintained by those who try and keep trying.." -W C Stone");
break;
case 20: System.out.println(" "The present is theirs; the future, for which I really worked, is mine." -Nikola Tesla");
break;
}
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
import static java.lang.Math.*;
class Inspiration{
private int theNum;
public Inspiration( int num ) {
theNum = num;
}
public void setTheNum( int num ) {
theNum = num;
}
public int getTheNum( ) {
return theNum;
}
public static void main( String[] args ){
try {
Scanner keyboard = new Scanner( System.in );
System.out.println("Please enter an integer from 1 to 20.");
int theNum = keyboard.nextInt( ); /* this user input assures that the final number is atleast one. */
Inspiration i = new Inspiration( theNum );
if (i.isValid( )) {
i.serendipidy();
i.inspirationMachine();
}
else
System.out.println("Invalid user input! Please enter an integer from 1 to 20.");
}
catch (Exception e) {
e.printStackTrace();
}
}
/* serendipidy means the occurrence and development of events by chance in a beneficial way. */
public void serendipidy(){
/* there is an if statement here because of the additional 9 digits that may be added to our value from the 'current time'. this matter is addressed where you find the following 3 $ ($$$) */
long chance; /* long to work with the System.currentTimeMillis() */
if( getTheNum() < 10 )
{
System.out.println(" You chose number " + getTheNum() );
//System.out.println(System.currentTimeMillis()+ " is the current time in milliseconds. ");
long ten = 10; /* long to work with the System.currentTimeMillis() */
chance = (System.currentTimeMillis() % ten); /* long's calculating */
int> theNum += oneChance; /* this line makes it so that at the end. */
System.out.println(theNum + " is NOW your lucky number today." );
}
else{
System.out.println(" You chose number " + getTheNum());
//System.out.println(System.currentTimeMillis()+ " is the current time in milliseconds. ");
long ten = 10; /* long type used to work with the System.currentTimeMillis() data */
chance = (System.currentTimeMillis() % ten); /* long's calculating*/
int> theNum += oneChance;
System.out.println(theNum + " is NOW your lucky number today." );
theNum -= 9;
}
}
public boolean isValid() {
if(( theNum >= 1 && theNum <= 20 ))
return true;
else
return false;
}
/* the Inspiration Machine output is the perfect balance; half your choice, half by chance. */
public void inspirationMachine( ) /* Inspiration Machine. */
{
switch( getTheNum( ) ){
case 1: System.out.println(" "Intelligence is the ability to avoid doing work, yet getting the work done." -Linus Torvalds");
break;
case 2: System.out.println(" "Let us train our minds to desire what the situation demands." -Lucius Annaeus Seneca");
break;
case 3: System.out.println(" "There's no reason to have a plan b, because it distracts from plan a." -Will Smith");
break;
case 4: System.out.println(" "Action is the real measure of intelligence." -Napoleon Hill");
break;
case 5: System.out.println(" "A goal is a dream with a deadline." -Napoleon Hill");
break;
case 6: System.out.println(" "You gravitate to that which you secretly love most." -Funkadelic");
break;
case 7: System.out.println(" "If you do not conquer self, you will be conquered by self." -Napoleon Hill");
break;
case 8: System.out.println(" "The best preparation for tomorrow, is doing your best today." -H. Jackson Brown, Jr.");
break;
case 9: System.out.println(" "What we think, we become." -Buddha");
break;
case 10: System.out.println(" "If we chase perfection, we can catch excellence." -Vince Lombardi");
break;
case 11: System.out.println(" "If you spend to much time thinking about a thing, you'll never get it done." -Bruce Lee");
break;
case 12: System.out.println(" "To hell with circumstances; I create opportunities." -Bruce Lee");
break;
case 13: System.out.println(" "To hell with circumstances; I create opportunities." -Bruce Lee");
break;
case 14: System.out.println(" "No matter what people tell you, words and ideas can change the world." -Robin Williams");
break;
case 15: System.out.println(" "The measure of who we are, is what we do with what we have." -Vince Lombardi");
break;
case 16: System.out.println(" "History repeats itself, so why not be on the winning side." -bernblend");
break;
case 17: System.out.println(" "The best way out is always through." -Robert Frost");
break;
case 18: System.out.println(" "Thinking will not overcome fear but action will." -W C Stone");
break;
case 19: System.out.println(" "Success is achieved and maintained by those who try and keep trying.." -W C Stone");
break;
case 20: System.out.println(" "The present is theirs; the future, for which I really worked, is mine." -Nikola Tesla");
break;
}
}
}
=====================================
output
====================================
Please enter an integer from 1 to 20. 12
You chose number 12
16 is NOW your lucky number today.
"If you do not conquer self, you will be conquered by self."
-Napoleon Hill
==========================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.