Sorting text from text File - java

package individual;
import java.io.*;
import java.util.*;
public class Individual{
public static void getContent(Scanner inp,String[] contents){
int i;
for(i=0;i<contents.length;i++)
contents[i]=inp.next();
public static void Sort(String[] contents){
int i,j;
int min;
String temp;
//for selection sort
for(i=0;i<contents.length-1;i++)
{
min=i;
for(j=i+1;j<contents.length;j++)
if(contents[j].compareTo(contents[min])<0)
min=j;
temp=contents[i];
contents[i]=contents[min];
contents[min]=temp;
}
}
public static void main(String[] args) throws FileNotFoundException{
String[] newAyat=new String[50];
//read From file that i save in drive
Scanner inFile=new Scanner(new FileReader("D:\\newFile.txt"));
getContent(inFile,newAyat);
Sort(newAyat);
}
}
}
so when i run the program i got an error. The output is not what i want. Is there any problems with my codes. And how to print out the sorting results?
This is the question that I try to solve:
Based on the list relates , you must program can display a list of words beginning with ' a ' , ' b ' , ' c ' and so on .

package textconcordance;
import java.util.*;
import java.io.*;
public class TextConcordance {
final static int SEN_LONG=8;
public static void getAyat(Scanner inp,String[] Sentence){
int i;
while(inp.hasNext()){
for(i=0;i<Sentence.length;i++)
Sentence[i]=inp.next();
}
}
//sort sentence
public static void sortSen(String[] Sentence){
int i,j;
int min;
String temp;
for(i=0;i<Sentence.length-1;i++)
{
min=i;
for(j=i+1;j<Sentence.length;j++)
if(Sentence[j].compareTo(Sentence[min])<0)
min=j;
temp=Sentence[i];
Sentence[i]=Sentence[min];
Sentence[min]=temp;
}
}
//for print
public static void print(String[] Sentence){
for(int i=0;i<Sentence.length;i++)
System.out.print(Sentence[i]+",");
}
public static void main(String[] args) throws FileNotFoundException{
String[] Sentence=new String[SEN_LONG];
Scanner inFile=new Scanner(new FileReader("D://Ghost.txt"));
getAyat(inFile,Sentence);
sortSen(Sentence);
inFile=null;
inFile=new Scanner(new FileReader("D://Story.txt"));
print(Sentence);
}
}

Related

startElement and endElement variable are holding 0 and 50 regardless of the input given to them in derived classes

At where I'm missing the link to parent and children class as the startElement and endElement of parent class variables are holding( 0 and 50 )respectively in both the ChildOne and ChildTwo classes regardless of input given to them.
eg: when startElement=20,endElement=100 the filter methods in both classes should return the values in the interval[20,100] but getting values in range [0,50]
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scan = new Scanner(System.in);
int startElement = scan.nextInt();
int endElement=scan.nextInt();
Parent a;
a = new ChildOne();
System.out.println(a.filter(startElement,endElement));
a = new ChildTwo();
System.out.println(a.filter(startElement,endElement));
}
}
class Parent {
public int startElement;
public int endElement;
public String filter(int startElement,int endElement)
{
return null;
}
Parent(int startElement,int endElement){
this.startElement=startElement;
this.endElement=endElement;
}
}
class ChildOne extends Parent{
public String filter(int StartElement,int endElement){
ArrayList<Integer> arr= new ArrayList<Integer>();
int flag=0;
System.out.println("input "+startElement +endElement);
for(int i=startElement;i<=endElement;i++){
if (i == 1 || i == 0){
continue;
}
flag = 1;
for(int j=2;j<=i/2;j++){
if(i%j==0){
flag=0;
break;
}
}
if(flag==1){
arr.add(i);
}
}
String str="";
for(Integer value:arr){
String s=String.valueOf(value);
str+=s+' ';
}
return str;
}
}
class ChildTwo extends Parent {
public String filter(int StartElement,int endElement)
{
ArrayList<Integer> arr= new ArrayList<Integer>();
System.out.println("input "+startElement +" "+endElement);
for(int i=StartElement;i<=endElement;i++)
{
int num=i,sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1){
arr.add(i);
}
}
String str="";
for(Integer value:arr){
String s=String.valueOf(value);
str+=s+' ';
}
return str;
}
}

what is the prolem in my code? it not compiled and get error.Please help me

This is the program for print the character of given index.But is gets error in function.
I give the function return type as char.But compiler told to rename the return type of main function
import java.util.Scanner;
class Demo5{
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
public char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
}
}
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
}
public static char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}

