 |
 |
When posting your question please:- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-fou
|
|
|
|
 |
Hi frnds, Wants to read a gz file(contains million lines) from bottom to top. plz help me.
Thanks in advance
|
|
|
|
|
 |
This is like a number guessing game. The computer generates a random number between 1 to 100. The user has one number like 53 in his mind. And the example goes like this
example:
computer:75(random number)
User:higher(input which says 75 is greater than the user guess 53)
Computer:38(almost half of the first generated random number)
User: lower
Computer:57(like 38+(38/2))
User:higher
I want to implement like that but the code i am using is generating the values more than 75(random number) and getting more number of guesses to the user.
The computer asks a series of questions to arrive at the number the user has in mind. The user can only
respond with “higher”, “lower” or “yes”.
The game ends when the user responds with “yes” or “end”.
Example:
Example:
Note:
The program should be written to arrive at the answer asking the least number of questions.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class NumberGuess
{
public static void main(String[] args) throws IOException
{
int max=0,min=0;
Random rand=new Random();
int ComputerNumber=rand.nextInt(100);
while(true)
{
System.out.println("Is the number:"+ComputerNumber);
System.out.println("Please give higher or lower or yes:");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String value=reader.readLine();
String yes="yes",lower="lower",higher="higher";
int temp;
if(value.equals(yes))
{
System.exit(0);
}
if(value.equals(lower))
{
int temp1=ComputerNumber; temp=ComputerNumber+(rand.nextInt(ComputerNumber))+1;
ComputerNumber=temp;
System.out.println("New number:"+ComputerNumber);
}
if(higher.equals(value))
{
int temp1=(ComputerNumber%10)/2-10; int temp2=(ComputerNumber/10)*10;
temp=(ComputerNumber-rand.nextInt(ComputerNumber));
System.out.println(temp1);
System.out.println(temp2);
ComputerNumber=temp2+temp1+1;
System.out.println("New number:"+ComputerNumber);
}
} }}
modified 2-Jan-15 11:13am.
|
|
|
|
|
 |
Will you please help me with the logic to implement that program I tried something but it is wrong thank you
|
|
|
|
 |
Leela Krishna Yadav wrote: I tried something but it is wrong OK, but we cannot guess what that is supposed to mean. Please edit your question and explain exactly what is wrong, and what you need help with.
|
|
|
|
|
 |
You should use "binary chop", not random values. For each response you need to take half the distance between the current and previous guess. If the user says "higher" then add it to the guess, else subtract it. So the sequence for your values would be something like:
Computer : 75 User : chose 53
Lower
subtract half of 75 - 0 : 38 Higher
add half of 75 - 38 : 57 Lower
subtract half of 57 - 38 : 48 Higher
add half of 57 - 48 : 52 Higher
add half of 57 - 52 : 54 Lower
subtract half of 54 - 52 : 53 Correct!
|
|
|
|
 |
Thank you Richard for that help i really appreciate it
|
|
|
|
 |
i have created a jar to copy two files using Variable argument whichis working correctly
e.g java jar filecopy.jar file1 file2
i want to provide a -help option in this which will be like below and user must aware what he has to pass in arguments
e.g java jar filecopy.jar -help
how do i do this not getting any idea :new to java,urgent help required !!!!
modified 31-Dec-14 4:09am.
|
|
|
|
 |
You just add the code to check for "-help" as a filename, and branch to the code which displays the help.
|
|
|
|
 |
did you men alike this ...
public static void main(String[] args) {
String filename1 = args[0];
String filename2 = args[1];
if ((!(filename1.length() > 0)) || (!(filename2.length() > 0))) {
System.out
.println("please provide the foldername and string to be replaced ");
return;
}
if(args[0] == "help" )
{
System.out
.println("please run the utility as filecopy file1 file2");
}
can you edit it if wrong
|
|
|
|
 |
Something like that, but you can easily test it.
|
|
|
|
 |
hi guys,
i have a problem in connecting my web application in javaEE, when i run the servlet i have an error : java.lang.NullPointerException
and this is a screenshot for the error : http://im51.gulfup.com/TnfWrg.jpg
this the code where i try to connect to the database
public class login_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public login_servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
java.sql.Connection connexion = null;
java.sql.Statement statement = null;
ResultSet resultat = null;
String userQuery="";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
try {
connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password");
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement = connexion.createStatement();
resultat = statement.executeQuery("SELECT user FROM Client;");
while (resultat.next()) {
userQuery = resultat.getString("user");
}
} catch (SQLException e) {
} finally {
try {
if (resultat != null)
resultat.close();
if (statement != null)
statement.close();
if (connexion != null)
connexion.close();
} catch (SQLException ignore) {
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("user");
String p=request.getParameter("pass");
HttpSession session = request.getSession(false);
if(session!=null)
session.setAttribute("name", n);
if( n.equals(userQuery)){
RequestDispatcher rd=request.getRequestDispatcher("home.jsp");
rd.forward(request,response);
}
else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.include(request,response);
}
out.close();
}
}
|
|
|
|
 |
