Java Queries Here..

coderunknown

Retired Forum Mod
Joined
Jan 16, 2010
Messages
13,208
is there a way to pass a value back to a class or function from an Action Listener?
 

prateek007391

In the zone
Joined
Nov 10, 2008
Messages
270
Please check these codes :

public class Card {

private String face;
private String suit;

public Card(String cardFace, String cardSuit){

cardFace = face;
cardSuit = suit;

}

public String toString(){

return face + " of " + suit;
}
}

Code:
import java.util.Random;

public class DeckOfCards {

	private Card deck[];
	private int currentCard;
	private final int NUMBER_OF_CARDS = 52;
	private Random randomNumber;
	
	public DeckOfCards(){
		
		String faces[] = {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"};
		String suits[] = {"HEARTS", "DIMOND", "SPADES", "CLUBS"};
		
		deck = new Card[NUMBER_OF_CARDS];
		
		currentCard = 0;
		randomNumber = new Random();
		
		for(int count = 0; count<deck.length; count++){
			
			deck[ count ] = new Card(faces[ count % 13 ], suits[ count / 13 ]);
		}
		
	}
	
	public void shuffle(){
		
		currentCard = 0;
		
		for(int first = 0; first<deck.length; first++){
			
			int second = randomNumber.nextInt(NUMBER_OF_CARDS);
			
			Card temp = deck[first];
			deck[first] = deck[second];
			deck[second] = temp;
			
		}
	}
	public Card dealCard(){
		
		if(currentCard < deck.length )
			return deck[ currentCard++ ];
		else
			return null;
		}
}

Code:
public class DeckOfCardTest {

	
	public static void main(String[] args) {
		
		DeckOfCards myDeckOfCards = new DeckOfCards();
		myDeckOfCards.shuffle();
		
		
		for(int i = 0; i<13; i++ ){
			
			System.out.printf("%-20s%-20s%-20s%-20s\n", myDeckOfCards.dealCard(), myDeckOfCards.dealCard(),myDeckOfCards.dealCard(),myDeckOfCards.dealCard());
		}
	}

}

The Output must be Different cards but instead I get null of null 53 times.

Please help me, let me know whats wrong.

Sorry, got things Working.

Such a small Mistake and everything messes up.
 

braindead

Bankaiiiii
Joined
Jun 9, 2011
Messages
97
I just started using netbeans and dont know much about java either.
For the following code it shows "package com.amazonaws does not exist ...."
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
......
i think i have to add libraries or something like that. I have already downloaded aws-java-sdk-1.3.8. how do i add now? help !
 

Faun

Wahahaha~!
Staff member
Joined
Dec 8, 2006
Messages
9,781
^^Should be able to add the jar files into the project
 

masterkd

Padawan
Joined
Feb 9, 2011
Messages
981
Download proper .jar file(sorry, don't know which one you need here)..right click on your project..go to properties->java build path->add external jar->select the .jar file->click open and you're done!!
 

braindead

Bankaiiiii
Joined
Jun 9, 2011
Messages
97
I got the jar files. just don't know how to add.
Properties attached.
Using NetBeans 7.11
 

gopi_vbboy

Cyborg Agent
Joined
Mar 1, 2007
Messages
1,436
What is static branch in a class mean? Like below-



Code:
public class BarcodeEAN128 extends Barcode {

      //some variables decalration

       
         static {              
        final Map m = new HashMap();
         //some processing
           }

}
 

masterkd

Padawan
Joined
Feb 9, 2011
Messages
981
@braindead,
sorry..previous one i posted for eclipse
for netbeans, project properties->libreries->add jar

What is static branch in a class mean? Like below-



Code:
public class BarcodeEAN128 extends Barcode {

      //some variables decalration

       
         static {              
        final Map m = new HashMap();
         //some processing
           }

}

this is static block..codes inside this block will get executed first when the class is loaded!!
 

braindead

Bankaiiiii
Joined
Jun 9, 2011
Messages
97
^^ tried that , doesnt seem to work.

Now im desperate. Could someone please help me from the start.
Have a bunch of java programs already written in n++.
How do i import all these as a single project to either netbeans or eclipse or any other IDE?
And also add .jar file after that?
 

Garbage

God of Mistakes...
Joined
Dec 26, 2005
Messages
1,896
Including external JARs in a JAR using Netbeans | one.more
 

utkarsh73

Journeyman
Joined
Oct 13, 2011
Messages
248
I just had an overview of Java and I wanted to learn it thoroughly. So I started with Thinking in Java-3rd Edition HTML version but it is only for upto JDK 1.4. Will it pose any problems if I learn with this book or is it recommended to learn from a book updated with latest JDK??
 

vickybat

I am the night...I am...
Joined
Aug 16, 2008
Messages
5,649
Guys having trouble providing keyboard input as arguments to a function especially when passing more than 1 argument. Written a java code i procedural style ( without any use of objects) to show if a triangle can be formed when we provide three integer inputs.

The code works flawlessly when manually providing arguments to the method isTriangle.

But when using providing inputs through keyboard , it throws error. Providing the code below:


Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class triangleFormation {
public static boolean isTriangle (int a, int b, int c){
	if (c>a+b || a>b+c || b>a+c ){
		return false;
	} else{
		return true;
	}
}

public static void main (String [] args)throws IOException {
	
   BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
	String s = bufferedreader.readLine();
	String r = bufferedreader.readLine();
	String t = bufferedreader.readLine();
	
    int m = Integer.parseInt(s);
    int n = Integer.parseInt(r);
    int o = Integer.parseInt(t);
	
	
	
	System.out.println("the triangle is " + isTriangle (m,n,o));
}
}

I don't know if the process of providing keyboard input is correct or not. Only thing i found that its a hassle i java or i don't know some basic concepts. Providing inputs was so easy in c++.

Please throw some light into it guys. :)
 
Last edited:
Top