Array issues I keep getting compilation errors [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 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!

Related

Cannot resolve method Color.valueOf(int) [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 5 years ago.
Improve this question
I want to create Color objects from Strings like "#b66c61" and"#33b7c4".
This is my code:
import android.graphics.Color;
......
String color_string = "#b66c61";
int color_int = Integer.parseInt(color_string.substring(1, color_string.length()-1));
Color color = Color.valueOf(color_int);
when I run it, I get the errer: Cannot resolve method valueOf(int)
although I'm sure the method exists : https://developer.android.com/reference/android/graphics/Color.html
any help?
You just have to use parseColor. see below -
String color_string = "#b66c61";
int myColor = Color.parseColor(color_string)
// use int color to set Color
myLayout.setBackgroundColor(myColor);
The method valueOf has been introduced in Android from API 26 onwards only. So it won't be available in other APIs and also there is no support library out there yet for 26. Thee exact use of this method would be illustrated only when things get out more clearly after the launch.
Check this
you can use
public static int parseColor (String colorString)
like this
String color_string = "#b66c61";
int color = Color.parseColor(color_string);
From Android documentation:
Supported formats are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'

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

IndexOutOfBounds error, hangman game [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm making a quick hangman game, and came across an IndexOutOfBoundsException and was wondering why. I don't see the problem/how this error would come about.
It happens at this line:
array[index]+=c;
Any feedback is appreciated.
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[]args) throws IOException {
Scanner console = new Scanner (System.in);
String[] phrase={"television"};
String[] array= new String[phrase.length];
int body =6;
while(array!=phrase) {
char c=(char)System.in.read();
int index= console.nextInt();
array[index]+=c;
if(array[index].charAt(index)==phrase[index].charAt(index)){
System.out.println("the new array");
}
}
}
}
There are many Issues with the code. few of them are below.
you are creating array of size "phrase.length" which will be of size 1, when i enter 2 for "console.nextInt();" it will throw index out of bound.
Array Equality check is wrong, you need to do something like
if( Arrays.equals(array1, array2) )

Error while running a Java program [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 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)

"Not a statement" Compile Error in java [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
I am having a problem when I am trying to compile a pattern program. I am creating this program in BLUEJ and when I am trying to compile it shows the error : "not a statement"
class pattern
{
public static void main()
{
int p=0;
for(int i=1;p=1;i<=4;i++,p++)
{
for(int j=1;j<=i;j++)
{
System.out.print(Math.pow(p,2);
}
System.out.println();
}
}
}
What is the problem?
A couple of issues there, the main one being this:
for(int i=1;p=1;i<=4;i++,p++)
// ^ ^ ^
The for statement consists of three, not four, parts separated with ;. I suspect you wanted
for(int i=1,p=1;i<=4;i++,p++)
// ^--- comma here
Separately, I believe you have to specify the argument to main even if you're not using it, so:
public static void main(String[] args)
In a comment on the qustion, cadrian pointed out a further problem:
System.out.print(Math.pow(p,2);
// Missing ) here ------------^

Categories