So you need to use your debugger to identify where and why you have a null reference.
|
|
|
|
 |
how to do pagination in java swing properly with defaultable model......i have done this but not working properly...:
please give me the code of pagination in swing? i am trying but not working properly......
|
|
|
|
|
 |
Hello. I am trying to write small java bean. The classes for java beans compile successfully. When I try to use them in my .jsp file I get the error
DatabseDAO cannot be resolved to a type
Here are declarations for these beans
import java.sql.*;
import java.io.*;
import java.util.*;
public class DatabseDAO implements Serializable
{
}
import java.io.*;
public class PersonBean implements Serializable
{
}
And here is how I use them in jsp file
<%
DatabseDAO db2 = new DatabseDAO();
ArrayList persons_list = db2.GetPersons("SELECT * FROM People");
PersonBean person_bean = null;
for (int i=0; i<persons_list.size(); i++)
{
person_bean = (PersonBean) persons_list.get(i);
%>
<tr>
<td><% =person_bean.getPersonId() %></td>
<td><% =person_bean.getPersonName() %></td>
<td><% =person_bean.getPersonAddress() %></td>
</tr>
<%
}
%>
The classes (bean and DAO) are not declared in any package. What is wrong here ?
This world is going to explode due to international politics, SOON.
|
|
|
|
 |
Shouldn't DatabseDAO be declared public?
|
|
|
|
 |
I found the answer.
modified 21-Dec-14 22:26pm.
|
|
|
|
|
 |
Hello, I am doing a project for school and I cannot figure out what is wrong.
int AB = 450;
int BC = 140;
int CD = 120;
int DE = 320;
int EF = 250;
int FG = 80;
String Place1, Place2, Place3, Place4, Place5, Place6, Place7, Place8, Place9, Place10;
double MPG;
double MPH;
double Gallon;
String Trip1;
double Hour1;
int Minute1;
double Money1;
int Dist1;
String String;
Scanner scan = new Scanner(new File("C:\\Users\\Evelyn\\workspace\\ACSL\\src\\testimput"));
NumberFormat numfmt = NumberFormat.getCurrencyInstance();
NumberFormat decfmt = new DecimalFormat("00");
String delims = "[ ]+[,]";
MPG = scan.nextDouble();
Gallon = scan.nextDouble();
MPH = scan.nextDouble();
Place1 = scan.next();
Place2 = scan.next();
Place3 = scan.next();
Place4 = scan.next();
Place5 = scan.next();
Place6 = scan.next();
Place7 = scan.next();
Place8 = scan.next();
Place9 = scan.next();
Place10 = scan.next();
For the program, there is a textfile that contains exactly:
Quote: 25, 3.79, 50
A, C
E, F
C, G
B, E
A, G
However, when I attempt to run it crashes.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Prog_2014.main(Prog_2014.java:34)
Does anyone know how to fix this? What am I doing wrong and what must I add.
|
|
|
|
 |
You have not set the comma as a delimiter in your scanner, so it is reading it as a part of the next field.
|
|
|
|
 |
Try adding the following line before the first call to nextDouble .
scan.useDelimiter("\\s*[,\\s*]\\s*");
modified 21-Dec-14 10:23am.
|
|
|
|
 |
Thank you so much! That's very useful. Would you mind explaining the logic and how that statement works so I will be able to do it in the future?
|
|
|
|
 |
Firstly, if you create a delimiter string (as you did in your code) you still need to apply it to the scanner so that the scanner applies it to every line it reads. The default for the scanner is to ignore all whitespace characters (as described in http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isWhitespace(char)[^]). If that does not control the input data that you are working with then you need to set a custom delimiter, which can be a simple string of characters, or a regular expression pattern, as described in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[^].
The pattern I suggested ("\\s*[,\\s*]\\s*") breaks down as follows:
Actual pattern is: \s*[,\s*]\s*
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
[,\s*] : a comma or 0 or more whitespace characters
\s* : 0 or more whitespace characters (e.g. spaces, tabs etc)
I'm not an expert in RegEx patterns so I am sure this can be improved upon. But what you have in your input file is:
25.0, 37.1, 10
So the the basic pattern is:
0 or more whitespace characters
0 or more comma characters
0 or more whitespace characters
a number
0 or more whitespace characters
|
|
|
|
 |
