Infra or precheck error: No class with name found - java

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.

Related

Java Scanner get char

import java.util.Scanner;
public class myClass {
public static void main(String[] args)
{
String[] exampleString;
char expression;
Scanner getExp = new Scanner(System.in);
while(expression = getExp.next().charAt(0))
{
// myString will added by expression character
}
}
}
I tried it on Eclipse Mars but
expression = getExp.next().charAt(0) part gets error always.
I didn't understand that what is the error .
Previously i think on this link
stackoverflow Scanner question
am i think wrong ?
You probably want
while (getExp.hasNext()) {
expression = getExp.next().charAt(0);
...
}

Compilation error in UVA 11854

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

Exception in thread "main" java.lang.NoClassDefFoundError:

import java.util.Scanner;
class BinarySearch
{
public static void main(String s[])
{
int a[] ,n ,i, c, lb=0, ub, mid, item;
Scanner in=new Scanner(System.in);
System.out.println("Enter size of array : ");
n=in.nextInt();
a=new int[n];
ub=n-1;
for(i=0; i<n ;i++)
{
c=i;
System.out.println("enter"+ c++ +"th element of array :");
a[i]=in.nextInt();
}
for(i=0; i<n ;i++)
{
c=i;
System.out.println(c++ +"th element of array is :"+a[i]);
}
System.out.print("Enter item which is to be searched from array : ");
item=in.nextInt();
mid=(lb+ub)/2;
while((lb<=ub)&&(a[mid]!=item))
{
if(item<a[mid])
ub=mid-1;
else
lb=mid+1;
}
if(a[mid]==item)
System.out.println(item+ "found at "+ mid+"th location");
else
System.out.println(item+ "not exist in this aaray");
}
}
This is a program of binary search of an array
This code is giving an Exception named NoClassDefFoundError.
main method not found exception is there.
please help me out in resolving this exception
The name of file should be BinarySearch.java.
You should compile using javac BinarySearch.java.
Launch using java BinarySearch
Your class should be the same name of the class file. like:
if your class file name is
BinarySearch.java
then the class should be named as
public class BinarySearch
There is no package statement, where are you running your java command? It is not able to find that class.

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.

Reading file and placing into array

I'm having troubles reading a file and then placing its contents into an array. The console says my error is here:
Exception in thread "main" java.lang.NullPointerException
at readFile.readFile(readFile.java:23)
at apples.main(apples.java:6)
However I do not now how to fix it.
import java.util.*;
import java.lang.*;
import java.io.*;
public class readFile {
private Scanner x;
public void openfile(){
try{
x = new Scanner( new File("/Users/Zachary/Desktop/chinese.txt"));
}
catch (IOException e){
System.out.println("you failed foo");
}
}
public void readFile(){
int y = 0;
int[] nums = null;
while(x.hasNext()){
for(y=0; y<10;y++) {
nums[y] = x.nextInt();
}
System.out.println(nums[y]);
}
}
public void closeFile(){
x.close();
}
}
public class apples {
public static void main (String[]args){
readFile r = new readFile();
r.openfile();
r.readFile();
r.closeFile();
}
}
You seem to have a NullPointerException at:
nums[y] = x.nextInt();
That is because of this line:
int[] nums = null;
It is null, so you can't put stuff in it. Here's a simple fix:
int[] nums = new int[10];
The above code will initialize nums so that it is an empty (not really - it's actually filled with 0) array like this:
---------------------
|0|0|0|0|0|0|0|0|0|0|
---------------------
If you want to be able to add as many numbers to it as you want, you will need an ArrayList (link).
Also, this code will throw an error:
for(y=0; y<10;y++) {
nums[y] = x.nextInt();
}
System.out.println(nums[y]);
That is because your System.out.println doesn't know what y is. Just move the System.out.println into the for loop so it can "see" y. (This is called scope)
The compiler error states that the error occurs at "readFile.java:23". This is line 23 of the file readFile.java. I believe that is this line:
nums[y] = x.nextInt();
The problem is that you declared nums as:
int[] nums = null;
When you try to access an array that is initialized to null, you should not be surprised to get a NullPointerException.
To fix the problem, you need to create an array object:
int[] nums = new int[SOME_SIZE];
You will need to provide the size yourself as you have not provided enough information for me to guess the value.
is the inner loop really necessary? you are looping x * 10 times which is really not a good way to assigned data.

Categories

Resources