|
Thanks for the suggestions.
I made a start now by listing all the files with the extension .log and it works.
since the files are in yyyymmdd.log format, i only want to list the last one - 1. then copy it to another file called logged.log for instance. Could you please help achieve this with the code below?. The code below navigate to the directory and list all files with the extension .log. The printed output is:
20180118.Log
20180117.Log
20180116.Log
So I am interested in the second one: 20180117.Log
import java.util.List;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.regex.Pattern;
class search
{
public static void main(String[] args) {
String WILD_CARD = "";
List <File> fileList = new LinkedList<File>();
File folder = new File("d:\\");
File[] listOfFiles = folder.listFiles();
if(WILD_CARD!=null) {
Pattern wildCardPattern = Pattern.compile(".*"+WILD_CARD+"(.*)?.log",Pattern.CASE_INSENSITIVE);
for(File file: listOfFiles) {
java.util.regex.Matcher match = wildCardPattern.matcher(file.getName());
while(match.find()){
String fileMatch = match.group();
if(file.getName().equals(fileMatch)) {
fileList.add(file);
}
}
}
}
else
fileList = new LinkedList<File>( Arrays.asList(folder.listFiles()));
for (File f: fileList) System.out.println(f.getName());
}
}
|
|
|
|
|
Obviously it is the opposite of what I suggested, as the list is in descending order.
|
|
|
|
|
I just double checked the output printed is in ascending order as shown below:
20180115.LOG
20180116.LOG
20180117.LOG
20180118.LOG
|
|
|
|
|
Then my first suggestion still applies. Is this really so difficult to understand? It seems quite straightforward to me; or maybe I am missing something.
|
|
|
|
|
Yes. Just not sure how to do it in the code
|
|
|
|
|
Just count the entries, or convert the list to an array and use the offset value I suggested.
|
|
|
|
|
Ok. I tried somehting like this so I can print the last file - 1
it prints the file but with the file path as well
Object item = (((Deque<File>) fileList).getLast()-1);
System.out.println(item);
|
|
|
|
|
So you have a working solution.
|
|
|
|
|
Not yet. It prints the path of the file rather than the file
It should print:
20180117.LOG
but it is printing
<pre> d:\20180117.LOG
I only want the file so I can create a copy of it.
Can you try it and see if works for you? may be format issue?
|
|
|
|
|
You just need to use a method to extract just the filename, such as: File (Java Platform SE 7 )[^]. Simple answers like this can usually be found in the documentation.
|
|
|
|
|
class facto
{
public static void main(String args[])
{
System.out.println(dust(5));
}
static int dust(int z)
{
if (z==1||z==2)
{
return 2;
}
else
{
return dust(z-1) * dust(z-2);
}
}
}
modified 16-Jan-18 8:05am.
|
|
|
|
|
Firstly, you never print any of the interim values, and secondly, your calculations are wrong. See Fibonacci numbers[^]
|
|
|
|
|
I wanted to print Factorial. So, I am sorry for the mistake in the question.
|
|
|
|
|
Your calculation is still wrong.
f(1) === 1 , but your code returns 2 .
And f(n) === n * f(n-1) , but your code is returning f(n-1) * f(n-2) .
Also, unless this is a homework question to test your ability to write recursive functions, it's much more efficient to use a loop to calculate factorials or the Fibonacci sequence.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Maybe you shouldn't give the factorial of 1 the value 2 as it is returning in your first "if" construct. Now I am not expert but I think this should work:
public class facto{
public static void main(String[] args){
System.out.println(dust(5));
}
static int dust(int n)
{
int output;
if(n==1){
return 1;
}
output = dust(n-1)* n;
return output;
}
}
I suppose that in your version the recursion is not able to complete properly because of "maybe" your trying to both return the value and calculate it at the same time. Hope the code snippet helps, I am just a beginner myself so apologies if I didn't get it right. P.S I know I haven't considered the condition of the input being 0.
modified 8-Feb-18 1:12am.
|
|
|
|
|
I have a code for Java in which I am creating 2 Array List from 2 dimensional array.. But I am getting an Array Out Of Bound exception.. I am passing 2 dimensional array to my class method answer.. But it is not working..
import java.io.*;
import java.util.*;
public class Abc {
static void answer(Integer[][] a,int l){
Integer[] un = new Integer[l];
Integer[] un1 = new Integer[l];
final Integer z = 0;
int max1=0,max2=0,count1=0,count2=0;
int ind=0;
ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l]));
ArrayList<integer> arr2 = new ArrayList<integer>(Arrays.asList(a[1][l]));
for(int i=0;i<l;i++){
if(i="" %="" 2="=" 0){
="" max1="Collections.max(arr1);
" count1="" +="max1;
" ind="arr1.indexOf(max1);
" arr1.set(ind,z);
="" arr2.set(ind,z);
="" }
="" else{
="" max2="Collections.max(arr2);"
="" count2="" arr2.set(ind,0);
="" arr1.set(ind,0);
="" }
="" if(count1="" <="" count2){
="" system.out.println("second");
=""> count2){
System.out.println("First");
}
else{
System.out.println("Tie");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
for(int i=0;i
|
|
|
|
|
Please do not post the same question to several forums, stick to one single choice
Multi-posting the same question will not help you to get a quicker or better answer.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
I have this HashMap: <integer,map<integer,string>>.
I want to delete all the Map of specific Integer.
For example, if my Map is:
<1,<11,'aa'><12,'bb'><13,'cc'>>
<2,<21,'dd'><22,'ee'>>
<3,<31,'ff'><32,'gg'><33,'hh'><34,'ii'>>
If the Input Is 2, I want it to be:
<1,<11,'aa'><12,'bb'><13,'cc'>>
<2,<0,null>>
<3,<31,'ff'><32,'gg'><33,'hh'><34,'ii'>>
How do I write it???
Help Me Please!
Thank You!
|
|
|
|
|
|
Thank You!
It's working...
|
|
|
|
|
I have Eclipse on my desktop at home, and on my netbook.
I have a workplace on a thumb drive.
I'm quite new to Eclipse, so this may be trivial to old hands, but it is currently a blockade to me.
The design and source code windows have disappeared. Previously, I could switch between them.
On my screen, I see MyProject.java in the one and only window pane in Eclipse. The font is too small for me to read. Previously, I was able to choose the font and size of the contents. When I try to do that now, no effect occurs on the source code.
The "Design" window pane has totally disappeared. What did I do to make that happen ? How do I get it back ?
I spent ten minutes trying to search and guess the menu system and got nowhere.
What do I click in order to ?...
- Return the "Design" window pane to my environment so that I can click on it and do the graphical stuff.
- Return the source code window pane to my sight which responds to Window -> Preferences -> General -> Appearance -> Colors And Fonts -> (So that I can do the actual code stuff)
Thank you for helping a clueless guy.
|
|
|
|
|
It is a while since I used eclipse, but as I recall there were menu items to show the various windows. There are also a couple of icons at the top right (I think) which allow you to show different layouts for different situations: one for development, and another for run/debug. Take a look at [^] which may give some clues. They do use some odd terminology but it does make more sense as you get used to it.
|
|
|
|
|
sir can u give me coding for fingerprint ,how to store,compare by using morphow device in java programming
|
|
|
|
|
|
Hi, I have a problem when running my project in NetBeans 8.2 and SQL server express 2014.
After doing the programming it shows the result as below
" com.microsoft.sqlserver.jdbc.sqlservExeption Login failed for user ''. ClientConnectionid: d963d05c-a68b-49c0-a7e9-88b829c96174 "
Can anybody help me with this issue, please?
Hehreby the quoding that i key in
"
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url= "jdbc:sqlserver://localhost:1433;"
+ "databaseName = testdb"
+ "user = sa; password = 12345";
Connection con = DriverManager.getConnection(url);
String sql = "Select* from test where username = ? and password =?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString (1, username.getText());
pst.setString (2, passwrod.getText());
ResultSet rs = pst.executeQuery();
if (rs.next())
{
JOptionPane.showMessageDialog(null, "ok, it match");
}
else
{
JOptionPane.showMessageDialog(null, "not ok, try again!!");
username.setText("");
passwrod.setText("");
}
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog (null,e);
}
|
|
|
|