Compilation error in UVA 11854 - java

This is the link for the actual problem, here
I have submitted my code multiple times, but each time it had a compilation error. The original message was
"Main.java:4: error: class Egypt is public, should be declared in a file named Egypt.java
public class Egypt {
^
1 error"
I have no idea where I went wrong. I have copied my code for the problem below. please help me with this code:
import java.util.Scanner;
import java.util.Arrays;
public class Egypt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
int[] arr = new int[3];
for (int i = 0; i < 3; i++)
arr[i] = input.nextInt();
if((arr[0]+arr[1]+arr[2])==0)
return;
Arrays.sort(arr);
int d = (int)(Math.pow(arr[0],2) + Math.pow(arr[1], 2));
if(Math.sqrt(d)==arr[2])
System.out.println("right");
else
System.out.println("wrong");
}
}
}

From the Java specifications here,
All programs must begin in a static main method in a Main class.
Do not use public classes: even Main must be non public to avoid compile error.
So, I think you must use
class Main

Related

Infra or precheck error: No class with name found

I do not understand why I am getting this error.
Can any one explain what is the meaning of this error? What causes this error and how to resolve one.
Error:
Infra or precheck error: No class with name found
Here is my code:
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
String str;
String tstr;
str=sc.nextLine();
tstr=sc.nextLine();
char [] s = new char[n];
char [] t = new char[n];
for(int i=0;i<str.length();i++){
s[i]=str.charAt(i);
}
for(int i=0;i<tstr.length();i++){
t[i]=tstr.charAt(i);
}
int count=0;
for(int i=0;i<n;i++){
if(((s[i]+13)%90)>t[i]){
count=count+t[i]-s[i];
}
else if(((s[i]+13)%90)<t[i]){
count=count+t[i]-s[i]+13;
}
}
System.out.println(count);
}
}
you are probably doing coderena fights on hackerearth.
i was also facing the same error as i was using java version(1.8.1)
change it to 1.7.1 and you are good to go
You can compile 2 or 3 times..then it will be automatically compiled, and if again when you submit the code if again "Infra or precheck error: No class with name found " arise this message then again submit 2 or 3 times..it will definitely solved your problem..
In my case, I had missed an opening brace on one of my methods.

Problems with non-static variable inputs and trying to run methods

I've done a bunch research into trying to solve this issue (for about 2.5 hours), but I'm still not able to compile my program. I have tried making the method not static, but when attempting to run it, it gives me this error:
"Error: Main method is not static in class prog6, please define the
main method as: public static void main(String[] args)"
When the main method is static, I get following error in a compiler
Error: "non-static variable input cannot be referenced from a static
context
usd = input.nextDouble();"
I'm sorry if this question comes off redundant, I don't mean to ask without looking for an answer on my own, but I've been working at this for hours now and I don't understand what I'm doing wrong.
Some extra info on this program: it's meant to take inputs from the user to find out what currency they want to convert to, and how much USD they would like to convert. Then, I would invoke a method in order to do the calculations and return them. (Any amount trying to be converted over $200, will need 5% fee.)
import java.util.Scanner;
public class prog6
{
Scanner input = new Scanner(System.in);
public static void main (String[] args)
{
char curr = 0;
double usd;
double result;
while (curr!='Q' || curr!='q') { //loop
System.out.println("What type of currency would you like to buy?");
curr = input.next().charAt(0);
System.out.println("How many dollars would you like to convert?");
usd = input.nextDouble(); //asking user for info needed to convert
if (usd>200) {
usd = (usd)*(0.95);
}
result = calc (curr,usd); //invoke the method
}
}
public double calc (char mCurr,double mUsd) //method
{
if (mCurr=='E' || mCurr=='e') {
return (mUsd)*(0.88);
}
else if (mCurr=='P' || mCurr=='p') {
return (mUsd)*(0.77);
}
else if (mCurr=='Y' || mCurr=='y') {
return (mUsd)*(113.17);
}
return 0;
}
}
The main method will need to be static. From there, create an instance of your class and call a non-static method from the static main method. eg..
public class Prog6 {
private Scanner input = new Scanner(System.in);
public static void main (String[] args) {
Prog6 prog6 = new Prog6();
prog6.start();
}
public void start() {
char curr = 0;
double usd;
double result;
// etc...
}
}
You could make the member variable static but it's better form to use regular non-static members and methods and call this from the static main method.
There are two ways to solve your prolem
Change the input variable to static;
or
In main method, prog6 myprog= new prog6(); and refer input as myprog.input ....

Client Class Error in Java

Now, I also just have a simple question on the client class below, an error I'm getting, how to fix that error, and properly print (this.toString())
import java.io.*;
import java.util.Scanner;
public class IndexClient
{
File file = new File("file.txt");
System.out.println(this.toString());
}
Basically, I am getting an error on the second to last line that says identifier expected right before the after the println and before the first parentheses. Why am I getting that error, how would I fix that error, and then how would I successfully print (this.toString())?
Update Number 1:
I am not sure that this is entirely necessary; however, if you need it, my toString() method is below:
public String toString()
{
String sb = "";
for (int d = 0; d < words.size(); d++)
{
sb += "The word: " + words.get(d) + System.lineSeparator();
}
return sb;
}
Update Number 2:
I really appreciate all the help and constructive criticism of the code that I can get. I hope I didn't turn this simple question into one too complex. Thank you very much :)
Update Number 3:
I am sorry for leaving so many notes. However, I was only just wondering if this is a common question that any of you guys see a lot because it seems like this comes up a lot in class, and the teacher assisstant can't answer the question. Thanks again :)
You can't use :
System.out.println(toString());
Out side a method, you can use it inside a method or you can create your main method :
public void myMethod(){
System.out.println(toString());
}
Or
public static void main(String args[]){
System.out.println(toString());
}
You cannot have code outside a method in a java class.
You must create an object of your class to access this.
A correct minimal implementation would look like that (taken from the Online Java IDE):
import java.lang.Math; // headers MUST be above the first class
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
OtherClass myObject = new OtherClass("Hello World!");
System.out.print(myObject);
}
}
// you can add other public classes to this editor in any order
public class OtherClass
{
private String message;
private boolean answer = false;
public OtherClass(String input)
{
message = "Why, " + input + " Isn't this something?";
}
public String toString()
{
return message;
}
}

