Posts

Showing posts with the label how to

Printing 1 to 100 without using any loop

Image
 Printing 1 to 100 without using any loop 1. Using Recursion : import java.util.*; class withoutLoop { static int i = 0; public static void main(String[] args) { if(i 2. Using Stream : import java.util.stream.IntStream; class withoutLoop { public static void main(String[] args) { IntStream.range(1,101).forEach(e->System.out.println(e)); } } 3. Another Method : Using Arrays.fill( ) method and Anonymous inner class import java.util.*; class withoutLoop { public static void main(String[] args) { Object[] obj = new Object[100]; Arrays.fill(obj,new Object(){ int count=0; @Override public String toString() { return Integer.toString(++count); } }); System.out.println(Arrays.toString(obj)); } } Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 3...

Round Upto 2 decimal places in java : using DecimalFormat

Image
 How to Round Number upto n decimal places in java. import java.util.*; import java.text.DecimalFormat; class MyDecimalFormater { static Scanner input = new Scanner(System.in); public static void main(String[] args) { try { double price = 1234567890.9876; // round up to 2 decimal places // fist method System.out.println("Using Math.round() : "+Math.round(price*100.0)/100.0); // second method System.out.println("By Using DecimalFormat :"); DecimalFormat f = new DecimalFormat("###.00"); System.out.println("round upto 2 places : "+f.format(price)); // exploring DecimalFormat DecimalFormat g = new DecimalFormat("$###.000"); System.out.println("round upto 3 places and using dollar symbol : "+g.format(price)); DecimalFormat h = new DecimalFormat("\u00A5###,###.000"); System.out...

Simple Socket Programming in java

Image
 In this Java network programming tutorial, you will learn how to develop a simple socket server program. Server.java package simple_tcp; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public Server() throws Exception { ServerSocket server_socket = new ServerSocket(2020); //opening a new port System.out.println("Port 2020 is open."); Socket socket = server_socket.accept(); System.out.println("Client " + socket.getInetAddress() + " has connected."); // I/O buffers: BufferedReader in_socket = new BufferedReader(new InputStreamReader (socket.getInputStream())); PrintWriter out_socket = new PrintWriter(new OutputStreamWriter (socket.getOutputStream()), true); out_socket.println("Welcome!...

Family Tree Project in Java

Image
Creating Family tree in java .  Trees are such an important structure in computer science. But what more important tree, than our family tree? ❤️   👨‍👩‍👧‍👦   ❤️    Made this simple program in java to implement family tree. Java File import java.util.*; class Person { String name; boolean married; String wife; int noOfChild; Person[] childs; public Person(String name){ this.name = name; } public Person(String name,boolean married,String wife,Person[] childs,int noOfChild) { this.name = name; this.married = married; this.wife = wife; this.childs = childs; this.noOfChild = noOfChild; } public Person(String name , boolean married,String wife) { this.name = name; this.married = married; this.wife = wife; } } class familyTree { static int population; static Scanner input = new Scanner...

How to take input from user in sublime 3 in JavaScript? [ SOLVED ]

Image
 [SOLVED] Take console (cmd) input in sublime 3 text editor in javascript Open Sublime  goto   >   Tools   >  Build System   >  Build New System       paste the following code in that file and save it as  anyName.sublime-build { "cmd":["start", "cmd", "/k" ,"node","$file_base_name"], "selector": "source.jW", "working_dir": "${file_path}", "shell": true } And now in your js file add these lines. Help from official website : click here const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }) readline.question(`What's your name?`, name => { console.log(`Hi ${name}!`) readline.close() })

How to find Max and Min value in array java.

Image
Finding min or max value in premative array import java.util.*; class cf { static Scanner input = new Scanner(System.in); public static void main(String[] args) { Integer[] arr = {2,3,1,5,3,61,12}; int max = Collections.max(Arrays.asList(arr)); int min = Collections.min(Arrays.asList(arr)); System.out.println(max); System.out.println(min); } } Output : 61 1

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Image
 Modifying variables inside java lambdas. Integer list[] = {1,2,3,4,5,6,7,8}; int index = 0; Arrays.asList(list).forEach(e -> { System.out.println(e+" at index "+index); index++; }); Error : maze2.java:22: error: local variables referenced from a lambda expression must be final or effectively final System.out.println(e+" at index "+index); ^ maze2.java:23: error: local variables referenced from a lambda expression must be final or effectively final index++; ^ Solution : Integer list[] = {1,2,3,4,5,6,7,8}; int[] index = {0}; Arrays.asList(list).forEach(e -> { System.out.println(e+" at index "+index[0]); index[0]++; }); Output : 1 at index 0 2 at index 1 3 at index 2 4 at index 3 5 at index 4 6 at index 5 7 at index 6 8 at index 7

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java