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 3 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a function that removes all spaces in the given string. This is my code:
fun nonSpaceString(s: String): String {
var result = " "
for (index in 0..s.length-1) {
if (s[index] != ' ') {
result += s[index]
}
}
return result
}
fun main(){
nonSpaceString("abc d e")
}
But the result is holding "a" and then holding "ab" but the end result holds nothing. Does anyone know what I'm doing wrong here?
You have initialized the result with a space character. So the result starts with a space that may confuse you. Apart from that, your function is working fine.
Additionally, you can use the replace function to remove all spaces from a string
"abc d e".replace("\\s+".toRegex(), "")
Related
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
I want to build a loop where the console keeps asking me for a string until I write the word "out". This is what I have till now, but it doesn't loop and I can't figure out why. Also I really want to use while or do while if possible.
Scanner input = new Scanner(System.in);
String name = input.nextLine();
while (!name.equals("out")) {
System.out.println(name);
break;
}
You're not updating name inside the loop and remove the break otherwise you're jumping out of the loop in the first iteration (which is not what you want)
while (!name.equals("out")) {
System.out.println(name);
name = input.nextLine(); // I've added it here for you
}
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
I have this sample from my project and I need to know why the result is what it is.
public class Main
{
public static void main(String[] args)
{
//url: https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup, websiteList: http://classicpartyrentals.com/, URL Contains Returns bool: false
String url = "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup";
String contains = "http://classicpartyrentals.com/";
System.out.println("Returns bool: " + url.contains(contains));
}
}
Output:
Returns bool: false
Code is always doing what you ask it to do:
String url = "https://classicpartyrentals.com/products/24681-gothic-
but
String contains = "http://classicpartyrentals.com/";
https versus http!
So the real answer is: especially when you are a beginner, chances that your code uncovered "some Java bug" is relatively small (very close to zero in reality!)
There is a much higher chance that your assumptions are wrong. You either don't fully understand the methods you are calling, or there is a subtle flaw in your input data.
Finally: also work on your naming. contains isn't a very good name here; you better call it expectedUrl, or something alike!
In your Code Url "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup" contains https
but your compare string "http://classicpartyrentals.com/" contain http so its not match and return false
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'm writing a little application that receives instructions in a StringBuilder filled with "(" and ")", for every ( the application sums 1, for every ( it decreases 1. I know it's a bit weird but it's a puzzle and it had to be done like that.
I'm having problems reading the content from the stringbuilder, Eclipse is complaining that it cannot access the specified index with charAt, what am I doing wrong?
I already checked and StringBuilder instructions is valid and properly filled with ( and ), no nulls or other symbols.
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==")"){
//do something
}
else(){
//do other thing
}
}
To use a Char in Java you use ' not "
so it should be :
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==')'){ //<-- note the change of " to '
//do something
}
else{
//do other thing
}
}
}
Edit 1:
The above solves the complication problem however
as #cricket noted, it should be charAt(i) for correctness of the code
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
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)
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
Problem solved : Used wrong Color Package
I am trying to read some values from an csv file, and when I put three of the numbers together, they form an rgb-value.
However, for some reason, my IDE, Netbeans, is giving me the error:
'new Color(float,float,float) has private access in the class Color'
I have no idea how using parseInt can yield a float, even after casting the result to an integer as well.
Thank you for your time and patience.
public void initBasicRGB(String definitionCSVContent) {
String[] lines = definitionCSVContent.split("\n");
String[] values;
for (String s : lines) {
values = s.split(";");
if (!s.isEmpty() && values.length==6 ) {
int red = (int)Integer.parseInt(values[1]);
int green = (int)Integer.parseInt(values[2]);
int blue = (int)Integer.parseInt(values[3]);
String nameProvince = values[4];
basicRGB.put(new Color(red,green,blue), nameProvince);
//the error is on the line above
}
}
}
I used a package from awt instead of fx