I am writing a simple Java program to rotate a Linked List, but when trying to compile the Java code through the compiler via: javac RotateLinkedList.java, I get the following error in my console:
jared#jared-linux:~/Desktop/Code Interviews$ javac RotateLinkedList.java
./LinkedList.java:9: error: class ListOperations is public, should be declared in a file named ListOperations.java
public class ListOperations{
^
RotateLinkedList.java:82: error: cannot access LinkedList
LinkedList<Integer> ll = new LinkedList<Integer>();
^
bad source file: ./LinkedList.java
file does not contain class LinkedList
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Which is odd as this looks to be that the source code for Java is what is failing? I am compiling against Java JDK 8 and on Ubuntu 14.04.
Below is a snippet of the code I am running to create the LinkedList, if that helps:
import java.util.*;
public class RotateLinkedList {
public static void main(String args[]) {
// Crate the linked List
LinkedList<Integer> ll = new LinkedList<Integer>();
// Adding elements to linked list
System.out.println("How many elements should be in the binary linked list: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if((num != null) && (num > 0)) {
System.out.println("Adding " + num + " elements into the linked list.");
for(int i = 0; i < num; i++) {
ll.add(i);
}
System.out.println("Linked List: " + ll);
} else {
System.out.println("NULL or invalid number inputted. Aborting");
}
}
}
Appreciate any feedback! Cheers.
The compilation error message is quite straight forward: The name of your java file is not the same as that of the public class defined in it.
It is one of the basic constructs of Java language that the file name should match exactly with the publicly declared class in that java file. Note here that there could be multiple classes in a java file, nevertheless, only one publicly declared class in it!
Just to confirm, try changing the name of your java file to "ListOperations.java" and then compile it.
So I was an idiot and forgot I had a LinkedList.java (which is mostly psuedocode, etc) file in my source folder when compiling, so the compiler wanted to import that file when compiling RotateLinkedList.java. Argh! Sorry about that. Appreciate the views / responses, though!
Related
I tried to import Combinations by
import java.lang.org.apache.commons.math3.util.Combinations;
but I keep getting error when I use Combinations in my source code.
import java.util.*;
import java.org.apache.commons.math3.util.Combinations;
public class PowerSet{ //gets power set for a set containing first n integers
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(args[0]);
for(int i=0; i<=n; i++){
Combinations c = new Combinations(n,i);
Iterator iter = c.iterator();
while(iter.hasNext()){
int[] iarr = (int[])iter.next();
System.out.print("{" + iarr[0]);
for(int a=1; a<iarr.length; a++){
System.out.println(", " + iarr[a]);
}
System.out.print("}, ");
}
}
}
}
And the error I get clearly says the class does not exist. Am I getting the hierarchy wrong or the way I should have imported the class is wrong?
package java.org.apache.commons.math3.util does not exist
import java.org.apache.commons.math3.util.Combinations;
^
PowerSet.java:11: error: cannot find symbol
Combinations c = new Combinations(n,i);
^
symbol: class Combinations
location: class PowerSet
As you can see from your import statement
java.lang.org.apache.commons.math3.util.Combinations;
is not in core java / jee package rather referring a 3rd party package repository. so you need to download that package and put that into your classpath or project lib directory or use your IDE or compile command to point that package somewhere in your system. however, now a days devs are using various build tool such as maven or gradle to manage such project dependencies overheads.
This a very quick and i feel obvious mistake but i keep getting the CANNOT FIND SYMBOL
symbol : method print(int,int)
this would lead me to believe that i'm not giving the method the right data type parameters, however..
public class Test
{
public static void main(String[] args)
{
TestSrv srvObj = new TestSrv();
srvObj.print(0, 0);
srvObj.print(1, 1);
srvObj.print(2, 10);
}
}
and this method, what it's meant to do aside, i keep getting errors from the above code for all 3 calls to the print method? I am passing it integers on all 3 occasions?
public class TestSrv
{
public void print(int num, int count)
{
for (int i = 0; i <= count; ++i)
{
System.out.print(num + ". " + "*");
}
}
}
Your code should compile. Make sure that you declare both classes in the same package or that you import TestSrv in Test.java.
You almost certainly didn't compile TestSrv after making changes. Using an IDE such as Eclipse or IDEA will take care of much of that detail for you.
while renaming my method and class and such so that it wasn't what i originally named it(as to not confuse anyone) i actually fixed the problem that i had.. that is why this compiled for everyone xD i feel stupid! thanks again
import javax.Swing.JOPtionPane;
public class while looping {
public static void main (String []args){
String a= JOptionPane.ShowInputDialog("Please enter an Integer");
int b=Integer.parseInt(a);
int j=1;
while(j<=b){
j++;
int k=1;
while (k<j){
System.out.print("*");
k++;
}
System.out.println();
}
}
}
}
This is my program and when i run it it says that javac: invalid flag: while what does it mean?
The "invalid flag" is probably related to the way you call the compiler. Do you call the compiler from a command line? How does the call look like?
You also have an error in your code, unrelated to the error message: you call your class "while looping". This is wrong on 2 (or 3) points: a class name must be one word, a class name must not be a reserved word ("while"), and a class name should start with a capital letter.
The last point is just a naming convention, but it makes life easier for everyone who has to read your code.
You can even see your error in your question with the highlighting:
public class while looping {
}
A class name may not contain any blank character.
Also it is usual to start class names with a capital letter.
Please rename your class to
public class WhileLooping {
}
and also rename your .java file accordingly.
I am attempting to access an ArrayList that was created in a different method within the same class. The scanner method pulls in data from a text file. The text file data appears this way: 123 12 1 43, with line breaks...
Currently, my method works to pull in the data, but does not compile after that method ends. I originally had the entire code within the same method and it worked fine. But I'd like to return the largest value by creating a new method within this class, and then create a tester class that will access this new method. Here is my existing code. Or if there is a better solution. I'm all ears.
public class DataAnalyzer {
public DataAnalyzer(File data) throws FileNotFoundException
{
List<Integer> rawFileData = new ArrayList<>();
FileReader file = new FileReader("info.txt");
try (Scanner in = new Scanner(file)) {
while(in.hasNext())
{
rawFileData.add(in.nextInt());
}
}
}
public int getLargest(rawFileData){
int largest = rawFileData.get(0);
for (int i = 1; i < rawFileData.size(); i++){
if (rawFileData.get(i) > largest)
{
largest = rawFileData.get(i);
}
}
for (Integer element : rawFileData){
if (element == largest)
{
System.out.print("This is the Largest Value: ");
System.out.print(element);
}
}
}
}
Your main issue is with your method declaration. It needs a type parameter:
public int getLargest(List<Integer> rawFileData)
Note the List<Integer>.
Now, there is already a method for this in the Collections utility class. You would do well to look over that link in detail - there are many useful methods there. To get the highest element from a Collection of Objects that have a natural order (such a Integer). For example
int largest = Collections.max(rawFileData)
So your method can be reduced to:
public int getLargest(List<Integer> rawFileData)
return Collections.max(rawFileData);
}
You need to think over your logic much more carefully before you begin to write code, for example, your first loop is good:
int largest = rawFileData.get(0);
for (int i = 1; i < rawFileData.size(); i++){
if (rawFileData.get(i) > largest)
{
largest = rawFileData.get(i);
}
}
You do exactly what any programmer would do. But then, instead of returning the largest when you find it, you for some reason loop again:
for (Integer element : rawFileData){
if (element == largest)
{
System.out.print("This is the Largest Value: ");
System.out.print(element);
}
}
Ask yourself what does this do? You have a List of, say, apples. You look at each one and compare them - finding the largest apple. You now have the largest apple in the List. You then loop over the List again looking for an apple that matches the apple you have already found. Why do this?
Further, you never return from the method. Your method is declared as returning an int; but you never do.
The missing type in your method definition is the problem here.
Change the method definition from
public int getLargest(rawFileData) {
....
}
to
public void getLargest(List<Integer> rawFileData) {
....
}
And the second for loop in the method is unnecessary. The largest integer is already stored in the variable "largest" and you can print it after the first for loop.
I am trying to run this program but I cannot, the compiler is sending me a ".class" error.
Can somebody help me with my problem and if it is possible a general tip about ".class" error?
Here is the program:
import java.io.*;
class Bus
{
private int kostos;
private int plithos;
private int typepiv;
Bus(int x,int y,int z)
{
kostos=x;
plithos=y;
typepiv=z;
}
public void KB(int[] x)
{
try{
for(int i=1;i<5;i++)
{
if(typepiv==2)
{
plithos=plithos+plithos/2;
kostos=kostos-kostos/2;
}
if(typepiv==3)
{
plithos=plithos-plithos/5;
kostos=kostos-kostos*25/100;
}
if(typepiv==1)
{
plithos=plithos;
kostos=kostos;
}
x[i]=plithos*kostos;
}
} catch(Exception ex){
ex.printStackTrace();
}
}
}
class testBus
{
public static void main(String args[])
{
String leof[]=new String[4];
int leof1[][]=new int[4][3];
for(int i=1;i<5;i++)
{
System.out.println("dwste onoma leoforiou");
leof[i]=UserInput.getString();
System.out.println("dwste kostos thesis enilika");
leof1[i][1]=UserInput.getInteger();
System.out.println("dwste plithos thesewn");
leof1[i][2]=UserInput.getInteger();
System.out.println("dwste tupos epibath gia enilikes=1,gia
paidia=2,gia suntaksiouxous=3");
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
----->leof2.KB(KostEnoik);
System.out.print("onoleo");
System.out.print(" ");
System.out.print("plithos");
System.out.print(" ");
System.out.print("kost(EURO)");
System.out.print("typepiv");
System.out.print(" ");
System.out.print("apotelesma kostEnoik");
for(int g=1;g<5;g++)
{
System.out.print(leof[g]);
System.out.print(leof1[g][2]);
System.out.print(leof1[g][1]);
System.out.print(leof1[g][3]);
System.out.print(KostEnoik[g]);
}
}
}
the compiler message says :
testBus.java:56:error:cannot find symbol
leof2.KB(KostEnoik);
symbol:bariable leof2
location:class testBus
1 error
Remove the array brackets [] when invoking KB
leof2.KB(KostEnoik);
and remove the preceding enclosing brace }.
Aside: Java naming conventions indicate that variables start with a lowercase letter e.g. kostEnoik. Also consider giving the method KB a meaningful name, e.g. calculateCost
Read Java naming conventions
concern is with your access
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it is defined i.e. outside for loop and scope is upto for loop and that's why the compiler will not be able to find that varialble .
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it's defined (in this particular case, the for loop) and that's not allowed.
method KB takes an int array as argument, but you don't have to add the [] when passing the argument. The correct line is
leof2.KB(KostEnoik);
However, there's something pretty odd with you logic: you're repeatedly (for loop) setting leof2, but only the last iteration of the loop will have any effect. I'm almost certain that that's not what you actually want, but the correct answer to where Bus leof2 should actually be defined depends on the correction of that issue.
leof2.KB(KostEnoik); this is the main culprit. whether you have imported UserInput.
Also try to go through the Java Basics
any method can be invoked using object when it is non static or class name when it is static. Please consider this link
Get leof2 object out side the For Loop.
Don't type [] when you pass the array as argument "leof2.KB(KostEnoik[]);".