Showing posts with label Java example. Show all posts
Showing posts with label Java example. Show all posts

How to find largest word in a given string using Java with O(n)

How to find largest word in a given string using Java with O(n)

package com.onlinecodegeek.java;

public class FindLargetWord {

public static void main(String args[]) {
String s = " Welcome to Java World";
String[] wordsArray = s.split(" ");
int maxsize = 0;
String maxWord = "";
for (int i = 0; i < wordsArray.length; i++) {
if (wordsArray[i].length() > maxsize) {
maxWord = wordsArray[i];
maxsize = wordsArray[i].length();
}

}
System.out.println("Max sized word is " + maxWord + " with size " + maxsize);
}
}

 

Thulasiram

How to find a line with Maximum length from a File contains data

How to find a line with Maximum length from a File contains data.
SOURCE : MaxLineWordFromFile.java
INPUT

I have stored a file at below location
C:\\new\\testing.txt
testing.txt file contains :---
this is my india and all indian are my ,,,,
all indians are like brothers and sisters and so we can say this our
hello here iamhey be aware of dogs and human
hello
you see!!! what exactly you are talking about



OUTPUT

longest line : all indians are like brothers and sisters and so we can say this our
14







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




Inheritance Access using Example in java

Inheritance Access using Example in java
SOURCE : InhertanceAccess.java






public class InhertanceAccess {

public void eat() {
System.out.println("inside eat");
}

static class B extends InhertanceAccess {
public void eat() {
System.out.println("inside B");

}
public static void main(String[] args) {

InhertanceAccess hs = new B();
hs.eat();

}
}
}


INPUT




OUTPUT

inside B







Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




sum of consecutive integer is equal to target is index

sum of consecutive integer is equal to target is index
SOURCE : IndexPrintArray.java




public class IndexPrintArray {

public void twoSum(int[] numbers, int target) {

int sum=0;
int [] m = new int[numbers.length];
for(int i =0;i
m[i]=numbers[i];
}
int [] twoindex=new int[2];

for(int i =0;i
sum=0;
sum+=m[i];
if((sum+m[i+1])!=target){
continue;

}else{

twoindex[0]=i+1;
twoindex[1]=i+2;
System.out.println("sum of consecutive integer is equall to target is index "+ " " +twoindex[0] +" "+ "to ");
System.out.println("index"+ " " +twoindex[1]);
break;

}


}


}



public static void main(String[] args) {

int[] numbers = {23,65,6,3,8,7};
int target=9;

IndexPrintArray in = new IndexPrintArray();
in.twoSum(numbers,target);
// System.out.println(twoindex[0]);



}

}



INPUT

int[] numbers = {23,65,6,3,8,7}
int target=9



OUTPUT

sum of consecutive integer is equal to target is index 3 to index 4








Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+