sobota, 31 stycznia 2015

how to create temp file with Java

import java.io.File;
import java.io.IOException;

public class tempFile
{
public static void main(String[] args) throws IOException
{
File f = File.createTempFile("temp_", null);

System.out.println(f.getAbsolutePath());

f.deleteOnExit();

}
}

output:
/tmp/temp_538005290434749678.tmp
/tmp/temp_5224913897241434358.tmp


czwartek, 22 stycznia 2015

python loop - basic escape

a = True
print("Enter quit to quit")
while(a):
   
    string = raw_input("Enter your name: ")
    if(string == "quit"):
        a = False
print("the end")
   
   

czwartek, 15 stycznia 2015

very small python "game" thanks to Bucky

import random

class Enemy:
    life = 10
 
    def attack(self):
        print("aouch!")
        self.life -= 1
     
    def checkLife(self):
        if self.life <=0:
            print ("!!!!you are dead!!!!!")
        else:
            print (str(self.life)+ " life left")
         
alien = Enemy()
predator = Enemy()

x = random.randrange(1,11)
for i in range(x):
    alien.attack()
 
alien.checkLife()

środa, 14 stycznia 2015

Simple python class calc

class Kalkulator:
def __init__(self,x,y):
self.x = x
self.y = y

def addXY(self):
print("suma: ",(self.x + self.y))

def subXY(self):
print("roznica: ",(self.x - self.y))

def mulXY(self):
print("mnozenie: ",(self.x * self.y))

def divXY(self):
if (self.y == 0):
print "div by zero"
else:
print("dzielenie: ",(self.x / self.y))

def line():
print 50*"-"

def Main():
a = int(raw_input("enter a: "))
b = int(raw_input("enter b: "))
line()

p1 = Kalkulator(a,b)
p1.addXY()
p1.subXY()
p1.mulXY()
p1.divXY()


if __name__ == '__main__':
Main()

piątek, 2 stycznia 2015

Smoothy way to open file in Java (with exception)

package Pliki;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class ReadFile
{
public static void main(String[] args)
{
try(BufferedReader reader = new BufferedReader(new FileReader("/etc/passwd")))
{
while (true)
{
String line = reader.readLine();
if(line == null)
{
break;
}
System.out.println(line);
}
reader.close();
}
catch (FileNotFoundException e)
{
System.err.println("There is no such file");
}
catch (IOException e)
{
System.err.println("IOExceptions");
}


}
}

czwartek, 1 stycznia 2015

escape from Loop java

package loops;

import java.util.Scanner;

public class runFromLoop
{
static void line()
{
System.out.println("=-=-=-=-=-=-=-=-=-=-=");
}
public static void main(String[] args)
{
String name;
Scanner input = new Scanner(System.in);
StringBuffer buffer = new StringBuffer();

System.out.println("Enter quit to exit");
line();
while(true)
{
System.out.print("Enter something: ");
name=input.nextLine();

if (name.equals("quit"))
{
System.out.println("quit");
break;
}
else
{
buffer.append(name + "\n");
}
}
line();
System.out.print(buffer);

input.close();

}
}