Hello. I am trying to authenticate input from databse using JDBC and then redirect the user to another page accordingly. Here is my code for small servlet that does authentication
public class ServletAuthUser extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String sUserName = request.getParameter("username");
String sPassword = request.getParameter("password");
String sURL = "Jdbc:Odbc:ServletsDB_DSN";
String sForName = "sun.jdbc.odbc.JdbcOdbcDriver";
MyDatabase db = new MyDatabase(sURL, sForName);
boolean bIsUserValid = (sUserName.equals("admin") && sPassword.equals("admin")) ? true : false; db.CloseConnection();
if(bIsUserValid == true)
response.sendRedirect("welcome.html");
else
response.sendRedirect("error.html");
}
And here is my welcome page.
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome ADMIN.</h1>
</body>
</html>
I have tried recompiling the servlets and then restarted the apache server several times but my login servlets still shows me the old output that I tried little ago (another servlet which generates data using JDBC). Why is it still showing the same old output despite recompiling the servlet and restarting the server? Please ask for any info if you need. Thanks for any input.
EDIT
I was not writing the deployment descriptor properly.
modified 19-Dec-14 14:13pm.
|
|
|
|
 |
Are you sure that you have copied the new servlet to the server correctly?
|
|
|
|
|
 |
It is unlikely that any response in this forum would be enough to help you, if you don't understand how to create a simple application such as you describe. For a start you will need to design your database and learn how to access it in Java; take a look at http://docs.oracle.com/javase/tutorial/jdbc/index.html[^]. As for the login screen, that is just a matter of adding a few labels, text boxes and buttons to a JFrame and accessing the information provided by the user. There are many useful samples in http://www.codeproject.com/KB/java/[^].
|
|
|
|
 |
thnks Richard
|
|
|
|
 |
What have you achieved so far?
The Eclipse IDE is a Eclipse RCP Application.
You can easily exchange the original login and build your thingy as a Eclipse RCP Plugin.
Lars Vogel has a lot of really good tutorials for you:
http://www.vogella.com/tutorials/EclipseRCP/article.html[^]
regards Torsten
I never finish anyth...
|
|
|
|
 |
what is interface ?and why it is used?
|
|
|
|
|
|
 |
Hello. I am implementing action event in a class 1, database handling in class 2 while all the controls are instantiated in a 3rd class. Heirarchy becomes like this.
class MyDatabase
{
}
class ActionHandler implements ActionListener
{
MyDatabase db = null;
View m_View = null;
public ActionHandler(View view)
{
m_View = view;
db = new MyDatabase();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == MyView.AddButton) db.AddRecord();
}
}
class View
{
ActionHandler action_handler = null;
public View()
{
action_handler = new ActionHandler(this);
AddButton.addActionListener(action_handler);
}
}
Question is: how do I pass my JTextField values from View class to ActionHandler class ? I don't think passing these values in the constructor of ActionHandler is good idea.
|
|
|
|
 |
You have a reference to the View object in your ActionHandler so you can access the view's properties and methods from there. Although I am not sure where MyView.AddButton fits in with the rest of your code.
|
|
|
|
 |
YESSS. Thanks. One more question: along with View class object if I pass an object of MyDatabase in ActionListener constrcutor as well...like this
class ActionListenderHandler implements ActionListener
{
MyDatabse MyDB = null; View myView;
public ActionListenerHandler(View view, MyDatabase db)
{
myView = view;
MyDB = db;
}
}
class View
{
MyDatabase db = null;
public View()
{
db = new MyDatabase("...");
actionListener = new ActionListenerHandler(this, db)
db.CloseConnection();
}
}
After calling db.CloseConnection in View class, do I have to call myDB.CloseConnection in ActionListener class explicitly ? Thanks
|
|
|
|
 |
No, because MyDB in the ActionListenderHandler and db in the View , both refer to the same object, just by different names, as shown by the line of code:
MyDB = db;
|
|
|
|
 |
Hello
I am experimenting with java, I need a tutorial that will help me with creating rights,since different roles will not have access to same areas of the application.
Also what is the best way to encrypt passwords?
Looking forward to your replies.
Thanks
Act as though it were impossible to fail
|
|
|
|
 |
Anele Ngqandu wrote: Also what is the best way to encrypt passwords?
You don't - you store a salted hash of the password.
Salted Password Hashing - Doing it Right[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
 |
Thank you Sir
Act as though it were impossible to fail
|
|
|
|
 |
Anele Ngqandu wrote: Also what is the best way to encrypt passwords? Just google for examples of encryption in java and you should find lots of examples.
Also, it is recommended that a password be hashed, as Richard mentions, and then store the hash but there are times where encryption is needed.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
 |
Act as though it were impossible to fail
|
|
|
|
 |
RyanDev wrote: there are times where encryption is needed.
I can think of two, and "authenticating users" isn't one of them.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
 |
what are those 2?
Act as though it were impossible to fail
|
|
|
|
 |
- You need to store a password to access an external system or service;
- You're writing your own password manager, and need to be able to see the original password;
For the simple case of "validate that the user entered the correct password", encryption is only slightly better than plain text.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
 |
Richard Deeming wrote: and "authenticating users" isn't one of them. Actually, I've had to do this before. I agree it is not ideal but when interfacing with other systems, sometimes you have to do it this way.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
 |
"Authenticating to a remote system" was one of the cases I was thinking of. I was just trying to keep it separate from "validating that the user entered the correct password".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|