Main Blog
Our most important blog

Home Page




Posts Tagged ‘Java Program’

can someone explain this java program?

//Alex Piwowar 03/10/2009
//reverse integers

import java.util.Scanner;

public class ReverseIntegers{
public static void main(String[] args){

//enter an interger less than 10000

//create scanner
Scanner input = new Scanner(System.in);

//enter an ineger
System.out.print(”Enter an integer less than 10000, ex. 9458 “);
int integer = input.nextInt();

int k = reverse(integer);

System.out.print(k);
}

public static int reverse(int n){

int ret;

while (n != 0){
ret = ret * 10 + ( n % 10 );
n = n / 10;
}

return ret;
}
}

what is this portion:

ret = ret * 10 + ( n % 10 );
n = n / 10;

required for?
how do the numbers reverse?

By: iba_p



2 Comments

This is regarding executing a shell script from java program?

Hi,
I want a sample java code that executes on windows and connects to a unix machine and executes a shell scripts over there and gets the outptut of the shell scripts? Is it possible to that?
I am able to connect to unix machine and but i am not able to execute shell scripts on that machine. Here is the sample code i am using.

import java.io.*;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.connection.ChannelState;
import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.transport.TransportProtocolEventHandler;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;
import com.sshtools.j2ssh.SftpClient;
public class Test
{
public static void main(String[] args)
{
try
{
ConfigurationLoader.initialize(false);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String server=”xyz”,id=”xxx”,password=”yyy”;
SshClient ssh = new SshClient();
ssh.setSocketTimeout(30000);
SshConnectionProperties properties = new SshConnectionProperties();
properties.setHost(server);
properties.setPrefPublicKey(”ssh-dss”);
ssh.connect(properties);
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(id);
pwd.setPassword(password);
int result = ssh.authenticate(pwd);
if (result == AuthenticationProtocolState.COMPLETE)
{
System.out.println(”authentcation successfull”);
SftpClient sftp = ssh.openSftpClient();
sftp.cd(”MyTool”);
Process proc = Runtime.getRuntime().exec(”./Test.sh”);
System.out.println(”Print Test Line.”);
StringBuffer strBuf=new StringBuffer();
String strLine=”";
BufferedReader outCommand = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
while((strLine=outCommand.readLine())!=null)
{
strBuf.append(strLine);
}
System.out.println(strBuf);

}
else
{
System.out.println(”unable to connnect (authentication failed)”);
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

using this i am able to connect to unix server . I guess “Process proc = Runtime.getRuntime().exec(”./Test.sh”);” this particular command is looking for file test.sh in local machine.

Any suggestions on this would help me a lot.

By: The ROCK



1 Comment

why doesn’t my polygon draw (java program)?

it makes this teeny tiny line

whats wrong with it, please help!!!!!!!!!!!!1
___________________________________
import gpdraw.*;

public class GraphicPolygon extends RegularPolygons {
private DrawingTool pen;
private SketchPad myPaper;
private double xPosition, yPosition;

// constructors

// Initializes a polygon of numSides sides and sideLength
// length in the superclass. The polygon is centered at
// xPosition = yPosition = 0
public GraphicPolygon(double numSides, double sideLength, double xPosition, double yPosition){

super (numSides, sideLength);
xPosition=0;
yPosition=0;
}

public GraphicPolygon(int numSides, double sideLength, double x, double y){
xPosition = x;
yPosition = y;
myPaper = new SketchPad (500, 500);
pen = new DrawingTool (myPaper);
}

// public methods

// Sets xPosition = x and yPosition = y to correspond to the new
// center of the polygon
public void moveTo(double x, double y){
pen.up();
pen.move(x,y);
}

public double getLength(int sideLength) {
return sideLength;
}

public double getSides (int numSides) {
return numSides;
}

// Draws the polygon about the center point (xPosition, yPosition).
// Uses properties inherited from RegularPolygon to determine the
// number and length of the sides, the radius of the inscribed circle,
// and the vertex angle with which to draw the polygon
public void draw(double sideLength, int numSides){
pen.down();
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
pen.forward(sideLength);
pen.turn(((numSides - 2)*180)/numSides);
}
}

public class Driver {

public static void main(String[] args) {

GraphicPolygon poly = new GraphicPolygon(4,150, 0, 0);
GraphicPolygon poly2 = new GraphicPolygon(4,150,10,100);
poly2.getSides(4);
poly2.getLength(150);
poly2.moveTo(10,100);
poly2.draw(4,150);
}
}

By: BB



1 Comment

JAVA program to find nos of pallindrome word in a string and print the pallindrome words?

class palin
{public static void main(String args[])
{ int i,j,k,l,lp,counter=0;
String m=”my mom chocolate cake icecream arora madam dog coco kaka itat papa”,n=”",o,p=”";
char ch=’ ‘,ph=’ ‘,bh=’ ‘,kh=’ ‘;
l=m.length();
// loop
for(i=0;i {ch=m.charAt(i);
if(ch==' '){break;}
p=m.substring(0,i);
continue;}
lp=p.length();
for(j=lp-1;j>=0;j–)
{ch=p.charAt(j);
n=n+ch;System.out.println(n);
}if(n.equalsIgnoreCase(p));
{System.out.println(”The word is pallindrome”);
System.out.println(p);
counter++;
}System.out.println(”nos of pallindrome words=”+counter);}
}

i cannot use split function plz help me……

By: priya



5 Comments

Java program output doubt?

I’m learning java, and I have the following doubt, When I compile and run this code:

public class Example {
public static void main(String args[]) {
int d1 = 11;
int d2 = 11;
method(d1, d2);
System.out.println(”d1 is ” + d1
+ “\nd2 is ” + d2);
}

public static void method(int d1, int d2) {
d2= 100 ;
d1 = d2;
}
}

I get this output in the console

d1 is 11
d2 is 11

Shouldn’t they be both 100 since I called the method to change before I outputted them?

By: Zelot X



2 Comments

What is wrong with this java program?

I’m writing a java program to change all the small letters in an inputted string into caps, and vice versa

import java.io.*;
public class smalltobig
{
public static void main(String []args)throws IOException
{
BufferedReader keyin=new BufferedReader (new InputStreamReader (System.in));

String inp;
int cnt = 0;int ctr = 0;
int z = 0;
char c = ‘ ‘;
char d = ‘ ‘;
String small = “qwertyuiopasdfghjklzxcvbnm”;
String output = ” “;

System.out.println(”enter”);
inp = keyin.readLine();

z = inp.length();

for(cnt = 0;cnt < z;cnt++)
{
for(ctr = 0;ctr < 26;ctr++)
{
if(inp.charAt(cnt) == small.charAt(ctr))
{
c = Character.toUpperCase(inp.charAt(cnt));
output = output + c;
break;

}

else
{
d = Character.toLowerCase(inp.charAt(cnt));
output = output + d;
break;
}

}

}

output = output.trim();

System.out.println(" new String is " + output);

}

}

By: siddarth p



1 Comment

Help with java program please?

public class ReverseWords{
public static void main(String[] args){
String word,word2;
char ch;
int pos,length;
word=IO.getString(”enter a word”);
word2=”";
length=word.length();
for(pos=length; pos < 0; pos--){
ch=word.charAt(-1);
word2=word2+ch;
}
if(word2.compareTo(word)>0){
System.out.println(” IS NOT A PALINDROME “);
}else{
System.out.println(” IS A PALINDROME “);
}

}
}

its not working..
enter a word: mom
IS A PALINDROME
enter a word: television
IS A PALINDROME
enter a word: computer
IS A PALINDROME

any ideas why, and can you help me fix it so it works?

By: anglesuks



No Comments