|
I tried that thing earlier but as I declared array as..
int[] value = {2,1,3,6,5}
So this line
Arrays.sort(value, Collections.reverseOrder()); giving error, red line under value, I don't understand why..
But now I declare array as ..
Integer[] value = {2,1,3,6,5}
It works....
package SortListOfNumbers;
import java.util.Arrays;
import java.util.Collections;
public class Main{
public static void main(String[] args)
{
Sorting srt = new Sorting();
srt.ascending();
srt.descending();
}
}
class Sorting{
public void ascending(){
int[] value = {2,1,3,6,5};
System.out.println("Sorting in ascending order");
System.out.println("Unsorted value");
for (int s : value)
System.out.print(s + " ");
System.out.println("\n");
Arrays.sort(value);
System.out.println("sorted value ascending");
for (int s : value)
System.out.print(s + " ");
System.out.println("\n");
}
public void descending(){
Integer[] value = {2,1,3,6,5};
System.out.println("Sorting in descending order");
System.out.println("Unsorted value");
for (int s : value)
System.out.print(s + " ");
System.out.println("\n");
Arrays.sort(value, Collections.reverseOrder());
System.out.println("Sorted Descending");
for (int s : value)
System.out.print(s + " ");
System.out.println("\n");
}}
Please explain why is it happen so.. it bit strange for me.
and thanks a lot.
|
|
|
|
|
nt_virus wrote: Please explain why is it happen so.. it bit strange for me.
Probably because int is a simple type, but Integer is a class. I would suggest reading the Java documentation for a full description of the differences and what methods and interfaces they support.
|
|
|
|
|
I looked at your code, you need to read the error message which says The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Comparator < Object > ) so when you added the Integer you are solving the problem that was generated because Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
|
|
|
|
|
See further information below from Member 4277480.
|
|
|
|
|
Thanks a lot a lot to both of you guys.. You made my day..
|
|
|
|
|
* Hi everybody !
*
* Im using PDFBox to convert pdf to xhtml.
*
* Can anybody help me.
*
* In PDFBox source, there are no class pdf2xhtml but i found a class at
* http://lucene.apache.org/tika/xref/org/apache/tika/parser/pdf/PDF2XHTML.html
*
* How can i use it.
*
* Thanks and regards
|
|
|
|
|
We can generally help here with standard code, but when using third party components it is better to find if they have a forum and ask there.
This is the second time in a week when you have asked a question like this. It was kindly sugested to you to ask the people who wrote the component. What's different today?
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
The users of this lib seem satisfied PDFToHTML[^]
Good Luck
|
|
|
|
|
Hello
i 'd like to know whether i can use .NET Framework inside Java or use .NET Framework classes and methodes in Java
Many Thanks
Thierry
|
|
|
|
|
Aside from J# I do not know of any interoperable software to do so. However, I use both in almost all the code I write. For example, If I want a feature that is not available in Java by default and is found in C# then I implement it using C# and make an executable and call it from Java. The calling varies depending on what I want some times I capture the output from the executable and process it in Java and other times I just let it do all the work.
|
|
|
|
|
|
You could convert the arrays to a list using Arrays.toList, then use Collections.indexOfSubList.
Or, if you want to do it yourself without converting the arrays to lists, take a look at the source for Collections.indexOfSublist to see how that does it, then implement your own version that operates directly in the arrays.
|
|
|
|
|
|
Snippet Example:
List<Integer> list = Arrays.asList(1,2,3,4,5);
System.out.println("List :"+list);
List<Integer> sublist = Arrays.asList(4,5);
System.out.println("SubList :"+sublist);
System.out.println("indexOfSubList: "+ Collections.indexOfSubList(list, sublist));
System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
if (Collections.indexOfSubList(list, sublist) != -1)
System.out.println(true);
else
System.out.println(false);
|
|
|
|
|
In your code you exit as soon as a mismatch is found rather then continuing to look in case the match occurs later.
Try this [untested]
static boolean hasSubsequence(int[] arr, int[] sub)
{
boolean matched = false;
for (int i=0;
i < arr.length-sub.length && !matched;
i++)
{
matched = true;
for (int j=0;
j <= sub.length && matched;
j++)
{
matched = (arr[i] == sub[j])
}
}
return matched;
}
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
|
try without the mistakes :
<pre> static boolean hasSubsequence(int[] arr, int[] sub) {
// no match for the outer loop
// this will exit when a match is found
boolean matched = false;
for (int i = 0; i <= arr.length - sub.length && !matched; i++) {
// start off matched for the inner loop
// this will exit when a mismatch is found
matched = true;
for (int j = 0; j < sub.length && matched; j++) {
matched = (arr[i+j] == sub[j]);
}
}
// at this point mayched is true if sub is inside arr
return matched;
}</pre>
Exercise for you is to work out WHY it was wrong....
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Hey, I was wondering if there are any jobs over the internet tht involve me taking part in a project that I don't get paid for, for example an unpaid position that I could simply be given a task and then log off the internet and complete the task. If you would like more information just ask...
- Todd.
|
|
|
|
|
|
I enjoy answering Java board questions
|
|
|
|
|
Member 4277480 wrote: I enjoy answering Java board questions
I've noticed that, you always offer very comprehensive answers. However, sometimes I think it does more good to get people to at least try and figure things out for themselves, so they may actually learn something.
|
|
|
|
|
True, but I always remember back in the days when I had many courses and used to be stuck on one program trying to figure out what the error is and try to solve it, that took some time which sometimes due to the exciting nature of programming made me give that program more time than the other courses. This way the guys and gals who ask can know what to do and next time they code they can use the answers as reference. I always say this Air,Food,Water, and Google are absolute necessities in life.
|
|
|
|
|
Hi can some kind person help with this JAVA Q on parsing text files:
INPUT TEXT (FASTA) FILE:
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485993 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485994 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485922 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485912 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485942 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
I need JAVA Code that parses the input file and generates 6 files each file contains file is labelled after the '>' and before the space ie
file 1 will be named AB485992 and that file will contain the text between the '>' and the following '>' ie:
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
file 2 will be labelled: AB485993 and it contents
>AB485993 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
file 3 will be labelled: AB485993 and it contents
>AB485994 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
and so on for all six files
So far I have code that parses out the word after '>' ie the file label and prints those to args file like below, can some kind person PLEASE show me how to parse the INPUT file and generate the 6 seperate files as described above each file labeled after the '>'
eg file1, labelled AB485992 and so on for the 6 files
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
THANKS SO MUCH!
import java.io.*;
import java.util.Scanner;
public final class ReadWithScanner {
public static void main(String... aArgs) throws FileNotFoundException {
File f = new File("args");
f.delete();
ReadWithScanner parser = new ReadWithScanner("file.text");
parser.processLineByLine();
log("Done.");
}
public ReadWithScanner(String aFileName){
fFile = new File(aFileName);
}
public final void processLineByLine() throws FileNotFoundException {
Scanner scanner = new Scanner(fFile);
try {
while ( scanner.hasNextLine() ){
processLine( scanner.nextLine() );
}
}
finally {
scanner.close();
}
}
protected void processLine(String aLine){
Scanner scanner = new Scanner(aLine);
scanner.useDelimiter(" ");
String[] fields = aLine.split(" ");
String start = ">";
if ( scanner.hasNext() ){
String name = scanner.next();
if (name.startsWith(start)){
String valueNeeded = fields[0];
String d = valueNeeded.substring(1,valueNeeded.length());
writeToLog(d);
}
}
else {
log("Empty or invalid line. Unable to process.");
}
scanner.close();
}
private void writeToLog (String fileName) {
File f = new File("args");
FileOutputStream out;
PrintStream p;
try {
out = new FileOutputStream(f, true);
p = new PrintStream( out );
p.println(fileName);
System.out.println(fileName);
}
catch (Exception e) {
System.err.println ("Error writing to file");
}
}
// PRIVATE /
private final File fFile;
private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}
private String quote(String aText){
String QUOTE = "'";
return QUOTE + aText + QUOTE;
}
}
|
|
|
|
|
I took it one step further.
import java.io.*;
import java.util.*;
public class SplitFiles {
public static void main(String[] args) throws Exception {
File mainfile = new File("MainFile.txt");
Scanner scan = new Scanner(mainfile);
ArrayList<String> List = new ArrayList<String>();
String getFileName = "";
while (scan.hasNext()) {
List.add(scan.nextLine());
}
for (int i = 0; i < List.size(); i++) {
if (List.get(i).charAt(0) == '>') {
getFileName = List.get(i).replace(">", "");
String result = "";
for (int j = 0; j < getFileName.length(); j++) {
if (getFileName.charAt(j) == ' ') {
result = getFileName.substring(0, j);
break;
}
}
File output;
if (new File(result + ".txt").exists()) {
BufferedWriter out = new BufferedWriter(new FileWriter(
result + ".txt", true));
out.write(List.get(i) + "\n");
int k;
for (k = i + 1; k < List.size(); k++) {
if (List.get(k).charAt(0) == '>')
break;
else {
out.write(List.get(k) + "\n");
}
}
out.close();
} else {
output = new File(result + ".txt");
PrintWriter out = new PrintWriter(output);
out.println(List.get(i));
int k;
for (k = i + 1; k < List.size(); k++) {
if (List.get(k).charAt(0) == '>')
break;
else {
out.println(List.get(k));
}
}
out.close();
}
}
}
}
}
Good Luck
|
|
|
|
|
Thanks so much for your help!
Just one small point the code you wrote here "for (k = i+1; k " is missing the end part any chances you know what the ending should be?
Thanks again!
|
|
|
|