Error while running a Java program [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I was trying to convert an array to a list and vice-versa and perform different methods of array and list respectively using the below code.
import java.util.*;
public class LinkedList{
public static void main(String[] args){
String[] things1 = {" seminar", " presentations", " hackathon", " movies"};
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
theList.add("tickets");
theList.addFirst("Hellow");
things1 = theList.toArray(new String[theList.size()]);
for(String x: things1)
System.out.printf("%s", x);
}
}
Now, the problem is that when I am running it from NetBeans it works correctly, but when I am trying it in normal text editor and running through terminal it gives an error:
LinkedList.java:10: error: type LinkedList does not take parameters
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
^
LinkedList.java:10: error: type LinkedList does not take parameters
LinkedList<String> theList = new LinkedList<String>(Arrays.asList(things1));
^
2 errors
I think the list should take parameter but the error is completely opposite.
Why is it so?

You import
java.util.*
And since there's a java.util.LinkedList in the standard libraries NetBeans seems to pick that one up instead while javac from the JDK uses your own LinkedList which doesn't take parameters (such as String in LinkedList<String). To fix this problem you can do one of the following
Rename your class
Wherever you use a LinkedList you put the full name such as com.myproject.LinkedList or java.util.LinkedList to make it explicit which one you want (replace com.myproject with the actual package that your own linked list is in)

Related

Array issues I keep getting compilation errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently working on some code but I'm having issues comparing two arrays, what am I missing?
package learn_Java_Fast;
//working on Arrays from Hello World
import java.*;
public class Arrays {
public static void main(String[] args) {
int[] arr1 = {0,2,4,6,8,10};
int[] arr2 = {0,2,4,6,8,10};
int[] arr3 = {10,8,6,4,2,0};
boolean result1 = Arrays.equals( arr1, arr2);
boolean result2 = Arrays.equals( arr1, arr3);
System.out.println(result1);
System.out.println(result2);
}
}
Your class name Arrays clashes with java.util.Arrays - rename your class to any other name and your code will compile.
Try to import the Arrays class directly with a more precise package naming in case of overlaps.
As Stepan pointed out, you have a name clash with java utility class, Arrays. You would be better served to rename your class. In addition, it is considered bad practice to import every single Java package through import java.*;. Instead, you can use import java.util.* to import the Java utility package.
Also, another poster incorrectly indicated that your array initialization is wrong; Your initialization via int[] array = {1, 2, 3} is in fact correct!

Does System.out.println support multiple arguments? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
public class Book
{
String bookName;
public static void main(String[] args)
{
Book Object = new Book();
Object.bookName = "Network Technology Design";
System.out.println("The book named", Object.bookName);
}
}
As you see, the
System.out.println("The book named", Object.bookName);
is wrong,but if I do that
System.out.println(Object.bookName);
No any error, why?
You need to concatenate the Strings with a "+", because System.out.println() only takes one parameter.
You have to do it like this:
System.out.println("The book named " + Object.bookName);
If you see the PrintStream class,then you can find that there are no such method println which accepts 2 arguments.
So
System.out.println("The book named", Object.bookName); is wrong and System.out.println(Object.bookName); is right
System.out.println expected String, and you try to pass additional paramters. As menthioned in comments, use string concatenation or foramt function
System.out.println(String.format("The book named %s", Object.bookName))
System.out.println() takes only one parameter - Any primitive data type or Object type. Need to make it one argument by concatenating or combining..!
PrintStream class overloadingprintln() method depending on arguments type it call the method and it have only one argument or no argument.
like println(), println(String x), println(int x) etc

Need help using subString [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
public class project5{
public static void main(String args[]){
String[] storage = {"123457897", "123456","654654654"};
int current;
current = Integer.parseInt(storage[1].subString(1,5));
System.out.println(current);
}
}
So I'm trying to, as an exercise, just get the first 5 numbers in the first thing of the array and parse it as an integer and store it as the variable current. It gives me the error:
test.java:5: error: cannot find symbol
current = Integer.parseInt(storage[1].subString(1,5));
^
symbol: method subString(int,int)
location: class String
1 error
What is it that I'm doing wrong?
There is no subString method on the String class. There only is substring (all lowercase).
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
For variable, class and method names casing matters.

Java .set for lists? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning the basics of lists in Java and I was wondering what .set did I already understand the concept of .add however I cant really find anything about .set and its relation to lists other then examples. I would really appreciate if someone could give me some insight on this command.
Set will specify the position in the List for the object you are storing. The List inteface represents an ordered collection of objects so the position is able to be changed. Similar to an array.
Look at the section Positional Access and Search Operators on: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
set(pos, elem) as per the Java docs:
Replaces the element at the specified position in this list with the
specified element.
This means that you can change the stored element/reference at a specific position in the list, as long as the position is within the allowed position bounds. So, if you have 3 elements within the list already, you can specify position in set(position,element) to a value between 0 and 2, inclusive. Here is a simple demonstration of how you can replace the 1th (so really the 2nd, as it's 0-indexed) element in an ArrayList and then set it back to the original value:
import java.util.ArrayList;
public class Foo {
public static void main(String[] args) throws Exception {
ArrayList<Integer> foo = new ArrayList<Integer>();
foo.add(1);
foo.add(1);
foo.add(2);
foo.add(3);
System.out.println(foo);
foo.set(1, 999);
System.out.println(foo);
foo.set(1, 1);
System.out.println(foo);
}
}
But really, this is explained more than clearly enough in the Java doc for List, so as others have said: read it and try it out next time.

ArrayList add method error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Here is the piece of code
List<String> list = new ArrayList<String>();
list.add("Monkey");
Eclipse says:
Multiple markers at this line
Syntax error on token(s), misplaced construct(s)
Syntax error on token ""Monkey"", delete this token
I wasted about an hour trying to understand what is wrong.
This situation makes me hate java.
Most likely you have placed your code outside of a method or class. Try the following:
import java.util.ArrayList;
import java.util.List;
public class Test {
public Test() {
List<String> list = new ArrayList<String>();
list.add("Monkey");
}
}

Categories