Can 'split' be used? [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 5 years ago.
Improve this question
I get this error "cannot resolve symbol method ‘split’ (java lang string)", when I try using the code below.
What could be causing the "split" error?
What is required to use "split" do I need to import android?
public void run() {
byte[] buffer = new byte[2048];
int mybytes;
String fields[];
while (true) {
try {
mybytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, mybytes);
System.out.print("|mybytes|:\t" + mybytes);
fields = mybytes.split(" ");
heat = Integer.parseInt(fields[1]);
speed = Integer.parseInt(fields[3]);

You need to Declare Your mybytes as String not as a int change it
Use this
String mybytes;
Instead of this
int mybytes;

Okay, first off I'll address your question:
You're trying to perform a split on an int, not a stream, change the line
fields = mybytes.split(" ");
to
fields = readMessage.split(" ");
Regarding the structure of the questions itself:
Next time you're going to ask a question refer to How to ask.
Please provide the log-cat (or just stack-trace) for the error.
Please explain what you tried to do.
Please provide the entire code related to your problem, and not just a couple of lines with a bunch of blank lines inside of them.
Again, next time please refer to the How to ask because if you post a question in the same format, you might very well be blocked from asking questions again.

I think you intend to split buffer, not mbytes

You are trying to split an int. Instead, use a String, like :
String mybytes;
mybytes = String.valueOf(mmInStream.read(buffer));
fields = mybytes.split(" ");
Or you can use
fields = readMessage.split(" ");

Related

Large string input isn't being read completely, on an online judge [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 days ago.
Improve this question
There's a question on Codechef. A test case in this, where a string of length 100000, is being input. But when i print the length of string after input, its only coming out to be 65526. Why is that so? Is the input not being taken properly?
For input I use,
JAVA CODE:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 131072);
int n = Integer.parseInt(br.readLine());
String c = br.readLine();
NOTE : I also tried doing it in C++, but same problem occurs there.
I also tried, increasing the buffer size in case it causes problem, but no effect.
Also, Scanner didn't work in java!
Can someone please tell me what's going wrong?

StringBuilder deleteCharAt not working [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 4 years ago.
Improve this question
String s = "world";
StringBuilder str = new StringBuilder(s);
str.deleteCharAt(0);
System.out.println(s);
this code outputs the following result : world , what am i doing wrong ? why is the first character of string not being deleted ?
The Issue with the StringBuilder and your code is that it does not exactly what you think it does.
A StringBuilder may take any CharSequence as a constructor argument but it will not alter the passed value directly. String in particular are immutable. Anyway StringBuilders do not alter the Object directly. They buffer the characters in a char[] which isn't immutable anymore.
To get the buffered value (and your new "altered" String) you have to call the toString() method of the StringBuilder since it will create a new String based on the interally stored buffer.
But since it System.out.println() implicitly calls the toString() method it is not needed here.
System.out.println(str);
you must put the value of your StringBuilder back to your string.
s = str.toString();
You are printing s, and s has not been changed. When you construct str using s, a copy of s is made. Told in a different way, the reference to each character in s and str is not the same, so even after your deletion in str, s stays the same.
Modify
System.out.println(s);
to
System.out.println(str.toString());

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'

How to initialize a String array with unknown length to null? [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'mm working on this program in which connects Prolog and Java GUI. And i am encountering this problem where i don't know how many solutions the prolog program is gonna pass to java and therefore i can't declare the String array with fixed-length. This is my code:
String[] options; int i;
Query qMeat = new Query(new Compound("meat", new Term[] {new Variable("X")}));
i = 0;
while(qMeat.hasMoreSolution()){
options[i] = "" + qMeat.nextSolution().get("X");
i++;
}
I am getting this NullPointerException, which i guess because i didn't initialize the String array to null. And i don't know how to do so. I tried java.util.Arrays.fill(options,"") But not helping =(
Please help.
If you don't know the required size of the array in advance, you should use an ArrayList instead.
List<String> options = new ArrayList<>();
while(qMeat.hasMoreSolution()){
options.add("" + qMeat.nextSolution().get("X"));
}

Illegal format string in TextIO.putf() method aka wrong Array Type? [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 guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.

Categories