How to make two files within one project in an online compiler?
java - Solving mismatch exceptions only appearing in one compiler - Stack Overflow
java - Online Compiler won't run more than one class - Stack Overflow
Just Some Help
Videos
I'm using an online java compiler on the browser because my school issued laptop does not allow me to download Eclipse. But in this website, I can only have one file. I want to one file for construction and one for running but I'm unable to do it.
The error trace seems to indicate that the InputMismatchException is thrown when scan.nextInt() is called.
After your print System.out.println("Would you like to enter another location?");, if the user inputs anything other than an integer, scan.nextInt() will throw an exception. Even if you read the user's input as a string as suggested by someone else, the parseInt method in escape = Integer.parseInt(scan.nextLine()) will throw an error because the string may not be a valid integer.
I suggest adding try-catch block as follows around the scan.nextInt() call so that your program doesn't crash when a user inputs something other than a valid integer (like the number 8.238).
boolean inputValid = false;
System.out.println("Would you like to enter another location?");
while (!inputValid) {
try {
escape = scan.nextInt();
inputValid = true;
} catch (InputMismatchException e) {
inputValid = false;
System.out.println("Please enter a valid Integer");
}
}
This code will continue asking the user until the user enters a valid integer.
It might be because the online scanner's implementation of nextInt() is different and is causing the problems.
Perhaps trying to parse the input as a string and the as a double/int would work:
Scanner scan = new Scanner(System.in);
double x; // the latitude input
double y; // the longitude input
double n = -90; // the maximum north value
double e = -180; // the maximum east value
double s = 90; // the maximum south value
double w = 180; // the maximum west value
int escape = 1; // the value that dictates when it's time to exit the
// while loop
while (escape == 1) {
System.out.println("Please enter the latitude:");
x = Double.parseDouble(scan.nextLine());
System.out.println("Please enter the longitude:");
y = Double.parseDouble(scan.nextLine());
if ((x > n) && (x <= 90))
n = x;
if ((x < s) && (x >= -90))
s = x;
if ((y > e) && (y <= 180))
e = y;
if ((y < w) && (y >= -180))
w = y;
if ((x > 90) || (x < -90) || (y > 180) || (y < -180))
System.out.println("Incorrect Latitude or Longitude");
System.out.println("Would you like to enter another location?");
escape = Integer.parseInt(scan.nextLine());
}
System.out.println("Farthest North: " + n);
System.out.println("Farthest South: " + s);
System.out.println("Farthest East: " + e);
System.out.println("Farthest West: " + w);
Again, this depends on the online scanner's way of doing things.
I don't know how onlinegdp - online compiler will save another class file & reference it. If they have functionality then you can try to add them in package & import that file using package.
It's pain to see the folder/project structure on most of the online compiler. They are meant to run small snippet & not to run the whole project.
Below is the snippet you can run on onlinegdp without creating the different file.
public class Main
{
public static void main (String[]args)
{
Worker bob = new Worker ();
System.out.println (bob.getHours ());
}
static public class Worker
{
private int hours;
private double rate;
public Worker ()
{
hours = 999;
rate = 999;
}
public Worker (int nHours, double nRate)
{
hours = nHours;
rate = nRate;
}
public int getHours ()
{
return hours;
}
public void setHours (int nHours)
{
hours = nHours;
}
public double getRate ()
{
return rate;
}
public void setRate (double nRate)
{
rate = nRate;
}
public double paycheck ()
{
return rate * hours;
}
public void raise (double raise)
{
rate = raise + rate;
}
}
}
just rename the file with .java screenshot of new file added as tab with .java extension