System.out.println help

Hello, I am trying to get my encoder value to print to the net console.
The only line giving me an error is.

System.out.println(encoder1.Get());

the error console says System is not defined. Any help would be appreciated.

Try this instead:


printf("Encoder1=%d", encoder1.Get());

Judging by the use of a capital in Get(), and that you posted to the C++ forum, I’m assuming you’re using C++. System.out.println isn’t a thing in C, the equivalent is printf(). An example:


// Top of file
#include <stdio.h>
...
// Inside your function
printf("%d
", encoder1.Get());

The %d means we have an integer to print, and
means to add a newline afterwards. Look here for more info

One thing to note, however, is that excessive use of printf can sometimes flood the network and if this is being called every iteration, you’ll get a ton of text in netconsole. I prefer to use the DriverStationLCD class, which will put the text on the right of the Operation tab of the driver station.


void OperatorControl(void)
{
	DriverStationLCD *dslcd = DriverStationLCD::GetInstance();
	...
	while (IsOperatorControl())
	{
		dslcd->Clear();
		...
		dslcd->Printf(DriverStationLCD::kUser_Line1, 1, "Left Encoder: %d", encoder1->Get());
		...
		dslcd->UpdateLCD();
	}
}

kUser_Line1 tells it which line to go on, 1 is the column to start in (generally 1), and the rest of it is called just like the regular printf(). Make sure you include the Clear() at the top of your loop, otherwise you can get some weird stuff in the text box.

Thanks, so am i correct in understanding that println is a java command not c++ which uses printf?
I will try this thanks for the help.

It’s because printf doesn’t have a class to go along with it. In Java, everything must have a class (You essentially can’t call a function without a . before it), but in C++ classes are optional.

Actually, in most variants of C++ we’ve got things called “streams” that greatly enhance the readability and flexibility of code by use of the overloaded bitshift operator- for instance:


#include <iostream>

. . .

using namespace std;

. . .

cout << encoder1.Get() << endl;

Printf’s really a C thing, but it is available in C++ if one needs fancier formatting or if one is allergic to the standard library for whatever reason. Then again, I can’t speak for WindRiver/Diab- I still haven’t gotten anything to run on that thing. It appears that they do have the simulator in this year’s version (as opposed to last year where LabVIEW was the hands-down choice because of my team’s large amount of code needing debugging and relative lack of robot), yet the installation of the C++ update seems to have failed probably due to the whole 32/64 bit thing. . .

it does work in windriver if you use cerr instead of cout (or if you reasign the streams)

EDIT: the C++ streams, that is