|
how to connect ble sensor in swing-java using netsbeans
or
how to create window application using bluetooth low-energy in java language not android
|
|
|
|
|
|
i want to know what causes serialization to do ?what is the loss if we dont use serialization in java?
|
|
|
|
|
|
Hey, I am new to the coding world as well as this site. I have been learning Java by myself for a few days now and wanted to make a gambling program just for fun and practice.
I am going to ask two questions, one short, and one long. I'll ask the long one first! How does my code look for a beginner? I know I haven't dipped into using multiple classes yet or anything, so how does this very basic program look?
package com.company;
import java.util.Scanner;
import java.util.Random;
public class StakingGame {
static int cashStack = 500000;
static int amountStaked;
public static void main(String[] args) {
System.out.println("Welcome to my staking game.");
while(cashStack > 0) {
amountStaked();
System.out.println("Rolling...");
whoWon();
}
System.out.println("You have no more money.");
}
public static void amountStaked() {
Scanner scan = new Scanner(System.in);
System.out.println("How much would you like to stake?");
amountStaked = scan.nextInt();
System.out.println("You have chosen to stake, " + amountStaked + "$, goodluck.");
}
public static int roll() {
Random r = new Random();
int min = 0;
int max = 100;
int rolled = r.nextInt(max-min) + min;
return rolled;
}
public static void whoWon() {
int playerRoll = roll();
int computerRoll = roll();
System.out.println("You have rolled a " + playerRoll);
System.out.println("Your oppent has rolled a " + computerRoll);
if(playerRoll > computerRoll) {
stakeWon(0);
} else if (playerRoll < computerRoll) {
stakeLost(0);
} else {
System.out.println("You guys tied.");
}
}
public static int stakeWon(int newAmount) {
newAmount = cashStack + amountStaked;
cashStack = newAmount;
System.out.println("You have won the stake! You now have " + cashStack);
return cashStack;
}
public static int stakeLost(int newAmount) {
newAmount = cashStack - amountStaked;
cashStack = newAmount;
System.out.println("You have lost the stake.. You now have " + cashStack);
return cashStack;
}
}
My second question is, well, more of a question. I want this program to run until I either terminate it or the player runs out of money. So this is how I decided to go about that. I created this loop in my main method,
public static void main(String[] args) {
System.out.println("Welcome to my staking game.");
while(cashStack > 0) {
amountStaked();
System.out.println("Rolling...");
whoWon();
}
System.out.println("You have no more money.");
}
Is that the best way to make the program run until you either lose or I terminate it??
Thankk you in advance!
|
|
|
|
|
1. The code looks OK but I have not run it to see if it works correctly.
2. That is a generally correct way to do what you want.
If you want to find more samples and guidance then The Java Tutorials[^] is a great place for learning.
|
|
|
|
|
CoderKlip wrote: Is that the best way to make the program run until you either lose or I terminate it??
You probably want the user to terminate it by typing something normal like '0' or 'quit'.
|
|
|
|
|
1. As you said you're new here, your code looks good comparable to other noobies, the indentations look perfectly professional.
2. You should use do-while loop in your code that after every round the code asks you to continue or not, terminating will be a little unsatisfying way to end a game.
|
|
|
|
|
------------
public String getLogin(StudentTO sto)
{
String u=sto.getUsername();
String p=sto.getPassword();
System.out.println(sto.getUsername());
System.out.println(sto.getPassword());
String sql="select * from studentstable";
System.out.println(sql);
Object obj=JdbcTemplate.queryForObject(sql, new RowMapper() {
public Object mapRow(ResultSet rs) throws SQLException {
rs.beforeFirst();
while (rs.next()) {
String userdb=rs.getString("username");
String passdb=rs.getString("password");
String roledb=rs.getString("role");
String string="";
if(userdb.equals(u) && passdb.equals(p) && roledb.equalsIgnoreCase("admin")) {
System.out.println("ADMIN_ROLE");
string="ADMIN";
return string;
}else if(userdb.equals(u) && passdb.equals(p) && roledb.equalsIgnoreCase("user")) {
System.out.println("USER_ROLE");
string="USER";
return string;
}
------------------ after this code the return statement is not coming as object-------------------
Object obj=string;
}
return obj; <-------------------------------------------
}
});
StudentTO sto1=(StudentTO)obj;
System.out.println(sto1);
return sto1.toString();
}
i only want my method String getLogin() to give m result as ADMIIN or USER
modified 15-Jul-17 16:11pm.
|
|
|
|
|
You are replacing obj each time round the loop, so if it finds a record that does not contain the correct values, then obj will refer to an empty string. When you find a matching set of values you should break out of the loop.
|
|
|
|
|
can you tell m to write code in a correct way
|
|
|
|
|
You just need to add a break statement in the two if clauses when you set the string value to "USER" or "ADMIN".
|
|
|
|
|
thnks for the reply but i had solved my prblm with myself.
actually ur ans is not a correct solution buy the way thnxxx
|
|
|
|
|
LOKENDRA YADAV wrote: actually ur ans is not a correct solution buy the way thnxxx The way your code is formatted and broken up in your question it is far from easy to understand, so I had to make a guess.
|
|
|
|
|
|
hi everybody,
i need help in video tracking.Fisrt of all i wanna make gui which shows video(any format) and slider to display with buttons aside which select file and extract the video.Example like this website page
Extract Frames from Video Files[^]
i wanna do like this display in java.Please help me
|
|
|
|
|
|
Hello there. I am trying to implement Observer Pattern . I get notified, successfully, whenever a thread completes. I can start the same thread again when I get notified (through TaskListener I have implemented). But how do I start it again if there was some exception? Example
static MyWork objMyWork = null;
static Thread objThread = null;
public static void main(String[] args) throws
{
TaskListener listener = new TaskListener()
{
@Override
public void threadComplete()
{
try
{
objMyWork = new MyWork();
objMyWork.SetData1(int data);
objMyWork.SetData2(String data);
objMyWork.addListener(this);
objThread = new Thread(objMyWork);
objThread.start();
objThread.join();
}
catch(Exception ex)
{
}
}
}
objMyWork = new MyWork();
objMyWork.SetData1(int data);
objMyWork.SetData2(String data);
objMyWork.addListener(listener);
objThread = new Thread(objMyWork);
objThread.start();
objThread.join();
}
1- What changes do I make in MyWork so that control always comes back in the
catch section of TaskListener::threadComplete() whenever there was exception?
2- Once control is there, how do I start this thread again (I dont want to nest another try-catch block and then another in the nested one and then another .... and so on).
Thanks for anything you share
|
|
|
|
|
|
Hi expert,
I have the following method but no matter what I did to the line pstmt.executeUpdate or change it to if (pstmt.executeUpdate() != -1), it just gave me the error - No value specified for parameter 2..., earlier it was No value specified for parameter 1....
Hope someone can tell me where I had go wrong in my code. Tks.
public static int writeTutorId() throws Exception {
openConnection();
String qry = INSERT_QRY;
int tutor_id = 0;
PreparedStatement pstmt = connection.prepareStatement(qry, new String[] { "tutor_id" });
pstmt.setInt(1, tutor_id);
if (pstmt.executeUpdate() != -1) {
ResultSet rs = pstmt.getGeneratedKeys();
if (null != rs && rs.next()) {
tutor_id = rs.getInt(1);
}
pstmt.close();
connection.close();
}
return tutor_id;
}
|
|
|
|
|
Your query - which you haven't shown - has at least two parameters.
You are only passing one parameter.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
Here's the query :-
private final static String INSERT_QRY = "INSERT INTO project.tutor"
+ "(tutorName, tutorIC, tutorEmail, tutorAddress1, tutorAddress2, zipcode, tutorContactNo, tutorAge, tutorGender, tutorQualification, tutorQualificationDetail) values"
+ "(?,?,?,?,?,?,?,?,?,?,?);";
I did not put in the tutorId cos it is supposed to be generated out by the method I have written.
Initially, I did not include the pstmt.setInt(1, tutor_id) but when I googled, it said I must add it in. Futhermore, I have another insert method whereby all the infor like tutorName etc are inserted.
This method is to solely get the generatedKeys out...
What went wrong ?
modified 3-Jul-17 11:40am.
|
|
|
|
|
Your query has 11 parameters.
You are passing 1 parameter.
Have a guess what the problem might be.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes. It is easy to guess the problem.
I was thinking that (if) there is only a way I do not have to repeat the code with the ps.set blah blah blah for all the rest of the parameters and just on the Id alone. There is a lot of repeating code. Alas, I do not know how to overcome something like this.
The problem now is that I got a java.lang.NullPointerException on the line - Integer parameter - Age.
tutor m = new tutor();
int tutor_id = m.getTutor_id();
PreparedStatement pstmt = connection.prepareStatement(qry, new String[] { "tutor_id" });
pstmt.setInt(1, m.getTutor_id());
pstmt.setString(2, m.getName());
pstmt.setString(3, m.getNRIC());
pstmt.setString(4, m.getEmail());
pstmt.setString(5, m.getAddress2());
pstmt.setString(6, m.getZipcode());
pstmt.setString(7, m.getContactNo());
pstmt.setInt(8, m.getAge());
What causes the problem cos I have double checked I have indeed entered the value into the form....
I don't think I can find out anything like this via googling except asking in forum....
|
|
|
|
|
karengsh wrote: don't think I can find out anything like this via googling except asking in forum. You could try using your debugger and learning how to diagnose simple problems for yourself. Which field on that line is null and how did it become so after all the previous statements worked correctly?
|
|
|
|