EDIT 2: Solved. Nothing special, just a wrong for-loop. Sorry.
This is my first question here. I am currently developing a GUI for easier creation of a config.xml for a certain importer software and I have basically written a data structure for XML with certain restrictions. Long story short, I tried to write a method as an alternative to toString which recursively prints all my data well-formatted. And well, as long as I don't get into the recursion everything goes just as planned. But as soon as I enable it...
This is the code:
private String details(boolean recursiveDetails, int tabs) {
if(recursiveDetails) System.out.println("DEBUG: launched details method with depth="+tabs);
String toStr = space(tabs)+"|============ <"+name+"> ============\n";
//recursion never reaches this point.
if(recursiveDetails) System.out.println("Debug check 1: before recursion with depth="+tabs);
int endlength = 28+name.length();
toStr += (space(tabs)+"| Parent: "+ parent +"\n");
if(contentString!=null && !contentString.equals(""))
toStr += (space(tabs)+"| Content String: \n| " + contentString+"\n");
toStr += (space(tabs)+"| Attributes: " + ((allAttr==null||allAttr.size()==0) ? "---" : "") + "\n");
for(Attribute a : allAttr)
toStr+=(space(tabs)+"| "+a+"\n");
toStr += (space(tabs)+"| Content: " + ((content==null || content.size()==0) ? "---" : "") + "\n");
//recursion!
for(Element e : content)
toStr+=(space(tabs)+"|" + (recursiveDetails ? e.details(true,tabs+1) : (" "+e)) + "\n");
if(recursiveDetails) System.out.println("Debug check 2: after recursion with depth="+tabs);
toStr += space(tabs)+"|";
for(int i=0; i<endlength; i++)
toStr+="=";
return toStr;
}
private String space(int tabs) {
String res="";
for(int i=0; i<tabs; tabs++)
res+=" ";
return res;
}
recursiveDetails is true when going for recursion, tabs is simply for indenting. content is a java.util.List of the same type as the class this method is in (Element), containing a variable amount of Element-objects. Every Element and Attribute object has a valid toString() method.
If I run the method with recursiveDetails = false on a small test structure, I get:
|============ <Catalog> ============
| Parent: <ImportConfig>
| Attributes:
| [Str | Project="null"]
| [Int | Priority="null"]
| Content:
| <Entry>
| <Entry>
|===================================
But once I run it with recursiveDetails = true I get this:
DEBUG: launched details method with depth=0
Debug check 1: before recursion with depth=0
DEBUG: launched details method with depth=1
Using breakpoints, I found out that everything works perfectly well, until the first line after the println. If I comment that line out, the program terminates at the next line and so forth. No Exceptions, no anything, no more printing.
I have tried using StringBuilder instead, replacing all + with .append(). I have also tried avoiding the a ? b : c operator, using classic ifs instead. The results are exactly the same.
Can anyone explain this to me?
EDIT: I am running Eclipse Java Neon on Ubuntu 16.04 Xenial with Java 8 OpenJDK amd64.
The loop in the spaces method is wrong. You have:
for(int i=0; i<tabs; tabs++)
and you almost certainly want to replace tabs++ with i++. Otherwise your loop will run indefinitely
The only possibility is that name is null and therefore name.length() throws a null pointer exception and your program dies.
Related
so ive done coding before but for some reason i cant seem to figure this one out and i keep over thinking the solution. so to summarize, the first method is supposed to return 1 oys, the second method should return 3 oys, and the third methods needs to return 5 oys. i've linked an screen shot to the project, it isn't for school or anything like that, just a friend trying to reteach me java, and then eventually full stacks sql. i mainly have dealt with android. project
Based on my understanding, you need to build a string in the loopedOyMethod() and return it. You can do this by using a while loop or a for loop.
while loop:-
String s = "";
while (x>0)
{
s = s + "Oy" + " ";
x = x - 1;
}
return s;
For loop:-
String s = "";
for(int i=0;i<x;i++)
{
s = s + "Oy" + " ";
}
return s;
I am currently developing a plugin in minecraft spigot. I don't believe this is a spigot problem though, I believe this is a java problem. I have a method called setInjury() in my class that is supposed to make a copy of a list, remove an item from the copy and replace the list with the new one. The weird thing is, this works. For one item in particular though, it just refuses to remove it. It will remove other ones, but not this one. I checked casing and everything. Even the boolean from .remove says it has been removed, yet it just doesn't.
(iStore is a file configuration file while store is my class it's in)
It used to be in an if else statement, but the boolean inside the remove works just the same. The weirdest thing about all of this is it was literally working yesterday.
List<String> sevs = iStore.getStringList(uuid + "." + section);
for(String s : iStore.getStringList(uuid + "." + section)) {
boolean isFracture = s.equals("Fracture") && injury.equals("Broken") ;
if(s.equals(injury)) {
continue;
}
sevs.add("Broken");
sevs.remove(isFracture ? "Fracture" : "Intact");
iStore.set(uuid + "." + section, sevs);
store.saveStore();
}
My other more messy code that does the same thing, to prove that it should remove it:
List<String> sevs = iStore.getStringList(uuid + "." + section);
for(String s : iStore.getStringList(uuid + "." + section)) {
if(sevs.contains(injury)) {
return;
}else if(injury.equals("Broken") && s.equals("Fracture")){
sevs.add("Broken");
sevs.remove("Fracture");
Bukkit.getLogger().info(Boolean.toString(sevs.remove("Fracture"))); //returns true??
iStore.set(uuid + "." + section, sevs);
store.saveStore();
}else{
sevs.add(injury);
sevs.remove("Intact");
iStore.set(uuid + "." + section, sevs);
store.saveStore();
}
List before running code:
rlegstat:
- Cured
- Fracture
//Desired result if injury is equal to broken:
rlegstat:
- Cured
- Broken
//Actual results
rlegstat:
- Cured
- Fracture
- Broken
There is probably an obvious answer. This question might have already been asked but I don't know how to word the question. I'm working in Java and in this moment, I am reading the input text from the command line and converting that stuff over to strings.
I am, for sure, inputting the x character into the command line and whether I set the code to
!(first.equals("x")) or (first.equals("x"))
I still get the system.out output text. I noticed that if I remove the || and the following equals snippet it works as intended and continues onto the code. However, I have to have either x or y be options for the first arg string. Can someone please let me know what I am doing wrong. Thanks.
Here is the snippet of code:
private something(String[] args)
{
first = args[0];
second = args[1];
third = args[2];
if (!(first.equals("x")) || !(first.equals("y")))
{
System.out.println("First is the problem " + first);
}
}
Here is the ouput:
First is the problem x
Edit: I also did this and got the same result:
if (first.equals("x") == false || first.equals("y") == false)
{
System.out.println("First is the problem " + first);
}
I'm using this if-statement as a check for whether or not the two values are inputted. If they aren't then the if-statement should trigger.
It works when I have it like this, but I end up losing the y:
if (first.equals("x") == false)
{
System.out.println("First is the problem " + first);
}
first is either NOT "x" or NOT "y". This will always be true for whatever string first is set.
=> Are you not republican or are you not democrat? ;)
This code appears to work. The if statement returns as true if args[0] equals either x or y. !(first.equals("x")) means that it is true for every string except "x", so this would do the opposite of what you want. Also, you need to define first, second, and third otherwise it won't compile.
Edit:
Looking at your edit, it appears the issue is caused by ||. Since this means 'or', first only has to not equal either x or y for the if statement to run. Therefore, since it can't equal both at once, the if statement will always run. You should use &&(and) instead. Also, notice that == false can be replaced with !.
For example:
if (!(first.equals("x")) && !(first.equals("y")))
{
System.out.println("First is the problem " + first);
}
Hey guys I have the following if condition...
//Add a space if necessary
if (i!=0 && spaceIn>0 && i%spaceIn==0) {
System.out.println("Converted Letter : " + curLtr + " at position " + i + " Needs a space");
curLtr = curLtr + " ";
};
The 1st 2 conditions are always true with the test input i give it.
Whats happening is the modula condition i%spaceIn==0 is not reporting as true when it is.
Example when i is 3 and spaceIn is 3 i%spaceIn=0 if condition works.
When i gets to 6 even tho i can see (from system.out further along) that the answer is 0 its not triggering the if condition.
Sometimes it wasn't doing it when i=12 either!
So weird.
Im printing out the answer to i%spaceIn throughout the loop and even tho the answer is 0 every multiple of 3 comes it sometimes wont trigger the if condition.
Same thing if spaceIn is 5. It skips 10. What ever number it is it seems to just skip sometimes for no reason.
What am i missing?
Use the following code...
You have terminated the if block... It can cause a problem...
if (i!=0 && spaceIn>0 && i%spaceIn==0) {
System.out.println("Converted Letter : " + curLtr + " at position " + i + " Needs a space");
curLtr = curLtr + " "; }
I hope it might work for you as it runs fine on my machine....
Sorry for really stupid question, I'm learning a new language and taking this code:
public class Exercise01 {
int i;
char c;
public static void main(String[] args) {
Exercise01 E = new Exercise01();
System.out.println("i = " + E.i);
System.out.println("c = [" + E.c + "]");
}
}
/* Output:
i = 0
c = [
*/
Why the output does not produce "]" character? Has it something to do with Unicode?
PostEdited: the variable E.c was not initialized for experimentation purpose.
It may be that the place your program is outputting to, a console or a window, is getting confused by the U+0000 character which is the value of E.c.
It works fine for me.
Initialize E.c and try again.
You are trying to print the null character as your char c hasn't need initialised. i.e. \0 Interestingly you can't copy and paste this character easily as most C code sees this as an end of string marker.
I see the ] when I run the code.
Try changing your code with
char c = '?';
gives me an output of
i = 0
c = [?]
One way to reproduce this problem is to run on unix
java Main | more
which outputs
i = 0
c = [
Probably has to do with the fact that E.c isn't initialized to anything
I think it is because c is not initialized and therefore holds \0, i.e. "end of line". So, println prints until end of line and does not print your ]
You should initialize your char C as well as the int i. Good code practice: It is important to initialize your variable once you declare a variable!