|
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!
|
|
|
|
|
Yes that happens with < > in code, I corrected it.
|
|
|
|
|
Thanks so much!
I tried compiling and I am getting complaints as follows, any ideas? Sorry I am new to JAVA and cant figure out how to fix it.
thanks again!
SplitFiles.java:24: cannot find symbol
symbol : method charAt(int)
location: class java.lang.Object
if (List.get(i).charAt(0) == '>')
^
SplitFiles.java:27: cannot find symbol
symbol : method replace(java.lang.String,java.lang.String)
location: class java.lang.Object
getFileName = List.get(i).replace(">", "");
^
SplitFiles.java:52: cannot find symbol
symbol : method charAt(int)
location: class java.lang.Object
if (List.get(k).charAt(0) == '>')
^
SplitFiles.java:74: cannot find symbol
symbol : method charAt(int)
location: class java.lang.Object
if (List.get(k).charAt(0) == '>')
|
|
|
|
|
Steps:
1. Copy the code into your Java project.
2. Create a Text file called MainFile.txt containing what you wanted in the first post.
It should work since I corrected the < and > (HTML problem) in my answer post.
|
|
|
|
|
It works, thank you so much!
|
|
|
|
|
Requirements of the program:
1) Write a Java Application that uses repetition and switch statements to print the song "The Twelve Days of Christmas."
2) One switch statement should be used to print the day (i.e. "first", "second", etc.) A separate switch statement should be used to print the remainder of each verse.
3) Show the song in a dialog box with a test area and a scroll bar instead of in the console. Code for this requirement is given as follow:
[CODE]
JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane(songArea);
songArea.setText(songString);
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",
JOptionPane.PLAIN_MESSAGE);
[/code]
Here is what I have which doesn't meet all of the requirements
[CODE]
//Ravi Shah
//CIS 226
//Assignment5: 12 Days of Christmas
//10/08/09
public class ChristmasSong {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
String prize = "";
String day = "";
String song = "";
System.out.print("");
number = 12;
System.out.println();
for (int j = 1; j <= number; j++)
{
switch (j)
{
case 1:
day = "First";
prize = "A Partridge in a Pear Tree \n ";
break;
case 2:
day = "Second";
prize = "\nTwo turtle doves, \nAnd " + prize;
break;
case 3:
day = "Third";
prize = "\nThree French Hens," + prize;
break;
case 4:
day = "Four";
prize = "\nFour Calling Birds," + prize;
break;
case 5:
day = "Five";
prize = "\nFive Golden Rings," + prize;
break;
case 6:
day = "Six";
prize = "\nSix Geese a Laying," + prize;
break;
case 7:
day = "Seven";
prize = "\nSeven Swans a Swimming," + prize;
break;
case 8:
day = "Eight";
prize = "\nEight Maids a Milking," + prize;
break;
case 9:
day = "Nine";
prize = "\nNine Ladies Dancing," + prize;
break;
case 10:
day = "Ten";
prize = "\nTen Lords a Leaping," + prize;
break;
case 11:
day = "Eleven";
prize = "\nEleven Pipers Piping," + prize;
break;
case 12:
day = "Twelve";
prize = "\n12 Drummers Drumming," + prize;
break;
}
song +="\nOn the " + day + " day of Christmas \nmy true love sent to me: " + prize;
}
System.out.println(song);
}
}
[/code]
|
|
|
|
|
Have you given any thought of at least trying to do your own homework first?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
I did write that code myself. I followed the example of switch statement from the book which included "break". so i included break in my program too.
|
|
|
|
|
So what exactly is your question?
|
|
|
|
|
This is what I understood from the question:
import javax.swing.*;
public class TwelveDays
{
public static void main(String[] args)
{
for (int i = 0; i < 12; i++)
{
int day = Integer.parseInt(JOptionPane.showInputDialog("What Day ?"));
switch(day)
{
case 1:
showIT(day);
break;
case 2:
showIT(day);
break;
case 3:
showIT(day);
break;
case 4:
showIT(day);
break;
case 5:
showIT(day);
break;
case 6:
showIT(day);
break;
case 7:
showIT(day);
break;
case 8:
showIT(day);
break;
case 9:
showIT(day);
break;
case 10:
showIT(day);
break;
case 11:
showIT(day);
break;
case 12:
showIT(day);
break;
default:
JOptionPane.showMessageDialog(null,"Can't you count", "Ha Ha Ha",JOptionPane.WARNING_MESSAGE);
break;
}
}
}
public static String RemainingSong(int day)
{
String song = "";
String complete [] = {"Verse 1 \n","Verse 2 \n","Verse 3 \n","Verse 4 \n","Verse 5 \n","Verse 6 \n","Verse 7 \n","Verse 8 \n","Verse 9 \n","Verse 10 \n","Verse 11 \n","Verse 12 \n"};
switch (day)
{
case 1:
song = looper(complete,0);
break;
case 2:
song = looper(complete,1);
break;
case 3:
song = looper(complete,2);
break;
case 4:
song = looper(complete,3);
break;
case 5:
song = looper(complete,4);
break;
case 6:
song = looper(complete,5);
break;
case 7:
song = looper(complete,6);
break;
case 8:
song = looper(complete,7);
break;
case 9:
song = looper(complete,8);
break;
case 10:
song = looper(complete,9);
break;
case 11:
song = looper(complete,10);
break;
case 12:
song = looper(complete,11);
break;
default:
JOptionPane.showMessageDialog(null,"Can't you count", "Ha Ha Ha",JOptionPane.WARNING_MESSAGE);
break;
}
return song;
}
public static String looper(String[] a,int number)
{
String temp = "";
for (int i = number; i < a.length; i++)
{
temp = temp + a[i];
}
return temp;
}
public static void showIT(int day)
{
JTextArea songArea = new JTextArea(20, 30);
JScrollPane scroller = new JScrollPane(songArea);
songArea.setText(RemainingSong(day));
JOptionPane.showMessageDialog(null, scroller, "Twelve Days of Christmas",
JOptionPane.PLAIN_MESSAGE);
}
}
Fill in the song lines in place of the verses.
Good Luck
|
|
|
|
|
I want a event where i can delete the text in the text field when ever the user selects(enter) text field to write data, i.e. the text field should be blanked whenever the user clicks on it.
|
|
|
|
|
Read these tutorials, in your case when you click on the button just set the text field property to ""
Tut 1[^] Tut 2[^] Tut 3[^]
Good Luck
|
|
|
|
|
Hi everybody !
I want to convert my pdf files to epub file.
Im using LRFTools(LRFTools-v0.9.209.jar).
I coverted successfully 10/20 files(10 files i got error.)
this is one of some error i got:
C:\>cd epub
C:\epub>set path="%path";"c:\Program Files\java\jdk1.6.0_12\bin"
C:\epub>java -Xms200M -Xmx300M -jar LRFTools-v0.9.209.jar convertPDF c:\epub\pdf
Loading 'CV_007_027_Business_Knigge.pdf' 21 pages -java.io.IOException: Not impl
emented
at org.pdfbox.pdmodel.graphics.color.PDSeparation.createColorSpace(PDSep
aration.java:110)
at org.pdfbox.pdmodel.graphics.color.PDColorSpaceInstance.createColor(PD
ColorSpaceInstance.java:79)
at org.pdfbox.util.operator.pagedrawer.SetStrokingCMYKColor.process(SetS
trokingCMYKColor.java:61)
at org.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:
452)
at org.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java
:215)
at org.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:17
4)
at org.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:110)
at lrf.pdf.PDFSerializer.procPDF(PDFSerializer.java:91)
at lrf.pdf.PDFSerializer.recurse(PDFSerializer.java:33)
at lrf.RecurseDirs.convertPDFActionParams(RecurseDirs.java:242)
at lrf.RecurseDirs.<init>(RecurseDirs.java:91)
at lrf.RecurseDirs.main(RecurseDirs.java:41)
Can anybody tell me about this problem and how to fix it.
Or any version of LRF tools that better.
Thanks and regards!
modified on Thursday, October 8, 2009 5:09 AM
|
|
|
|
|
Where did you download the tools from? I would check if they have a forum and maybe they can help.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
|