System.out.println() not working - java

this is my first post. I am excited to a part of this community and I have been struggling with this problem for a while, so here goes:
In the following code:
if (j == 0)
{
if (!Arrays.equals(cipherData, c))
{
System.out.print("C: ");
for (int i = 0; i < encryptedData.length; i++)
System.out.print((char)cipherData[i]);
System.out.println();
}
}
The System.out.println()
method returns nothing at all. No line, or anything and I have no idea why. The goal is to print a blank line after printing the byte array is printed above when those if conditions are true.
Any help would be much appreciated and welcome.

System.out.print() does not print a newline character.
You're outputting a bunch of stuff, then printing a newline with System.out.println(). This causes the cursor to drop to the next line.
You need another one if you want a blank line after that.
Edit to add: I missed the fact that your for loop conditional is ... different than the array you're printing. Did you mean for that to be the case?
Also, since you're possibly printing non-printable characters, it is completely plausible that you're causing the terminal to be in a state where the newline will no longer work.
What it comes down to is, println() isn't broken. Either it's not getting called, or if you don't see a newline occur when it is called then the terminal is in a state where it no longer recognizes it.

Check
http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println()
You may need System.out.print('\n');

Before iterating for loop you can check length of encryptedData
System.out.println("encryptedData.length:: "+ encryptedData.length);
if encryptedData.length return greater than 1 then it will go into for loop.
You should debug step by step .

I have concerns about this:
System.out.print((char)cipherData[i]);
Assuming that cipherData is an array of bytes, then casting a byte to a char and printing it via a character stream is not likely to give pretty results. For a start, bytes that are less that 32 decimal will map to ASCII "control characters".
And also you may be printing the wrong array ... or using the length of the wrong array.
(But the explanation for your problem is that you need to call println a second time to be a blank line. The first println is just terminating the line containing the ... umm ... "characters" from your cipher array.)

Sounds like the problem is that you have run a JVM without any standard output attached. Like on Windows, using javaw.exe to run a jar (which is the default, beware). Java.exe outputs to the console window, if you run it from a console, but javaw.exe does not. If you run the program from the file explorer window, even though you are using java.exe, you still won't get any standard out, because it's hidden by windows.
So, run the program in a command line window, and use java.exe, not javaw.exe.

Related

Is it possible to remove new lines from the console in Java?

So I have been trying out different ways to represent information in the console and I have noticed printing \b doesn't remove newlines in the console.
Here is an example:
System.out.println("ggg");
System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b");
shows up as ggg.
Is there a way to make this work?
"/b" just omit a character from the console; the problem is that in your code the "ggg" line will be printed and thew following "/b"s will be printed on a new line in the console, they can not affect the prior line.
You need to use "/b" on the same line.
Visit http://www.java2s.com/Code/Python/String/EscapeCodesbtnar.htm to grasp java escape codes.
\b doesn't remove new lines, depending on the OutputStream, it may remove a character. For example, try
System.out.println("ggg\b");

Getting multiple errors compiling Java code in Eclipse

