[Solved] How to take input in java like this way? [closed]

Here’s one method, taking the user’s input one line at a time: import java.util.Scanner; class Matrix{ public static void main(String[] args){ char[][] matrix = new char[5][5]; Scanner in = new Scanner(System.in); System.out.println(“\nInput 5×5 char matrix one line at a time:\n”); for(int i = 0; i < matrix.length; i++){ String row = in.nextLine(); for(int j=0;j<matrix[i].length;j++){ matrix[i][j]=row.charAt(j); … Read more

[Solved] How to load a dictionary on android app start and use it from different activities

create application class and write this code in application class public class MyApplictaion extends Application { private static MyApplication myApplication = null; public Map<String, String> fontDic; @Override public void onCreate() { super.onCreate(); fontDic = new HashMap<String, String>(); fontDic.put(“1”, “0x0627”); fontDic.put(“2”, “0x0628”); fontDic.put(“3”, “0x062A”); fontDic.put(“4”, “0x062B”); } public static MyApplication getInstance() { if (myApplication == null) … Read more

[Solved] Where is the getInputStream() function in java 1.8?

Well…no, there wouldn’t be, ServerSockets don’t have streamed input/output. A ServerSocket accepts connections, creating a Socket for each connection received (see accept), which is where the streams for that connection are. solved Where is the getInputStream() function in java 1.8?

[Solved] How to transform a JsonArray to JsonObject? [closed]

//Json object String json = ‘{“myid”:”123″,”post”:”harry”}’; JSONObject jobj = new JSONObject(json); String id = jobj.getString(“myid”); //Json Array String json = ‘[{“myid”:”123″,”post”:”harry”},{“myid”:”456″:”ramon”}]’; JSONArray jarr = new JSONArray(json); JSONObject jobj = jarr.getJSONObject(0); String id = jobj.getString(“myid”); You will have to wrap it in a try catch to make sure to catch exceptions like json strings that cant … Read more

[Solved] Cannot access an Object Reference instantiated by a sibling class

So, blah defines an instance field called label but does not initialise it public abstract class Blah { protected JLabel label; } BlahChildOne initialises the label from Blah public class BlahChildOne extends Blah { public BlahChildOne() { label = new JLabel(); } } BlahChildTwo does not initialises the label from Blah, so it is still … Read more

[Solved] i want to search for “MAN-xxxx-xxxxx” in a text file using regular expression.Can anyone help me this? [closed]

please find below working code import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String line = “This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK?This cat MAN-1243-23445 placed OK? This cat MAN-1243-23445 placed OK?”; String pattern = “(MAN-\\d{4}-\\d{5})”; … Read more

[Solved] Print pyramid pattern in java [duplicate]

Try this: public static void main(String[] args) { for(int i=0;i<5;i++) { for(int j=0;j<5-i;j++) { System.out.print(” “); } for(int k=0;k<=i;k++) { System.out.print(“* “); } System.out.println(); } } Output: * * * * * * * * * * * * * * * Just for your knowledge System.out.println will print on a new line where System.out.print … Read more

[Solved] Java sync class and this by two threads

In your example, All MyThread instances created with id of 1 will synchronize on the MyThread class object. This means that no two MyThread instances can be “in” the first synchonized block at the same time. All MyThread instances created with id of 2 will synchronize on this. Now this is the thread object itself1. … Read more

[Solved] employee’s job to calculate salary using java

Try if (kindOfEmployee == 2) { System.out.println(“Overtime Rate:”); overtimeRate = input.nextInt(); System.out.println(“Overtime Hours:”); overtimeHours = input.nextInt(); overtimePay = overtimeRate*overtimeHours; } 0 solved employee’s job to calculate salary using java

[Solved] Removing duplicates from arraylist using set

Find the intersection Find the union Subtract the intersection from the union Code: public static void main(String[] args) { Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5)); Set<Integer> set2 = new HashSet<Integer>(Arrays.asList(1, 3, 6, 7)); Set<Integer> intersection = new HashSet<Integer>(set1); intersection.retainAll(set2); // set1 is now the union of set1 and set2 set1.addAll(set2); // set1 … Read more

[Solved] Time shift in time format as input, the shifted real time as output [closed]

You will probably want some thing like the Calendar for the time addition. Example: Calendar future = Calendar.getInstance(); future.add(Calendar.HOUR_OF_DAY, enteredHour); future.add(Calendar.MINUTE, enteredMinute); future.add(Calendar.SECOND, enteredSecond); //And so on… //Now the calendar instance ‘future’ holds the current time plus the added values. As for entering the time, one possibility is to enter it as a text, and … Read more

[Solved] Simple Chessboard in java

The idea is to make your black and white fields dependent of both of your for-loop counters. for(int i =0; i < n; i++) { for(int j = 0; j < n; j++) { if( (i+j) % 2 == 0){ System.out.print(“*”); } else { System.out.print(” “); } } System.out.println(“”); } or switch the ” ” … Read more

[Solved] How to implement 2 level sort in a csv file using Java [closed]

When it’s only a small file you could solve it like this read all lines into a list implement your own Comparator sort the list using your own comparator The example is only a PoC. Anything not necessary to show the principle has been omitted. import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; … Read more