Will this print a file? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
What would this code do?
FileStream object = new FileStream("c:\input_final.txt",true);
I thought it would not run because they are not two \ and therefore see it as a escape character. Am I wrong and would it print/show/open the file?

No. It will not, because it is not legal code.
Unless you have a custom class FileStream (which you have not posted) then I assume you meant one of -
FileOutputStream which will not work because c:\input_final.txt is not a valid String; \i is not a valid escape sequence.
and
FileInputStream does not have a constructor that takes a boolean as the second argument.

Will this print a file?
No.
It is not valid code. It won't compile ... let alone run!
It is most likely that you mean something like this:
... = new FileOutputStream("c:\\input_final.txt",true);
which says "create an output stream for appending to file "c:\input_final.txt". But even that WILL NOT "print" or "show" the file, or "open" it in an application / browser / editor or whatever.

Related

Is there a way to use python and java in the same program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.

Math.max are there some Syntax errors I'm not seeing? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
These lines are the culprit:
I was getting negative numbers so I decided to use the Math.max to ensure that the results would be no less than 0, but now I'm getting errors. My search just found me this method to use, but is there some particular Syntax I need in this form?
x.hp-=Math.max(0,(y.atk-x.def));
y.hp-=Math.max(0,(x.atk-y.def));
Everything works now, Thanks.
The incorrect edit was due to a redundant ( opening parentheses.
) is missing in for Math.max method. i.e (Math.max(0,(y.atk-x.def)))
(Math.max(0,(x.atk-y.def)))
(Math.max(0,(y.atk-x.def)))
end both with " ) "
Contrary to other answers here,
(Math.max(0,(y.atk-x.def))
has an extra parenthesis, at the beginning. Remove it:
Math.max(0,(y.atk-x.def))
You can indeed put an extra pair around the whole thing, as you have started to do, and as other answers appear to insist is necessary, but it isn't. It's redundant.

Text not being read into variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Here is my code, I am supposed to read in data such as AAABBBCCCDDD and output a3b3c3d3.
I have updated the code, and now the code compiles and runs, however nothing is output. I dont know if its the way im reading data in or if the code is incorrect.
String text;
FileReader data = new FileReader("input.txt");
BufferedReader in = new BufferedReader(data);
text=in.readLine();
in.close();
//Counter looks at length of data
int counter=0;
//Counter2 looks at current letter or number to make see if its the same then iterates it
int counter2=0;
while (text.charAt(counter)<=text.length())
{
while (text.charAt(counter)==text.charAt(counter2+1))
{
counter2++;
}
System.out.println(text.charAt(counter) + counter2);
counter=counter2;
}
The reason the compiler is complaining is because in this loop:
while (in.readLine()!=null)
{
text = in.readLine();
}
it's possible that the body of the loop will never get executed, which means it's possible that text will never be set to anything. And the Java compiler doesn't like it when you use a variable that may not have been set to anything.
But the whole loop is wrong anyway. You're only using one input string, so why is this a loop? Before we can help fix this problem, we need to know what you're trying to accomplish. And if you really do want a loop, it would be wrong to call in.readLine() twice as you have above, since that means it will read two lines each time through the loop.
Assuming you've properly imported all the java.io classes, here are the two problems causing compilation to fail:
String text;
This must be initialized to something. Such as
String text = null;
Possibly setting it in the while loop is not good enough.
The other problem is this variable, output doesn't exist anywhere, which is why this line won't compile:
System.out.println(output);
I think you mean for it to be text

Dead code in Java/Android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-??
would creating a dummy method solve this issue??
these statements basically set the variables to their type-based initial values..
Thanks in advance!
The code that you describe is not dead code.
Dead code is code that will never execute.
Here is an example:
private int secretSchmarr;
public boolean blammo()
{
boolean returnValue;
secretSchmarr = calculateSecretValue();
returnValue = useSecretValue(secretSchmarr);
secretSchmarr = 99; // this is not dead code.
return returnValue;
secretSchmarr = 98; // This is dead code because it can never execute.
}
I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.
Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.
I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF
if(Boolean.parseBoolean(fin.next()))
myCode();

What exactly is <code> in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Edit: Thanks for the quick answers. Please mark this question for deletion. It doesn't serve SO in any way.
I was reading an article here {Listing 2. Iterating a file
} and what stumped me was the literal <code> written in Java! - I've never seen that before - I now get it's meaning somewhat but has it been there since beginning or is it a new feature.
I am reproducing part of the code here..
return new Iterable<String>() {
public <code>Iterator</code><String> iterator() {
return new <code>Iterator</code><String>() {
public boolean hasNext() {
return line != null;
.....
It's an error in the HTML markup of the site. Rightclick page and view source. It's been marked up as <code>Iterator</code> instead of <code>Iterator</code>.
This has nothing to do with Java. You might contact the site/page author to have them to fix it.
It's probably a remnant of generating code using JavaDoc. The <code></code> tags are used to highlight Java code in JavaDoc, in which it generates a Courier New font.
Haha I think it's just HTML that's mistakenly shown as Java code, it's not part of the language. :)
I think that is a HTML formatting error. They are trying to use their custom code tag within another code tag.
That's not valid java. I think it's a typo.
After all the correct answers an alternative explanation:
It is to protect the code by making it uncompilable. That way no-one can steal it:)

Categories

Resources