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.
Related
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.
I was going through some basics and wrote the following code for 2D array and saved it under 'Matrix.java' name. Then I compiled it in CMD and found that Matrix.java is been converted to matrix.class. Is this normal?
As per my knowledge Java compiler does not change the file-name syntax. And I never faced such thing previously, am I missing something here?
I can not post the image of the screenshot of CMD because of the low reputation marks here :/
//Initialize a Two Dimensional Array
class matrix {
public static void main( String args[]){
double matrix[][] = {
{ 0*0, 1*0, 2*0,3*0},
{ 0*1, 1*1, 2*1,3*1},
{ 0*2, 1*2, 2*2,3*2},
{ 0*3, 1*3, 2*3,3*3},
};
int i,j;
//printing the array
for(i=0; i<4; i++){
for(j=0; j<4; j++){
System.out.print(matrix[i][j] +" ");
}
System.out.println();
}
}
}
This is because of your class name class matrix. Java compiler create class file from those.Java recommend to use java class name and file name should same exactly.
Mess up with upper and lower Char in Class name and File name. Your class should be named exactly the same as the file:
//Initialize a Two Dimensional Array
class Matrix {
public static void main( String args[]){
double matrix[][] = {
{ 0*0, 1*0, 2*0,3*0},
{ 0*1, 1*1, 2*1,3*1},
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
Basically I create a custom object in one program and instantiate with values in one program, then I create another program and want to print the instantiated object output.
import java.util.Scanner;
class example {
int value;
String name;
String city;
}
public class Yummy21 {
static example[] obj=new example[3];
static Scanner input=new Scanner(System.in);
public static void main(String args[])
{
init();
}
public static void init()
{
for(int i=0;i<3;i++)
{
obj[i]=new example();
}
for(int i=0;i<3;i++)
{
System.out.println("for the object "+i+" enter the name ");
obj[i].name=input.next();
System.out.println("for the object "+i+" enter the city ");
obj[i].city=input.next();
System.out.println("for the object "+i+" enter the name ");
obj[i].value=input.nextInt();
}
}
}
And the next program:
public class Yummy22 extends Yummy21 {
public static void main(String args[])
{
for(int j=0;j<3;j++)
{
System.out.println("the value of the object["+j+"].name is "+obj[j].name);
}
}
}
The first program works fine, meaning it takes the values and the second program shows NullPointerException.
Yummy22.main does not invoke Yummy21.init. perhaps put a
static {
init();
}
in Yummy21.
This being said, these 3 classes are abominations of Java. Please at least follow naming conventions. Class example should be Example. Also better to code all this with object level fields, constructors and non static members
serialize the data from program 1 and write it to disk and then read it from program 2.
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.