I was wondering if there is someway for me to set the color of the text that I output to the console in Java. It does not matter if it is system specific as the program will only be run on my Windows 7 x64 laptop.
This question: Change color in java eclipse console was asked several weeks ago and had a good solution(by #VonC) to a similar problem however it only addressed the issue inside eclipse.
Can the same effect be achieved if I execute my program from the command line? and if so how?
You can take a look at the Java Curses Library:
http://sourceforge.net/projects/javacurses/
Here's an entry on how to use it:
http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html
thy this....
furthermore read http://jansi.fusesource.org/
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
Another library you may be interested in is Jansi: http://jansi.fusesource.org/
Jansi interprets ANSI code and format them for the console output. It works for both unix and windows.
Update 11/2014: you can also see the github Page
Not directly related to Java console output, but if you're looking to use ANSI colors in Kotlin console output, this is a great library to use - https://github.com/importre/crayon
I needed colors for a project. Here is a class for everyone. Just do Colors.(color) + "whatever" to add color, bold, or italic. Use Colors.reset to reset the colors. Hope this helps.
package util;
public class Colors {
public static final String reset = "\u001B[0m";
public static final String bold = "\u001b[1m";
public static final String italic = "\u001b[3m";
public static final String underline = "\u001b[4m";
public static final String reversed = "\u001b[7m";
public static final String black = "\u001b[30m";
public static final String blue = "\u001b[34m";
public static final String cyan = "\u001b[36m";
public static final String green = "\u001b[32m";
public static final String magenta = "\u001b[35m";
public static final String red = "\u001b[31m";
public static final String white = "\u001b[37m";
public static final String yellow = "\u001b[33m";
public static final String brightBlack = "\u001b[30;1m";
public static final String brightBlue = "\u001b[34;1m";
public static final String brightCyan = "\u001b[36;1m";
public static final String brightGreen = "\u001b[32;1m";
public static final String brightMagenta = "\u001b[35;1m";
public static final String brightRed = "\u001b[31;1m";
public static final String brightWhite = "\u001b[37;1m";
public static final String brightYellow = "\u001b[33;1m";
public static final String bgBlack = "\u001b[40m";
public static final String bgBlue = "\u001b[44m";
public static final String bgCyan = "\u001b[46m";
public static final String bgGreen = "\u001b[42m";
public static final String bgMagenta = "\u001b[45m";
public static final String bgRed = "\u001b[41m";
public static final String bgWhite = "\u001b[47m";
public static final String bgYellow = "\u001b[43m";
public static final String bgBrightBlack = "\u001b[40;1m";
public static final String bgBrightBlue = "\u001b[44;1m";
public static final String bgBrightCyan = "\u001b[46;1m";
public static final String bgBrightGreen = "\u001b[42;1m";
public static final String bgBrightMagenta = "\u001b[45;1m";
public static final String bgBrightRed = "\u001b[41;1m";
public static final String bgBrightWhite = "\u001b[47;1m";
public static final String bgBrightYellow = "\u001b[43;1m";
}
Related
public class LengthConversion {
public static final String M_TO_CM = "meter to centimeter";
public static final String M_TO_MM = "meter to millimeter";
public static final String M_TO_DM = "meter to decimeter";
public static final String KM_TO_M = "kilometer to meter";
public static final String INCH_TO_M = "inch to meter";
public static final String FOOT_TO_M = "foot to meter";
public static final String ASM_TO_M = "angstrom to meter";
public static final String FM_TO_M = "fermi to meter";
public static final String MILE_TO_KM = "mile to kilometer";}
I am adding these string to the ArrayList one by one by .add() function. Is there another way?
Maybe you need an enum:
public enum LengthConversion {
M_TO_CM("meter to centimeter"),
M_TO_MM("meter to millimeter"),
M_TO_DM("meter to decimeter"),
KM_TO_M("kilometer to meter"),
INCH_TO_M("inch to meter"),
FOOT_TO_M("foot to meter"),
ASM_TO_M("angstrom to meter"),
FM_TO_M("fermi to meter"),
MILE_TO_KM("mile to kilometer");
public final String conversion;
LengthConversion(String conversion) {
this.conversion = conversion;
}
}
I have the following abstract class
public abstract class IStreamSorterTest<T> {
protected static String FOLDER_NAME = null;
protected static String FILE_EXT = null;
protected static String SCHEMA_FILE_EXT = null;
protected static final String PATH_PREFIX = "src/test/resources/" + FOLDER_NAME;
protected static final String USERS_FILE = "users" + FILE_EXT;
protected static final String SORTED_USERS_FILE = "sorted_users" + FILE_EXT;
protected static final String USERS_XML_SCHEMA = "users_schema" + SCHEMA_FILE_EXT;
Extended by several classes in which I would like to ONLY define the variables FOLDER_NAME, FILE_EXT and SCHEMA_FILE_EXT without creating new variables.
What is the best way to do it?
You can't make these variables static as they would only exist in the superclass.
You could pass these Strings to the abstract class' constructor:
public abstract class IStreamSorterTest<T> {
protected final String pathPrefix;
protected final String userFile;
protected final String sortedUserFile;
protected final String usersXMLSchema;
protected IStreamSorterTest(String folderName, String fileExt, String schemaFileExt) {
pathPrefix = "src/test/resources/" + folderName;
userFile = "users" + fileExt;
sortedUserFile = "sorted_users" + fileExt;
usersXMLSchema = "users_schema" + schemaFileExt;
}
}
Then in a subclass:
public class SomeClass extends IStreamSorterTest<SomeType> {
private static final String FOLDER_NAME = ...;
private static final String FILE_EXT = ...;
private static final String SCHEMA_FILE_EXT = ...;
public SomeClass() {
super(FOLDER_NAME, FILE_EXT, SCHEMA_FILE_EXT);
}
}
i was trying to print colored string in bluej using
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
but i was not getting the desired result
please help me get colored easily
Windows does not have the necessary ANSI color library that is needed to print colored text...You may have to search for an ANSI library.
My System runs XUBUNTU and the color code works perfectly.
Therefore the problem is with Windows.
How can I obtain information about device supported media formats?
I'm targeting everything from API 15 and up and I would like to know for example which profile of "h264/avc" device supports.
See these classes:
MimeTypeMap.java (frameworks\base\core\java\android
\webkit)
Here is ContentType.java (frameworks\base\core\java\com\google\android\mms) provides all format supported by android device.
package com.google.android.mms;
import java.util.ArrayList;
public class ContentType {
public static final String MMS_MESSAGE = "application/vnd.wap.mms-message";
// The phony content type for generic PDUs (e.g. ReadOrig.ind,
// Notification.ind, Delivery.ind).
public static final String MMS_GENERIC = "application/vnd.wap.mms-generic";
public static final String MULTIPART_MIXED = "application/vnd.wap.multipart.mixed";
public static final String MULTIPART_RELATED = "application/vnd.wap.multipart.related";
public static final String MULTIPART_ALTERNATIVE = "application/vnd.wap.multipart.alternative";
public static final String TEXT_PLAIN = "text/plain";
public static final String TEXT_HTML = "text/html";
public static final String TEXT_VCALENDAR = "text/x-vCalendar";
public static final String TEXT_VCARD = "text/x-vCard";
public static final String IMAGE_UNSPECIFIED = "image/*";
public static final String IMAGE_JPEG = "image/jpeg";
public static final String IMAGE_JPG = "image/jpg";
public static final String IMAGE_GIF = "image/gif";
public static final String IMAGE_WBMP = "image/vnd.wap.wbmp";
public static final String IMAGE_PNG = "image/png";
public static final String AUDIO_UNSPECIFIED = "audio/*";
public static final String AUDIO_AAC = "audio/aac";
public static final String AUDIO_AMR = "audio/amr";
public static final String AUDIO_IMELODY = "audio/imelody";
public static final String AUDIO_MID = "audio/mid";
public static final String AUDIO_MIDI = "audio/midi";
public static final String AUDIO_MP3 = "audio/mp3";
public static final String AUDIO_MPEG3 = "audio/mpeg3";
public static final String AUDIO_MPEG = "audio/mpeg";
public static final String AUDIO_MPG = "audio/mpg";
public static final String AUDIO_MP4 = "audio/mp4";
public static final String AUDIO_X_MID = "audio/x-mid";
public static final String AUDIO_X_MIDI = "audio/x-midi";
public static final String AUDIO_X_MP3 = "audio/x-mp3";
public static final String AUDIO_X_MPEG3 = "audio/x-mpeg3";
public static final String AUDIO_X_MPEG = "audio/x-mpeg";
public static final String AUDIO_X_MPG = "audio/x-mpg";
public static final String AUDIO_3GPP = "audio/3gpp";
public static final String AUDIO_OGG = "application/ogg";
public static final String VIDEO_UNSPECIFIED = "video/*";
public static final String VIDEO_3GPP = "video/3gpp";
public static final String VIDEO_3G2 = "video/3gpp2";
public static final String VIDEO_H263 = "video/h263";
public static final String VIDEO_MP4 = "video/mp4";
public static final String APP_SMIL = "application/smil";
public static final String APP_WAP_XHTML = "application/vnd.wap.xhtml+xml";
public static final String APP_XHTML = "application/xhtml+xml";
public static final String APP_DRM_CONTENT = "application/vnd.oma.drm.content";
public static final String APP_DRM_MESSAGE = "application/vnd.oma.drm.message";
private static final ArrayList<String> sSupportedContentTypes = new ArrayList<String>();
private static final ArrayList<String> sSupportedImageTypes = new ArrayList<String>();
private static final ArrayList<String> sSupportedAudioTypes = new ArrayList<String>();
private static final ArrayList<String> sSupportedVideoTypes = new ArrayList<String>();
static {
sSupportedContentTypes.add(TEXT_PLAIN);
sSupportedContentTypes.add(TEXT_HTML);
sSupportedContentTypes.add(TEXT_VCALENDAR);
sSupportedContentTypes.add(TEXT_VCARD);
sSupportedContentTypes.add(IMAGE_JPEG);
sSupportedContentTypes.add(IMAGE_GIF);
sSupportedContentTypes.add(IMAGE_WBMP);
sSupportedContentTypes.add(IMAGE_PNG);
sSupportedContentTypes.add(IMAGE_JPG);
//supportedContentTypes.add(IMAGE_SVG); not yet supported.
sSupportedContentTypes.add(AUDIO_AAC);
sSupportedContentTypes.add(AUDIO_AMR);
sSupportedContentTypes.add(AUDIO_IMELODY);
sSupportedContentTypes.add(AUDIO_MID);
sSupportedContentTypes.add(AUDIO_MIDI);
sSupportedContentTypes.add(AUDIO_MP3);
sSupportedContentTypes.add(AUDIO_MPEG3);
sSupportedContentTypes.add(AUDIO_MPEG);
sSupportedContentTypes.add(AUDIO_MPG);
sSupportedContentTypes.add(AUDIO_X_MID);
sSupportedContentTypes.add(AUDIO_X_MIDI);
sSupportedContentTypes.add(AUDIO_X_MP3);
sSupportedContentTypes.add(AUDIO_X_MPEG3);
sSupportedContentTypes.add(AUDIO_X_MPEG);
sSupportedContentTypes.add(AUDIO_X_MPG);
sSupportedContentTypes.add(AUDIO_3GPP);
sSupportedContentTypes.add(AUDIO_OGG);
sSupportedContentTypes.add(VIDEO_3GPP);
sSupportedContentTypes.add(VIDEO_3G2);
sSupportedContentTypes.add(VIDEO_H263);
sSupportedContentTypes.add(VIDEO_MP4);
sSupportedContentTypes.add(APP_SMIL);
sSupportedContentTypes.add(APP_WAP_XHTML);
sSupportedContentTypes.add(APP_XHTML);
sSupportedContentTypes.add(APP_DRM_CONTENT);
sSupportedContentTypes.add(APP_DRM_MESSAGE);
// add supported image types
sSupportedImageTypes.add(IMAGE_JPEG);
sSupportedImageTypes.add(IMAGE_GIF);
sSupportedImageTypes.add(IMAGE_WBMP);
sSupportedImageTypes.add(IMAGE_PNG);
sSupportedImageTypes.add(IMAGE_JPG);
// add supported audio types
sSupportedAudioTypes.add(AUDIO_AAC);
sSupportedAudioTypes.add(AUDIO_AMR);
sSupportedAudioTypes.add(AUDIO_IMELODY);
sSupportedAudioTypes.add(AUDIO_MID);
sSupportedAudioTypes.add(AUDIO_MIDI);
sSupportedAudioTypes.add(AUDIO_MP3);
sSupportedAudioTypes.add(AUDIO_MPEG3);
sSupportedAudioTypes.add(AUDIO_MPEG);
sSupportedAudioTypes.add(AUDIO_MPG);
sSupportedAudioTypes.add(AUDIO_MP4);
sSupportedAudioTypes.add(AUDIO_X_MID);
sSupportedAudioTypes.add(AUDIO_X_MIDI);
sSupportedAudioTypes.add(AUDIO_X_MP3);
sSupportedAudioTypes.add(AUDIO_X_MPEG3);
sSupportedAudioTypes.add(AUDIO_X_MPEG);
sSupportedAudioTypes.add(AUDIO_X_MPG);
sSupportedAudioTypes.add(AUDIO_3GPP);
sSupportedAudioTypes.add(AUDIO_OGG);
// add supported video types
sSupportedVideoTypes.add(VIDEO_3GPP);
sSupportedVideoTypes.add(VIDEO_3G2);
sSupportedVideoTypes.add(VIDEO_H263);
sSupportedVideoTypes.add(VIDEO_MP4);
}
// This class should never be instantiated.
private ContentType() {
}
public static boolean isSupportedType(String contentType) {
return (null != contentType) && sSupportedContentTypes.contains(contentType);
}
public static boolean isSupportedImageType(String contentType) {
return isImageType(contentType) && isSupportedType(contentType);
}
public static boolean isSupportedAudioType(String contentType) {
return isAudioType(contentType) && isSupportedType(contentType);
}
public static boolean isSupportedVideoType(String contentType) {
return isVideoType(contentType) && isSupportedType(contentType);
}
public static boolean isTextType(String contentType) {
return (null != contentType) && contentType.startsWith("text/");
}
public static boolean isImageType(String contentType) {
return (null != contentType) && contentType.startsWith("image/");
}
public static boolean isAudioType(String contentType) {
return (null != contentType) && contentType.startsWith("audio/");
}
public static boolean isVideoType(String contentType) {
return (null != contentType) && contentType.startsWith("video/");
}
public static boolean isDrmType(String contentType) {
return (null != contentType)
&& (contentType.equals(APP_DRM_CONTENT)
|| contentType.equals(APP_DRM_MESSAGE));
}
public static boolean isUnspecified(String contentType) {
return (null != contentType) && contentType.endsWith("*");
}
#SuppressWarnings("unchecked")
public static ArrayList<String> getImageTypes() {
return (ArrayList<String>) sSupportedImageTypes.clone();
}
#SuppressWarnings("unchecked")
public static ArrayList<String> getAudioTypes() {
return (ArrayList<String>) sSupportedAudioTypes.clone();
}
#SuppressWarnings("unchecked")
public static ArrayList<String> getVideoTypes() {
return (ArrayList<String>) sSupportedVideoTypes.clone();
}
#SuppressWarnings("unchecked")
public static ArrayList<String> getSupportedTypes() {
return (ArrayList<String>) sSupportedContentTypes.clone();
}
}
check related details here - http://developer.android.com/reference/android/media/package-summary.html
Hope this helps you.
I got this error when i try to run my code:
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at edu.ndhu.bm.healthow.HealThowConstant.<init>(HealThowConstant.java:91)
at edu.ndhu.bm.healthow.HealThow.main(HealThow.java:511)
i have searched for similiar answer but i still couldn't fix it. This must be something related to the conversion of data types but i do not know how to fix the problem.
I am new to Java programming so any help will be much appreciated. Thank you. and here's the code:
package edu.ndhu.bm.healthow;
import java.util.Properties;
public class HealThowConstant
{
public final int n;
public final int m;
public final int d;
public final int t;
public final double taux;
public final double etax;
public final double ax;
public final double bx;
public final double alphax;
public final double betax;
public final double qx;
public final double add_taux;
public final double tauUpperBoundx;
public final double lux;
public final double minlux;
public final double gux;
public final double mingux;
public final double tauy;
public final double etay;
public final double ay;
public final double by;
public final double alphay;
public final double betay;
public final double qy;
public final double add_tauy;
public final double tauUpperBoundy;
public final double luy;
public final double minluy;
public final double guy;
public final double minguy;
public final double tauw;
public final double etaw;
public final double aw;
public final double bw;
public final double alphaw;
public final double betaw;
public final double qw;
public final double add_tauw;
public final double tauUpperBoundw;
public final double luw;
public final double minluw;
public final double guw;
public final double minguw;
public final String nfetay;
public final int ant;
public final int iteration;
public final String parameter;
public final int fl;
public final int fu;
public final int pl;
public final int pu;
public final int cl;
public final int cu;
public final int rl;
public final int ru;
public final int Cl;
public final int Cu;
public final int ul;
public final int uu;
public final int al;
public final int au;
public final String scenario_generate;
public final int scenario;
public final int average;
public final int variance;
public final int stage;
public final double runtime;
public HealThowConstant(Properties properties)
{
n = Integer.parseInt(properties.getProperty("n"));
m = Integer.parseInt(properties.getProperty("m"));
d = Integer.parseInt(properties.getProperty("d"));
t = Integer.parseInt(properties.getProperty("t"));
taux = Double.parseDouble(properties.getProperty("taux"));
etax = Double.parseDouble(properties.getProperty("etax"));
ax = Double.parseDouble(properties.getProperty("ax"));
bx = Double.parseDouble(properties.getProperty("bx"));
alphax = Double.parseDouble(properties.getProperty("alphax"));
betax = Double.parseDouble(properties.getProperty("betax"));
qx = Double.parseDouble(properties.getProperty("qx"));
add_taux = Double.parseDouble(properties.getProperty("add_taux"));
tauUpperBoundx = Double.parseDouble(properties.getProperty("tauUpperBoundx"));
lux = Double.parseDouble(properties.getProperty("lux"));
minlux = Double.parseDouble(properties.getProperty("minlux"));
gux = Double.parseDouble(properties.getProperty("gux"));
mingux = Double.parseDouble(properties.getProperty("mingux"));
tauy = Double.parseDouble(properties.getProperty("tauy"));
etay = Double.parseDouble(properties.getProperty("etay"));
ay = Double.parseDouble(properties.getProperty("ay"));
by = Double.parseDouble(properties.getProperty("by"));
alphay = Double.parseDouble(properties.getProperty("alphay"));
betay = Double.parseDouble(properties.getProperty("betay"));
qy = Double.parseDouble(properties.getProperty("qy"));
add_tauy = Double.parseDouble(properties.getProperty("add_tauy"));
tauUpperBoundy = Double.parseDouble(properties.getProperty("tauUpperBoundy"));
luy = Double.parseDouble(properties.getProperty("luy"));
minluy = Double.parseDouble(properties.getProperty("minluy"));
guy = Double.parseDouble(properties.getProperty("guy"));
minguy = Double.parseDouble(properties.getProperty("minguy"));
tauw = Double.parseDouble(properties.getProperty("tauw"));
etaw = Double.parseDouble(properties.getProperty("etaw"));
aw = Double.parseDouble(properties.getProperty("aw"));
bw = Double.parseDouble(properties.getProperty("bw"));
alphaw = Double.parseDouble(properties.getProperty("alphaw"));
betaw = Double.parseDouble(properties.getProperty("betaw"));
qw = Double.parseDouble(properties.getProperty("qw"));
add_tauw = Double.parseDouble(properties.getProperty("add_tauw"));
tauUpperBoundw = Double.parseDouble(properties.getProperty("tauUpperBoundw"));
luw = Double.parseDouble(properties.getProperty("luw"));
minluw = Double.parseDouble(properties.getProperty("minluw"));
guw = Double.parseDouble(properties.getProperty("guw"));
minguw = Double.parseDouble(properties.getProperty("minguw"));
nfetay = properties.getProperty("nofactoryetay");
ant = Integer.parseInt(properties.getProperty("ant"));
iteration = Integer.parseInt(properties.getProperty("iteration"));
parameter = properties.getProperty("parameter");
fl = Integer.parseInt(properties.getProperty("fl"));
fu = Integer.parseInt(properties.getProperty("fu"));
pl = Integer.parseInt(properties.getProperty("pl"));
pu = Integer.parseInt(properties.getProperty("pu"));
rl = Integer.parseInt(properties.getProperty("rl"));
cl = Integer.parseInt(properties.getProperty("cl"));
cu = Integer.parseInt(properties.getProperty("cu"));
ru = Integer.parseInt(properties.getProperty("ru"));
Cl = Integer.parseInt(properties.getProperty("Cl"));
Cu = Integer.parseInt(properties.getProperty("Cu"));
ul = Integer.parseInt(properties.getProperty("ul"));
uu = Integer.parseInt(properties.getProperty("uu"));
al = Integer.parseInt(properties.getProperty("al"));
au = Integer.parseInt(properties.getProperty("au"));
scenario_generate = properties.getProperty("scenario_generate");
scenario = Integer.parseInt(properties.getProperty("scenario"));
average = Integer.parseInt(properties.getProperty("average"));
variance = Integer.parseInt(properties.getProperty("variance"));
stage = Integer.parseInt(properties.getProperty("stage"));
runtime = Double.parseDouble(properties.getProperty("runtime"));
}
}
This stack trace is saying that on line 91, you are using Integer.parseInt but you are passing in null into it. That is why this error is happening. You must pass in a String that represents an integer. You can't pass in null.
You either need to check for null and avoid passing it in, or make sure that line 91 always passes in non-null.
As Daniel Kaplan said, you are passing in a null value into Integer.parseInt which is causing the exception. The Javadoc for parseInt says that this will cause a NumberFormatException to be thrown:
Throws:
NumberFormatException - if the string does not contain a parsable integer
As you said in your comment, this is happening at t = Integer.parseInt(properties.getProperty("t")); The problem is that your properties variable is called and cannot find a property. Looking at the Javadoc for the java.util.Properties method public String getProperty(String key), emphasis mine:
Searches for the property with the specified key in this property
list. If the key is not found in this property list, the default
property list, and its defaults, recursively, are then checked. The
method returns null if the property is not found.
It seems like you want to avoid passing in a null to the Integer.parseInt method. You can do this is in different ways depending on how you want to model your class, but here are a few that you could do:
Do a null check before you do Integer.parseInt
Use the public String getProperty(String key, String defaultValue), that supplies a default if a property cannot be found. I.e., properties.getProperty("t", "15")