I am a beginner in java and taking the course Algorithm, which is provided by Princeton. I am stuck at redirection and piping on page 40 in chapter 1.
I use notepadd++ with nppexec to run java, and my execute is written as
NPP_SAVE
javac -encoding UTF-8 "$(FULL_CURRENT_PATH)"
echo
echo ==========编译成功后开始运行==========
java -cp "$(CURRENT_DIRECTORY);D:\Program Files\java\jdk\lib\algs4.jar" "$(NAME_PART)"
However when I use redirection symbol > and < such as
NPP_SAVE
javac -encoding UTF-8 "$(FULL_CURRENT_PATH)"
echo
echo ==========编译成功后开始运行==========
java -cp "$(CURRENT_DIRECTORY);D:\Program Files\java\jdk\lib\algs4.jar" "$(NAME_PART)" largeW.txt < largeT.txt
the program doesn't work. So I think symbol > and < may be used in cmd, and I want to know how to redirect with nppexec.
Any advice is helpful. Thank you.
This example is from the book on page 9:
import edu.princeton.cs.algs4.*;
import java.util.*;
public class BinarySearch
{
public static int rank(int key, int[] a)
{
int lo = 0;
int hi = a.length - 1;
while (lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
public static void main(String[] args)
{
In in = new In("G:\\java\\1.1\\binarysearch\\BinarySearch.txt");
int[] whitelist = in.readAllInts();
Arrays.sort(whitelist);
while (!StdIn.isEmpty())
{
int key = StdIn.readInt();
if (BinarySearch.rank(key, whitelist)!= -1) StdOut.println("here it is\n");
else StdOut.println("where it is\n");
}
}
}
Execute of nppexec is
NPP_SAVE
javac -encoding UTF-8 "$(FULL_CURRENT_PATH)"
echo
echo ==========编译成功后开始运行==========
java -cp "$(CURRENT_DIRECTORY);D:\Program Files\java\jdk\lib\algs4.jar" "$(NAME_PART)" largeW.txt < largeT.txt
The largeW.txt and largeT.txt have some integers in them.
However this execute doesn't read two txts.
Regrettably, you don't say precisely what you are attempting to do.
What you have written should provide the program java with input from the file data.txt. In all probability, java ignores the input as it has all the information it needs from the remaining parameters.
If you want to supply data.txt as input to whatever the java program is then try using ^< to tell cmd that the < is data, not a directive for cmd.
If that doesn't work, then you'd need to explain further what you are attempting to do.
Maybe run it directly from cmd without using nppexec?
To compile javac .java and run it java < data.txt
Related
I am looking for a quick way to remove null characters from a text file in Windows.
The solution consisting in using Notepad++ and replacing "\0" by nothing in all document (as described here) is not working with very big files. Mine is about 180M and notepad++ is stuck infinitely trying to do the job.
I know that this is an old post, but i think it would be useful for others.
This approach works only if the nulls to delete are on the end of the line (In my case i have lines long 1000+ with 600 characters of null on the end).
Just copy the whole thing, and past it on a new file tab, and automatically notepad will replace all nulls in spaces. Then just save using ctrl+space+s to trim all lines.
Hope this helps
Here is the solution I found for Windows. The idea is to import this solution from UNIX to Windows.
1) Downdload and install CoreUtil which is a collection of basic file, shell and text manipulation utilities for Windows.
In windows 7 exec files will be typically be installed in
c:\Program Files (x86)\GnuWin32\bin
2) remove NULL characters by running this command in cmd window:
tr -d '\000' <input_file >output_file
example:
c:\Program Files (x86)\GnuWin32\bin>tr -d '\000' <putty_measurements_1.log >putty_measurements_2.log
I've been looking for tools to remove trailing NULLs from big files, and the solutions I found didn't work with 1GB+ files or took ages. Therefore I designed my own in C#, which works pretty well, and here it is:
private void CopyContentsUntilNull(string source, bool keepFileDate = true)
{
string destination = $"{Path.GetDirectoryName(source)}{Path.GetFileNameWithoutExtension(source)}_fixed{Path.GetExtension(source)}";
var sourceDate = File.GetLastWriteTime(source);
int bufferSize = 10000;
var buffer = new byte[bufferSize];
int nullCount = 0;
int readCount;
using (var srcStream = File.OpenRead(source))
using (var dstStream = File.OpenWrite(destination))
{
do
{
readCount = srcStream.Read(buffer, 0, bufferSize);
int bytesToCopy = FindTrailingNull(buffer, readCount);
if (bytesToCopy > 0)
{
if (nullCount > 0)
{
var block = Enumerable.Repeat((byte)0, nullCount).ToArray();
dstStream.Write(block, 0, nullCount);
nullCount = 0;
}
dstStream.Write(buffer, 0, bytesToCopy);
}
nullCount += bufferSize - bytesToCopy;
} while (readCount == bufferSize);
}
if (keepFileDate)
File.SetLastWriteTime(destination, sourceDate);
}
private int FindTrailingNull(byte[] buffer, int readCount)
{
for (int i = readCount - 1; i >= 0; i--)
if (buffer[i] != 0)
return i + 1;
return 0;
}
Beware that some files already have NULLs at the end, like zip files (from 2 to 4), so you might need to add some at the end until it works. The same applies to docx, xlsx, etc. as they are also zip files.
I have installed java, followed all the ubuntu specific advises that I found on SO.However, as I do javac Program.java, it looks like I'm entering an infinite loop.Just keep typing and can't even get out(even clt-c does not work).
Reverse.java
/**
* This program echos the command-line arguments backwards.
**/
public class Reverse {
public static void main(String[] args) {
// Loop backwards through the array of arguments
for(int i = args.length-1; i >= 0; i--) {
// Loop backwards through the characters in each argument
for(int j=args[i].length()-1; j>=0; j--) {
// Print out character j of argument i.
System.out.print(args[i].charAt(j));
}
System.out.print(" "); // add a space at the end of each argument
}
System.out.println(); // and terminate the line when we're done.
}
}
I can run other programs(Ruby, C++, C etc).There's sth that I'm missing here and I will appreciate any help.
Which is more quickly evaluated? I'm using java syntax, but any programming language would apply to the question.
while (1==1) {
}
while (true) {
}
I tried testing this with a large loop. The results varied far too much to give me a solid answer.
What you are trying to do is a "micro optimisation". It's not worth it. Any decent compiler will produce identical code, since 1 == 1 is obviously the same as true. Even if it was different code, that little bit of change will not make any measurable difference.
While #gnasher729's answer is totally right for Java, here is how to find out by yourself.
See the following class file:
import java.util.Date;
public class SoOne {
public static void main(String... args) {
long counter = 1;
long start = new Date().getTime();
while ( 1 == 1 ) { // other class has while ( true )
counter += 1;
if ( counter % 1000000 == 0 ) {
System.out.println("after: "
+ ( new Date().getTime() - start )
+ ": " + counter
+ " per second: "
+ ( counter
/ ( new Date().getTime() - start )));
}
}
}
}
If you save this to f.ex. SoOne.java, change the line to while (true), save it as SoTrue.java, and compile both via javac SoOne.java SoTrue.java, you get the class files. These can be disassembled via
javap -c SoOne.class >so.javap
javap -c SoTrue.class >st.javap
The differences are
diff so.javap st.javap
1,3c1,3
< Compiled from "SoOne.java"
< public class SoOne {
< public SoOne();
---
> Compiled from "SoTrue.java"
> public class SoTrue {
> public SoTrue();
So the compiler really removes both checks. See also https://en.wikipedia.org/wiki/Program_optimization#When_to_optimize about when to optimize.
i want to out put like this-
150 can be fitted in:
short
int
long
150000 can be fitted in:
int
long
1500000000 can be fitted in:
int
long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
long
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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
byte b;
long l;
int t, v;
int[] n;
Scanner in=new Scanner(System.in);
System.out.println("Inter the number");
v=in.nextInt();
n=new int[v];
int i=0;
while(n.hasNextInt())
{
n[i]=in.nextInt();
i++;
}
for(int k=0;k<t;k++)
{if(b<n[k])
{
System.out.println(k+"this is fit in");
System.out.println("*short");
System.out.println("*int");
System.out.println("*long");}
else if(t==n[k]){
System.out.println(n[k]+"this is fit in");
System.out.println("int");
System.out.println("long");}
else if(t<=n[k]){
System.out.println(n[k]+"this is fit in");
System.out.println("int");
System.out.println("long");}
else if((t<=n[k])&&(l==n[k])){
System.out.println(n[k]+"this is fit in");
System.out.println("long");}
else{
System.out.println(n[k]+"this not fitted any where");}}}}
A quick fix is to use an IDE like eclipse or netbeans that formats your code and makes life far easier to debug, develop in open-source and reduces the development time.
Coming to the errors: I can spot that you have missed a } at the end of the program script and I notice a syntactically wrong statement:
} else if (t <= n[k] > ) {
Please confirm to correct syntactically code and use formatting to enable fellow developers help you. Good luck!
I expect it's the fact you have no logical join (&& or ||) and value here:
else if(t<=n[k]>){
// ------------^
Also note that the type int[] has no hasNextInt method, so this line:
while(n.hasNextInt())
won't compile (as n is int[]).
There are about a half dozen other logic errors in there (using uninitialized variables, etc.), but hopefully that gets you headed the right way.
I got a relatively simple question.
I have a few mac applications that have launchers written in bash.
I wanted to add a little feature to the launchers, by letting others access a config.app or something else located in /Contents/Resources, when they press the 'option/alt' key at the startup of the app. Kinda like iTunes or iPhoto, where you can access a little options menu.
I don't know how the code should look like when it's purely in bash; i found a few examples that make use of applescript and/or cocoa hooks, but none purely in bash.
Something like: if 'optionKeyDown'; then open "$WORKDIR/../Resources/config.app"
Or is this not possible in pure bash at all?
There's a good solution here: http://lists.apple.com/archives/applescript-users/2009/Sep/msg00374.html
Take the following code:
#import <Carbon/Carbon.h>
int main (int argc, const char * argv[]) {
unsigned int modifiers = GetCurrentKeyModifiers();
if (argc == 1)
printf("%d\n", modifiers);
else {
int i, result = 1;
for (i = 1; i < argc; ++i) {
if (0 == strcmp(argv[i], "shift"))
result = result && (modifiers & shiftKey);
else if (0 == strcmp(argv[i], "option"))
result = result && (modifiers & optionKey);
else if (0 == strcmp(argv[i], "cmd"))
result = result && (modifiers & cmdKey);
else if (0 == strcmp(argv[i], "control"))
result = result && (modifiers & controlKey);
else if (0 == strcmp(argv[i], "capslock"))
result = result && (modifiers & alphaLock);
}
printf("%d\n", result);
}
return 0;
}
Paste it into a file called, e.g. keys.m.
Then build a command line utility like this:
$ gcc keys.m -framework Carbon -o keys
Put the keys executable somewhere in your path, e.g. /usr/local/bin, or even just in the same directory as your bash script, then you can call it as e.g. keys option and check the returned string for "0" or "1".