Calling non-static method (in a different class) from static

I've done some looking around on here for a solution, but I can't find one. I tried these ones and many others, and I run into the same issue.
I am trying to make a simple text game, and I run into the issue where I have a main class, and a class called "gameboard" that I have as an array defined like this:
static GameBoard[] gameboard = new GameBoard[9];
Now, this works fine until I try to change the characteristics of a single one of these array objects. I will do:
gameboard[input].setState(2);
and the specific instance of gameboard that should change will not be the only one: all of them change when I do this. It's weird. Only gameboard[**input**] should change, not all 9 of the gameboard instances. EVERY variable and method I have is "static", but because of the main method (public static void main...), everything seems to have to be static. How do I get rid of all this static?
GameBoard Class
package com.name.tictactoe;
public class GameBoard {
char[] States = {'N','X','O'};
char state;
public void setState(int s){
state = States[s];
}
public char getState(){
return state;
}
}
Main class (called Game)
package com.name.tictactoe;
import java.util.Scanner;
public class Game {
static boolean turn, win;
static GameBoard[] gameboard;
static Scanner kb = new Scanner(System.in);
static int input;
public static void main(String[] args){
gameboard = new GameBoard[9];
reset();
displayStates();
askTurn();
displayStates();
askTurn();
}
public static void askTurn() {
System.out.println();
System.out.println();
System.out.println("Where do you want to go? Use the numbers shown, where the first segment is the top and the last is the bottom - left to right.");
input = kb.nextInt();
if(input > 8){
System.out.println("Input out of bounds. Game over by default.");
try {
Thread.sleep(1000000000);} catch (InterruptedException e) {e.printStackTrace();}
}
gameboard[input].setState(2);
}
public static void reset(){
for(int i = 0; i < 9; i++){
gameboard[i].setState(0);
}
}
public static void displayStates(){
for(int i = 0; i < 9; i++){
System.out.print(gameboard[i].getState() + " ");
if(i ==2 || i ==5){
System.out.print(" II ");
}
}
System.out.println();
for(int i = 0; i < 9; i++){
System.out.print(i + " ");
if(i ==2 || i ==5){
System.out.print(" II ");
}
}
System.out.println();
}
}
UPDATE: The current answers don't work. Although Eclipse doesn't realize this, making GameBoard non-static causes null pointer exceptions when any method in it is referenced.
A static variable belongs to the class, not the object, so of course all of your GameBoards are being affected!
Just because you're in a static method doesn't mean you can't manipulate instance variables. First, make everything in your GameBoard class non-static (unless you really do need some of the values shared across all instances). This includes your instance variables and your getter/setter methods.
If your program works exclusively from the main method, then keep your GameBoard[] object static. Then, when you make that method call:
gameboard[input].setState(2);
This will change only the state of the GameBoard at index input.
Edit:
To instantiate your GameBoard[] with basic GameBoard objects inside of it, you can do this at the beginning of your main method:
for(int x=0; x<gameboard.length; x++) {
gameboard[x] = new GameBoard(); //Assuming you want to use the default constructor
}
The other objects in your array should not change. Can you add some more code for more context?
As far as I can understand you, you are doing something like:
public class Main {
public static String[] strings = new String[2];
public static void main(String[] args) {
strings[0] = "test";
System.out.println(strings[1]);
}
}
In my example output is "null" as expected.
How do I get rid of all this static?
Just create instances of your objects in the main function.
GameBoard[] gameboard = new GameBoard[9];
For what you describe to happen all the elements of the gameboard array must be set to the same object (nothing to do with the array being static), check your code where you populate the gameBoard array with new instances of the GameBoard class for a bug that could cause the same instance to be written to all elements (or post that code here so people can see the problem).

Passing values from main class to other classes in Java

I'm on a project that requires passing values from main class to other classes. All of the classes are correctly working when they have their main functions. However, my project should have only one main class that calls other functions in other classes.
An example for main and another class is below. I want to scan the input through main class and pass it to parse class. Current implementation gives two error which are:
Exception in thread "main" java.lang.NullPointerException
at url.checker.Parse.GetInput(Parse.java:16)
at url.checker.Main.main(Main.java:14)
Java Result: 1
Given input: -url=google.com,-time=5,-email=asdfg#hotmail.com
Main.java:
package url.checker;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the parameters as '-url=' , '-time=' , '-email='.");
Parse.s = in.nextLine();
Parse.GetInput(Parse.s);
}
}
Parse.java:
package url.checker;
import java.util.Scanner;
public class Parse
{
static String s;
static String result[];
public static void GetInput(String s)
{
int i;
for(i=0;i<3;i++)
{
if (s.startsWith("-url="))
result[0] = s.substring(5, s.length());
else if (s.startsWith("-time="))
result[1] = s.substring(6, s.length());
else if (s.startsWith("-email="))
result[2] = s.substring(7, s.length());
else
System.out.println("You didn't give any input.");
}
}
}
Thank you very much for your help.
static String result[];
You never initialized the array; Since its a static it gets initialized to null by default.
Initialize using static String[] strings = new String[10];. This will hold 10 elements of String.

Categories

Resources