Programming Hub
Hello Everyone! I am a blogger and I will share the code of probably all programming languages like Java Programming, C Programming, Python Programming, HTML Code, CSS Code, MYSQL Database and also share about the SQL Queries.
Monday, October 25, 2021
String Array and passing method in java
Searching an Element in Array and Return true if found in java
In this task pass the Array to a method and search an Element in Array and Return true if found in java. we use the Scanner method and search the element in Array.
Code:
import java.util.Scanner;public class SearchingElement{public static void main(String[] args){Scanner input = new Scanner(System.in);int size;// Size of ArraySystem.out.print("Enter size of Array: ");size = input.nextInt();System.out.println("\n\n");//Declaring and creating an array of specified sizeint[] numbers = new int[size];//Take Array elements from userfor(int i=0; i<size; i++){System.out.print("Enter value for numbers[" + i + "] = ");numbers[i] = input.nextInt();System.out.println("\n\n");}//Value to be searchedSystem.out.print("Enter the Value you want to Search: ");int value = input.nextInt();// Calling method to find valueboolean found = findValue(numbers,value);if(found)System.out.print("\n " + value + " exists in the Array");elseSystem.out.println(value +" does not exist in Array");}//mainpublic static boolean findValue(int[] array, int value){//iterate over each element and print itfor(int i = 0; i < array.length; ++i){if(array[i] == value){return true;} }return false;}}//class
Code:
| Searching an Element in Array and Return true if found in java |
A program that determines whether an input number is an integer or a real number in java
write a program that determines whether an input number is an integer or a real number.
Three sample runs of this program are as follows:
Enter a number: 50
The number 50 is an integer.
Enter a number: 50.0
The number 50 is an integer.
Enter a number: 50.75
The number 50.75 is a real number.
HINTS: Use only ONE variable for the input. You will also need to separate the real part (after the decimal point) of the input and the integer part (before the decimal point). Think about how you will do this.
Code:
public class NumberTypeLab5{public static void main(String[] args) {int x =25;x = x*100;if(x/100==0){System.out.println("Number is not integer: " + x);}else{System.out.println("Number is integer: " + x);}}}
String Array and passing method in java
In this task, we pass a method and also cover the passing String Array to the method. Code: import java.util.Scanner; public class String...