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 did exactly what has been instructed in this tutorial(https://www.youtube.com/watch?v=hyVmCqiCNmY) of stringbuffer, but I am getting an error in Netbeans with an orange line right across this line of code
StringBuffer b = New StingBuffer("how are you doing today");
whereas when Iam doing the same thing in eclipse, there is no such error
Program:
public class StringBuff {
public static void main(String[] args) {
StringBuffer b = New StingBuffer("how are you doing today");
System.out.println(b);
}
}
I tried to add the image of the program from Netbeans, but I don't qualify yet to add images.
It seems that you have typo in New. Java is case sensitive so New is different than new.
BTW StringBuffer is supplemented by StringBuilder. The orange line does not indicate an error but code that should be improved.
This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
Read the official Java Tutorial, not some YT video.
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 am a complete beginner at Java, and I have encountered a problem with the While loops. I just started them, so to practice I copied the example code into Eclipse and tried to run it, but there is no output (code below).
I tried debugging it, so I searched for instructions and tutorials online. I get to the part where I am in the debugging view, and when I'm trying to run the While loop, nothing happens. I click the button to Step into, as all the instruction say, and there should be some output shown there, but there just isn't. Can someone help me with that? How can I get an output from that code? I tried it in Eclipse and TextPad text editor. The program complies okay, but when I run it there is no output.
Debugging view
After clicking step into
Code Picture
Code:
public static void main(String[] args) {
int value = 1;
while (value<10);
{
System.out.println("Hello!");
value=value+1;
}
}
Your while doesn't loops because you've inserted a semicolon ; after the while loop declaration.
You have to remove it to make your loop able to enter in its scope, like this:
public static void main(String[] args) {
int value = 1;
while (value<10) {
System.out.println("Hello!");
value=value+1;
}
}
You need to remove the ; by while (value<10) than it should work
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
This is very weird.
A class I wrote has the following data member:
final static long MAX_FILE_SIZE_BYTES = 50000000000L;
at one point in my code the following block is run
System.out.println("MAXFILESIZEBYTES: " + MAX_FILE_SIZE_BYTES);
and the output is:
MAXFILESIZEBYTES: -1539607552
My question is, why is this long value overflowing? Java is supposed to be machine independent, and longs are supposed to hold 64 bits. What gives?
Cannot reproduce.
50000000000L is 0x0000000BA43B7400.
-1539607552 is FFFFFFFFA43B7400, which is what you would get if you cast the value to int.
Ergo somewhere you are casting it to int. Maybe you have a shadowed variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
In this code, the user types in yes if they would like to play again
I have to use functions for this code, I cannot simply state while(again.equals("yes"));.
Part of code:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
My question is why is }while(running==true); a syntax error? I declared the variable above it, shouldn't that allow it to run?
You need a "do" statement first, i.e. do { statements; } while (condition). Also
you don't need to save the return from playAgain() into a variable, you can call it directly from the while().
There are many Java tutorials out there, try the official Oracle ones from starters: The while and do-while Statements
Update
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));