i have problem understanding difference between two solutions

why should i use two swap functions in second and i only one swap function in first
//first one am getting correct answer with only one swap
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
perm(s,0,s.length());
}
static void perm(String s,int left,int right)
{
if(left==right)
{
System.out.print(s+" ");
}
for(int i=left;i<right;i++)
{
s=swap(s,i,left,1);
perm(s,left+1,right);
}
}
static String swap(String s,int i,int j)
{
char ch[]=s.toCharArray();
char temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
s=new String(ch);
return s;
}
}
//in the second code am getting correct answer with two swap functions
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int nums[]={1,2,3};
perm(nums,0,nums.length);
}
static void perm(int[] nums,int left,int right)
{
for(int k=0;k<nums.length;k++)
{
System.out.print(nums[k]);
}
System.out.print(" ");
if(left==right)
{
ArrayList<Integer> l1=new ArrayList<Integer>();
for(int i=0;i<nums.length;i++)
{
//l1.add(nums[i]);
}
//l.add(l1);
}
for(int i=left;i<right;i++)
{
nums=swap(nums,i,left);
perm(nums,left+1,right);
nums=swap(nums,i,left);
}
}
static int[] swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
for(int k=0;k<nums.length;k++)
{
nums[k]=nums[k];
}
return nums;
}
}

Have string returned from one method print out in another method, in Java?

I have the following code, which I put together:
import java.io.*;
import java.util.Scanner;
public class StringReverser {
public static void main(String args[]) {
System.out.println("Please enter some text to reverse.");
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
reverse(userInput);
}
public static String reverse(String userInput) {
int i = 0;
while (userInput.length() <= 0) {
return userInput;
}
String reversed = reverse(userInput.substring(1)) + userInput.charAt(0);
return reversed;
}
public static void displayData(String reversed) {
System.out.println(reversed);
}
}
I want the returned reversed string from the reverse method to be printed in the displayData method.
How do I get it to print from there?
Can be some thing as below
public class StringReverser {
public static void main(String args[]) {
System.out.println("Please enter some text to reverse.");
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
displayData(reverse(userInput));
}
public static String reverse(String userInput) {
int i = 0;
while (userInput.length() <= 0) {
return userInput;
}
String reversed = reverse(userInput.substring(1)) + userInput.charAt(0);
return reversed;
}
public static void displayData(String reversed) {
System.out.println(reversed);
}
}
call reverse method from display data .
displayData(reverse(userInput))
or you can save the returned value in string variable like
String reverseString = reverse(userInput);
displayData(reverseString )
Try this:
displayData(reverse(userInput));

How to filter words in Java?

I want to check if there are 'bad' words in some cases such as checking IDs in register form. But I do not know how to check it.. The bottom code is what I got far with it.
String words = "admin,administrator,babo,sir,melon";
public boolean checkWord(String input) {
if(something here that i need to find??) return false;
else return true;
}
The pattern of words are divided in comma, and I really need help with it please!
The simplest thing would be to search for a word in a sorted array, like this:
private static String[] WORDS = new String[] {
"admin", "administrator", "babo", "melon", "sir"
};
public boolean checkWord(String input) {
return Arrays.binarySearch(WORDS, input) < 0; // Not found
}
Another example if you want to look for group of words inside your input
public class TestCheckWord {
static String words = "admin,administrator,babo,sir,melon";
public static void main (String args[]){
System.out.println(checkWord("Hello melon"));
System.out.println(checkWord("Hello sir"));
System.out.println(checkWord("Hello you"));
}
public static boolean checkWord(String input) {
String wordArray[] = words.split(",");
for(int i=0; i<wordArray.length; i++){
if(input.indexOf(wordArray[i])>-1)
return true;
}
return false;
}
}
and yet even another way to look for words only if your input contains only one word.(the order in the array doesn't matter in this case.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestCheckWord2 {
public static void main (String args[]){
System.out.println(checkWord("babo"));
System.out.println(checkWord("bobo"));
}
private static String[] WORDS = {"admin", "babo", "melon", "sir", "administrator"};
private static Set<String> mySet = new HashSet<String>(Arrays.asList(WORDS));
public static boolean checkWord(String input) {
return mySet.contains(input);
}
}
public class steve {
static boolean checkWord(String input, String words) {
if(words.contains(input)) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
String words = "admin,administrator,babo,sir,melon";
System.out.print(steve.checkWord("babo",words));
}
}

Categories

Resources