Linux subsystem on windows cannot compile java code - windows

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.

Related

Array accessing in arduino and for loop

How can I access data values from the arrays in Arduino programming?
The program is given below :
int myArraylt[24]= {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
int myArraygt[24]= {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};
void setup() {
for (int i=1;i=1;i++)
if (myArraygt(i)>myArraylt(i))
println( SSystem is on MG);
else
println( SSystem is on GRID);
}
void loop() {
// put your main code here, to run repeatedly:
}
In your code there are few mistakes
array index starts from 0 ,but in your for loop you started it from 1
In the loop condition expression is checked as i=1 it runs loop only for array index 1
In order to access data values from array use array[i] format(not array(i))
for printing in arduino use Serial.println() ,not just println()
For accessing the array values the code should be as follows
int myArraylt[24]= {3530,1580,3880,2780,4040,11260,7935,6655,2100,5100,1450,2200,2200,5900,6180,4230,2405,3560,4535,12635,12085,3500,930,3430};
int myArraygt[24]= {0,0,0,0,0,0,6320,5496.9,5948,4124.1,3848.4,3573,3022.2,3297.6,3298.2,3573,4123.2,0,0,0,0,0,0};
void setup() {
for (int i=0;i<24;i++)
if (myArraygt[i]>myArraylt[i])
Serial.println( SSystem is on MG);
else
Serial.println( SSystem is on GRID);
}
void loop() {
// put your main code here, to run repeatedly:
}
And if you want to print SSystem is on MG and SSystem is on GRID on the screen then it must be put inside double quotes as Serial.println( "SSystem is on MG"); and Serial.println( "SSystem is on GRID");

JAVA (I've tried reversing two String using for loop

(1) Can anyone tell me how do I declare array org[] and rev[] where I'm storing my two strings the original and reverse string?
(2) When I'm trying to store the characters of original String in org[] using charAt() they are throwing me an error.
Can anyone help me out on reversing two strings without making use of reverse() instead making use of for loop ?
String a = "12345";
char [] original= a.toCharArray();
char[] reverse = new char[a.length()];
int j =0;
for(int i=reverse.length-1; i>=0; i--) {
reverse[j] = original[i];
j++;
}
System.out.println(new String(reverse));
Here's a quick method you can use (must go inside your class):
public static void getCharacters(String input, char[] forward, char[] reverse)
{
for(int i = 0; i < input.length(); i++)
{
forward[i] = input.charAt(i);
reverse[i] = input.charAt(input.length() - 1 - i);
}
}
This assumes that forward and reverse were already allocated, and were initialized to the length of the string input. Example usage from inside another method:
String input = "Hello, world!";
char[] forward = new char[input.length()];
char[] reverse = new char[input.length()];
getCharacters(input, forward, reverse);
// forward and reverse now contain the characters from input

Redirection and Piping

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

I am getting ';' expected error,illegal start expression and else without if

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.

XTend For-Loop Support and Adding Range Support

I can't seem to find a great way to express the following in Xtend without resorting to a while loop:
for(int i = 0; i < 3; i++){
println("row ");
}
println("your boat");
So, I guess my question has two parts:
Is there a better way to do the above? I didn't see anything promising in their documenation
A large portion of the features the language has are just Xtend library extensions (and they're great!). Is there range() functionality à la Python that I don't know about?
I ended up rolling my own and got something like the following:
class LanguageUtil {
def static Iterable<Integer> range(int stop) {
range(0, stop)
}
def static Iterable<Integer> range(int start, int stop) {
new RangeIterable(start, stop, 1)
}
def static Iterable<Integer> range(int start, int stop, int step) {
new RangeIterable(start, stop, step)
}
}
// implements Iterator and Iterable which is bad form.
class RangeIterable implements Iterator<Integer>, Iterable<Integer> {
val int start
val int stop
val int step
var int current
new(int start, int stop, int step) {
this.start = start;
this.stop = stop;
this.step = step
this.current = start
}
override hasNext() {
current < stop
}
override next() {
val ret = current
current = current + step
ret
}
override remove() {
throw new UnsupportedOperationException("Auto-generated function stub")
}
/**
* This is bad form. We could return a
* new RangeIterable here, but that seems worse.
*/
override iterator() {
this
}
}
The exact replacement for
for(int i = 0; i < 3; i++){
println("row ");
}
is
for (i : 0 ..< 3) {
println("row ")
}
Notice the exclusive range operator: ..<
Also you can doing it more idiomatically with
(1..3).forEach[println("row")]
Very new to Xtend but man it makes programming on the jvm awesome.
To me a range-based forEach implies the range is somehow meaningful. For looping a specific number of times with no iteration variable, I find Ruby's times loop expresses the intent more clearly:
3.times [|println("row")]
Sadly it's not a part of IntegerExtensions, but the implementation is trivial:
def static times(int iterations, Runnable runnable) {
if (iterations < 0) throw new IllegalArgumentException(
'''Can't iterate negative («iterations») times.''')
for (i: 0 ..< iterations) runnable.run()
}
Heh, I found the answer a little while later:
for(i: 1..3) {
println("row ")
}
Since Xtend 2.6, we also support the "traditional" for-loop, just like in Java.
There is actually a version of forEach() that accepts a lambda with two parameters.
It is useful if you need to access the iteration index within the loop.
(10..12).forEach[ x, i | println('''element=«x» index=«i»''')]
prints:
element=10 index=0
element=11 index=1
element=12 index=2

Resources