I'm new to programming, and I'm using Eclipse to make some easy programs like calculators and vote counting. (Don't mind the language, it's Portuguese and i'm from Brazil.)
So as you can see in the image with "Questão1.java." Class opened compiles perfectly, and the "Questão2.java" shows quite a lot of errors, and I have absolutely no idea what it means.
This one is giving a lot of errors:
This one compiles perfectly, no errors and results as expected:
Thanks everyone for answering, i found out the error and it was indeed the "printf"...and also i'll remember to never post codes as images next time, again thank you guys.
The problem is that you use printf() instead of print() or println().
printf() adds formatting to whatever you are trying to print, and the String that determines how your output should be formatted uses characters like %, which you also use in the class that throws errors.
The error
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' '
at [and so on...]
at Questao2_Lista[bla bla bla].main(Questao2.java:18)
can be read in the following way:
The first line defines what kind of exception was thrown, in this instance it is an "UnknownFormatConversionException".
The lines below it are called a "stacktrace", that show where exactly the exception was thrown and the "way" it got propagated up the call-stack.
It looks like this is the only error, I can't see any of the other errors you talked about so I guess you just assumed that every line was a separate error?
The problem with "Questão2.java" is the System.out.printf statement. The statement has a '%' which is a special character. % is used as a preceding character for a place holder
Eg: %d in the string will be replaced with a number passed as an argument.
int lines =10;
System.out.printf ("There are %d lines", lines);
Will come as:
There are 10 lines
If its just a print statement Use System.out.println instead of System.out.printf
Try to use System.out.print() or System.out.println() instead System.out.printf() in line 18 of Questão2.java

'Communicate' in Python does not work

I'm trying to write a python program to test a java program that takes input from stdin using Scanner.
All other posts point to using communicate with popen, but for me it absolutely does not work. When i run my python program, it just calls popen and then stops while the java program waits for input. I wrote a print statement after popen to check. It never prints.
Its very simple. I just want to give this program that waits for input some input.
here is the code:
import os.path, subprocess
from subprocess import PIPE
p = subprocess.Popen(['java', 'Main'], stdin=PIPE, stdout=PIPE)
print 'after subprocess' #this never get's printed
output = p.communicate(input='5 5 4 3 2 1'.encode())[0]
print output
Without more information (like some sample Java code) it's hard to be sure, but I'll bet the problem is that the Java code is waiting for a complete line, and you haven't sent one.
If so, the fix is simple:
output = p.communicate(input='5 5 4 3 2 1\n'.encode())[0]
As a side note, why exactly are you calling encode on that string? It's already encoded in whatever character set your source code uses. So, when you call encode, it has to first decode that to Unicode. And then, because you didn't pass an argument to encode, it's going to encode it to your default character set (sys.getdefaultencoding()), which doesn't seem any more likely to match what the Java code is expecting than what you already have. It's rarely worth calling encode with an argument, and you should almost* never call it on a str, only a unicode.
* In case you're wondering, the exception is when you're using a handful of special codecs like hex or gzip. In Python 3, they decided that the occasional usefulness of those special cases was nowhere near as much as the frequent bug-magnet of calling encode on already-encoded strings, so they took it out of the language.

Carriage return Java

is there any way to print a line in java and then go back to the beginning of that line, i tried using \r however this only prints a new line and does not go back to the original line.
So basically if the user inputs "Hello this is lol"
I want to print all the a's in the sentence (none), all the b's, etc...
eg.)" e "
then"He " --> this however must be on the same line as above and you must be able to see the change.
Is there any way to do this in java?
I ran this on my Mac, which is a FreeBSD underneath:
public static void main (String[] args) {
System.out.println("Hello there\rWho's");
}
It printed out
Who's there
If you are running this on Windows, then I cannot be sure what they'll do, but all *nixes should behave as posted.
This is not really a java question, but has more to do with the behavior of your terminal/console.
You are sending the correct character to return to the beginning of the line '\r' but it sounds like your console is not handling this correctly.
You should also be using the print() and not println() function (or whatever the methods are called on the specific object you are using to write). The println() function will add a '\n' character which will cause a new line to appear.
It depends what you mean by "print a line". If you're talking about the command line of an OS window, then you're going to nee to about output terminal control characters to control the cursor.
It depends very much on which operating system you are using, but there are libraries output that can make it simpler.
You might be able to output the backspace character \b to erase the current line, but it may not work.

Scanner cuts off my String after about 2400 characters

I've got some very basic code like
while (scan.hasNextLine())
{
String temp = scan.nextLine();
System.out.println(temp);
}
where scan is a Scanner over a file.
However, on one particular line, which is about 6k chars long, temp cuts out after something like 2470 characters. There's nothing special about when it cuts out; it's in the middle of the word "Australia." If I delete characters from the line, the place where it cuts out changes; e.g. if I delete characters 0-100 in the file then Scanner will get what was previously 100-2570.
I've used Scanner for larger strings before. Any idea what could be going wrong?
At a guess, you may have a rogue character at the cut-off point: look at the file in a hex editor instead of just a text editor. Perhaps there's an embedded null character, or possibly \r in the middle of the string? It seems unlikely to me that Scanner.nextLine() would just chop it arbitrarily.
As another thought, are you 100% sure that it's not all there? Perhaps System.out.println is chopping the string - again due to some "odd" character embedded in it? What happens if you print temp.length()?
EDIT: I'd misinterpreted the bit about what happens if you cut out some characters. Sorry about that. A few other things to check:
If you read the lines with BufferedReader.readLine() instead of Scanner, does it get everything?
Are you specifying the right encoding? I can't see why this would show up in this particular way, but it's something to think about...
If you replace all the characters in the line with "A" (in the file) does that change anything?
If you add an extra line before this line (or remove a line before it) does that change anything?
Failing all of this, I'd just debug into Scanner.nextLine() - one of the nice things about Java is that you can debug into the standard libraries.

Categories