String index out of bounds exception java

You are calling str.substring(i, j-i) which means substring(beginIndex, endIndex), not substring(beginIndex, lengthOfNewString). One of assumption of this method is that endIndex is greater or equal beginIndex, if not length of new index will be negative and its value will be thrown in StringIndexOutOfBoundsException. Maybe you should change your method do something like str.substring(i, j)? Also if size is length of your str then for (int i = 0; … Read more

What is inverse function to XOR?

The inverse is XOR! If you have: You can get a or b back if you have the other value available: a = c^b; // or b^c (order is not important) b = c^a; // or a^c For example if a = 5, b = 3 (and thus c = 6 as you mentioned) you get: b=0011 (3) a=0101 (5) c=0110 (6) XOR or … Read more

What is java interface equivalent in Ruby?

Ruby has Interfaces just like any other language. Note that you have to be careful not to conflate the concept of the Interface, which is an abstract specification of the responsibilities, guarantees and protocols of a unit with the concept of the interface which is a keyword in the Java, C# and VB.NET programming languages. In Ruby, we use the … Read more

Wrong project is being run in Eclipse

Trying to guess your problem: When you press the Run as button (White arrow in green circle), Eclipse doesn’t run the program you’re editing. Instead, it runs again the last program you executed. That’s the reason why you see the output of another project: You’re telling Eclipse to repeat its execution. So, to run your new app, … Read more

How to write logs in text file when using java.util.logging.Logger

Try this sample. It works for me. public static void main(String[] args) { Logger logger = Logger.getLogger(“MyLog”); FileHandler fh; try { // This block configure the logger with handler and formatter fh = new FileHandler(“C:/temp/test/MyLogFile.log”); logger.addHandler(fh); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); // the following statement is used to log any messages logger.info(“My first log”); … Read more

How can I set size of a button?

The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible JFrame frame = new JFrame(“test”); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(4,4,4,4)); for(int i=0 ; i<16 ; i++){ JButton btn = new JButton(String.valueOf(i)); btn.setPreferredSize(new Dimension(40, 40)); panel.add(btn); } … Read more