javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

A simple way to do it is read the entire file and store its data somewhere (for example a Vector). Then change the credit in the Vector, after you find the right element, and save the Vector with the new data back to the file

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

As the printStacktrace states:

...
at java.util.Scanner.nextInt(Scanner.java:2050)
at Television.setChannel(television.java:28)
...

There is an error at the method: setChannel of the Television class at line: 28. And the problem is with the way you use the nextInt method of the Scanner class. Better use the 'next' method and convert what you get to int.
It wasn't very difficult to figure that out, so post only the code of the Television class.

AND USE CODE TAGS. Click the button: '#' that is above where you post your reply

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Don't worry everybody does that mistake. And I am tired of replying to it.

Just use the equals method.

When you use '==' to compare String (remember Strings are Objects) you compare to see if they are the SAME objects. They are not, but they have the same value:

if (Input1.equals("P")) {
 .....
}
String s1="a";
String s2="a"; //2 Different objects.

(s1==s2) //false
(s1.equals(s2)) //true

BUT

String s1 = "a";
String s2 = s1; //they are the same

(s1==s2) //true
(s1.equals(s2)) //true

Also you will need to have all of your IFs as 'else if':

if () {

} else if () {

} else if () {

} .... {

} else {
System.out.println("");
System.out.println("Error - Input not recognised!");
System.out.println("");
}
Laidler commented: the .equals method was very usefull in my studies +1
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And now that I looked at it, Yes the problem IS the 'while'. I was about to say that it made no difference even if it was 'if',
BUT:
When you use While and the query doesn't return anything, the variables: 'mobileno' that are inside the while don't change value, so when later you do: if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd"))) It will return true even if there is no entry in the database because you use the old values that 'mobileno'

So use the if statement

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you are creating a new user, are you checking if the user already exists before performing an INSERT?

Perhaps the mobileNo should be declared as Primary Key. Then if you try to insert a new row that has the same mobileNo it will throw an SQLException.

Or if you unfamiliar with this, you can try quering with mobileNo and if the query returns something don't INSERT.

Tha above are suggestions and questions for the logic used in the: newregister.jsp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Are you familiar with the 'append' argument of the FileWriter ?

BufferedWriter writer= new BufferedWriter(new FileWriter(file, true));

When you try to write to the file, the new data will be appended after the existing

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Programmers are never in shape... always either skinny or fat or pudgy... hardly ever muscular or physically active

Not true.
I was a fencing champion in my country. My colleague who sits next to me should be working as a bodyguard with all the muscles he has. And the guy who sits opposite to me is 6 feet tall and participates in tennis tournaments.

And I think we should stop replying to this thread because the Administrators will give us all bad rep with all the irrelevant things we have written

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

thanks for your help. its working now

Did you put BufferedReader in a try-catch block? Do you want an example?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know how to catch Exceptions? I assumed that you do.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Once you get the file: File file = fc.getSelectedFile(); use the code to read the file's lines.

File file = fc.getSelectedFile();

BufferedReader reader = new BufferedReader(new FileReader(file));

String line = reader.readLine();
while (line != null) {
//do whatever you want with the line read.
//perhaps append it to the TextArea.

line = reader.readLine();
}
reader.close();

Your imports are fine.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

With this code you can read files:

File fileName;
//OR
String fileName;

BufferedReader reader = new BufferedReader(new FileReader(fileName));

String line = reader.readLine();
while (line != null) {
//do whatever you want with the line read.
//perhaps append it to the TextArea.

line = reader.readLine();
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Read the file for its data.
Then use that data to query the database: Run an UPDATE to record you want to update.

Learn how to read files.
Learn SQL.
Learn how to run queries with java.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

kindly please help to solve my problem???
create a program that the user allow to input a 5 integers, that looking the lowest value?

It would be useful to read a Thread before posting to it. Perhaps your question has already been answered.

jasimp commented: Good point. +9
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Every time you do: Time application = new Time(); you create a new object and therefor you reset the values back to zero.
So create only ONE object (do this once: Time application = new Time(); )
And then use the application instance to change its values.


By the way, the Time class is totally wrong. The get, set methods are not suppose to print anything:

class Time {
private int seconds = 0;
....
....
public Time() {

}

public Time(int seconds, ......) {
   this.seconds = seconds;
   .....
   .....
}

public int getSeconds() {
  return seconds;
}

public void setSeconds(int seconds) {
  this.seconds = seconds;
}
}

Now if your teacher wants the set methods to return boolean, then:

public boolean setSeconds(int seconds) {
  if (seconds<0) {
     return false;
  }
  this.seconds = seconds;
  return true;
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I hate to break it to you mahaboob Basha but this is not the right way to do this thing.
Firstly you need to put DB functionality out of the Servlet.
Secondly Servlets are hardly used for generating GUI. (Use JSP)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I think you have misplaced the parenthesis:

This:
if (!Character(userPass.charAt(index == 'c')))

Should be:
if (!Character(userPass.charAt(index) == 'c'))

You could also try:

goodSoFar = userPass.equals("cool");
return goodSoFar;
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Vehicle vehicles [] = new Vehicle[5]; int i;
vehicles[0].initializeVehicle(18000.00f, 2007, "Nissan", "Sentra");

vehicles[0] is null. You need to do:
vehicles[0] = new Vehicle();
or
vehicles[0] = new Vehicle(price, year, make , model);

Also, I could not find where the initializeVehicle method is declared. It should be put in the Vehicle class if you want to call it like this


The rest of the code is a mess

Also the SAME way the Vehicle has price and year,
The DealerShip class will have private variables: private Salesman salesman; and Vehicle [] vehicles = new Vehicle[5]; And you will use get, set methods for the above. NO main method is required.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

How about some code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have tested and here is an example:

public static void main(String[] args) {
		BufferedReader lnr = null;
		String line = null;
		
                int N=1000;
		int totalRecords = 0;
		
                String [][] array = new String[N][];
		
                try {
			lnr = new BufferedReader (new FileReader("accountList.txt"));

			for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
	
				array[totalRecords] = line.split(" ");
				totalRecords++;
			}
			
			lnr.close();
		} catch (Exception e) {
			System.out.println("An error has occured: "+e.getMessage());
			//e.printStackTrace();
		}
		for (int i=0;i<totalRecords;i++) {
			for (int j=0;j<array[i].length;j++) {
				System.out.print(array[i][j]+" ");
			}
			System.out.println();
		}
	}

2-D arrays in java don't exist. Actually what you do is create a single array and each element has another array:

String [][] array = new String[N][]; //2-D array with N rows

array[totalRecords] = line.split(" "); // the row: [U]totalRecords[/U] will have the array returned by the split method.

So the array[totalRecords] has another array inside it and to access it you do:
array[totalRecords][0],
array[totalRecords][1],
array[totalRecords][2]

You initiall set the max row to be N=1000 because you don't know the size of the file. But you use the totalRecords variable to count the lines, because inside the loop you don't an int variable so you must do:
array[totalRecords] = ...
totalRecords++

in order to place the next line to the next row.

When you print the values you don't use the N at the first loop because not all rows of the array have value. You use the totalRecords. Remember you have used to count the …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

It was printed because it was the accountId of the second line of the file. You are the one who wrote the code that loops through each line of the file:

for (....) {

}

With the above code you loop each line and get the values from each line:
account[0], account[1], account[2], ....

Why would you have it any other way? What would be the point of getting only the first line and not the rest of the lines of the files?

Now if you want to store the entire file in a single array, then Yes, use a 2-D array:

int N=1000;
String [][] array = new String[N][]
....
....
int count=0;
for (....) {

array[count] = line.split(" ");
count++;
}

I don't know if the above will work. You might want to test it.
If it doesn't then declare the 2-D array: [N][5] and in the loop take the line in a different 1-D array and put the values in the 2-D array.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have tried this:

public static void main(String[] args) {
		BufferedReader lnr = null;
		String line = null;
		try {
			lnr = new BufferedReader (new FileReader("accountList.txt"));
			for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
	
				String account[] = line.split(" ");
				
				System.out.println(account[0]);
			}
			
			lnr.close();
		} catch (Exception e) {
			System.out.println("An error has occured: "+e.getMessage());
			//e.printStackTrace();
		}
	}

The file has these:

12345 1234 200000 Jerry Lopez
55555 1234 200000 Jerry Lopez

And it printed:

12345
55555

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = line.split(" ");

//print whatever you want
//and do whatever you want with the elements:
//account[0], account[1], account[2], account[3], account[4]

}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Look at your code:

for (line = lnr.readLine(); line!=null; line = lnr.readLine()){

String account[] = new String[4];
int count = 0;


int accountNumber[] = Integer.parseInt(account[count][1]);
int pinNumber[] = Integer.parseInt(account[count][2];
int balance[] = Integer.parseInt(account[count][3];
String name = account[count][4];


System.out.println(accountNumber[0][1]);
}

You read the line BUT inside the loop you don't do anything with it. Instead you call this: Integer.parseInt(account[count][1]); .
The account array is not 2-D and the Integer.parseInt does not return an array.


Now inside the line you have something like this:
12345 1234 200000 Jerry Lopez

So If you use the split method:

String [] array = line.split(" ");

The elements of the array would be:
0: 12345
1: 1234
2: 200000
3: Jerry
4: Lopez

Also for reading from a file try this shorter version:

BufferedReader lnr = new BufferedReader (new FileReader("accountList.txt"));
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When comparing Strings, almost at 99.99% of the cases, you will need to use the equals method:

String s1 = "aa";
String s2 = "aa";

System.out.println( s1.equals(s2) ); //it will print true
System.out.println( s1.equals("aa") ); //it will print true
System.out.println( "aa".equals(s2) ); //it will print true

System.out.println( s1==s2); //it will print false

The equals compares the values of the objects. 's1', 's2' are not the same objects but they have the same value. So use the equals.
When you use the '==' you compare the objects themselves. So like I said:'s1', 's2' are not the same objects so '==' will return false

BUT

String s1 = "aa";
String s2 = s1;
System.out.println( s1==s2); //it will print true

So when you compare Strings: next[0] == ID use : next[0].equals(ID)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Try to leave the GUI out of it at the beginning.

First write a class that will represent the data you will be saving.

Then in a separate class write methods that read and write to a file, a List (ArrayList, Vector) of DVDs.
Test the above in main method. Just create in the code some DVDs and save them to the file. Then read them and print them.
When all the operations needed are done, then create the GUI and call these methods.

That way it easier to proceed because you don't mix different things together. It would be difficult for someone that doesn't know much, to try to create a GUI and in the same class read and write to file.
With the above way you will be able to proceed step by step. If there is an error while reading you will be able to concentrate only to that and not worry about the rest the class

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I think you are contradicting yourself:

I didn't even necessarily ask you to do the entire thing

If anyone can do this I will spam +rep on your for the remainder of your DaniWeb life

it takes me too long to figure out how to fix all of my errors. And it would take even longer if I brought them here

Why would it take even longer to fix your errors if you brought them here? Everybody else does. We don't know what you have done so far and what approach you have selected. With the information you gave us we would have to write the thing from the begining.

As a suggestion create different classes for each of the accounts and One different class for creating them and using them

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

In Physics everything is measured in meters and seconds.
So after you get the speed and distance, Convert both to meters/sec and meters. After you do the division you will have a clean value in seconds which can be easily converted to minutes and hours.
If the final value has decimal points: 65.33 seconds then keep them at the seconds value or round them or have another variable for miliseconds:
1 minute and 5.33 seconds or
1 minute and 5 seconds or
1 minute and 5 seconds and 330 milliseconds

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Doesn't this answer your question?
http://www.daniweb.com/forums/thread156720.html

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you have already a page that does the login and the inserts to the database,
then you are familiar with writing HTML, submiting forms, and getting the parameters from the request.

If you have written nothing and you expect to get the entire code, you won't. If you are new to java then this is not for you, because this isn't just basic java.

It will require something more than a few lines of java code for anyone to send. We will need to see your code and what you have written, in order to understand what kind of changes are required in YOUR code and where to put them

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have a variable that counts how many times the password has been entered.
When the password is wrong increase its value and put its value to a hidden field so to get it at request when the form is submitted again the next time.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Also this is wrong:

WrongMenu(int n){
		System.out.println("you've entered : "+ n);
		System.out.println("please reenter: ");
	}

The Exceptions don't get created to print messages. With the above the message will be printed when you do: throw new WringMenu(1); Exceptions need to be caught and handled. Like this:

catch(WrongMenu e){
      System.out.println(e.getMessage());
}

When you this constructor:

WrongMenu(String s){
		super(s);
	}

You set the message to be 's' and when you call the getMessage() you print that argument.
So try something like this:

WrongMenu(int n){
         super("You've entered : "+ n+". Please reenter.");
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.next();
			s1 += s+" ";
                       // or s1 += s+"/n";
		
		}
		

		System.out.println("you've typed: \n");
		System.out.println(s1);

That is not what enuff4life has asked. Try this:

String s = new String("");
		String s1 = new String("");
		System.out.println("enter String : ");
		Scanner keyboard = new Scanner(System.in);
		
		while(!s.equals("end")){
			s = keyboard.nextLine();

                       if (!s.equals("end")) {
                             s1 += s + "\n"; 
                       }
		}
		System.out.println("you've typed: \n");
		System.out.println(s1);

Or keep the while-loop as it is and use the methods:
String.indexOf()
and
String.subString()

int index = s1.indexOf("end");
System.out.println(s1.subString(0,index));

Link for the String class.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Two for-loops

stultuske commented: why make it harder if it indeed is this easy? +3
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried the link I provided?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Here are some compiler errors: Sanner input = new Scanner(System.in) should be Scanner input = new Scanner(System.in) Also the <h> variable is never initialized so you will get an error.
Try doing h = 0, but then the result would be zero, since you never read it.

Also it is Math.PI with capital

Plus here is a link for the JDK:

http://www.java.com/en/download/index.jsp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

He is not a java programmer

Wow, and I thought that his code was written in Pascal

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I don't understand exactly what your method tries to accomplish but I believe that initializing sum with counter will get you some results, or/and returning the sum instead of zero at the else {...} :

public static int myMethod(int counter)
	{
		//int sum = 0;
		int sum = counter;

                if(counter == 0)
			return 0;
		else
	    {
		   sum = sum +  myMethod(counter-1);
		   System.out.println("Sum is " + sum);
		   
                  //return 0;
                  return sum;
		}
	}

I mean if you look at your return statements you always returned 0. So even if you did: sum = sum + myMethod(counter-1); in the end you returned 0 and sum was initialized to zero. So:
sum = 0 + myMethod() and myMethod returned zero again

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

No, reread what you/he wrote.

I'm not saying he was suggesting making it a public variable, just that in the context of his comment, he meant public variable.

Yes you are right. Sorry

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

No, he meant public variable. He/she was saying that the OP should be saying myCircle.getRadius() instead of myCircle.getRadius (notice the parentheses). In the latter case, you would need to declare a public variable named getRadius in the Circle class.

You could make this variable static as well (as stultuske said), however, static attributes are typically called with MyClass.staticAttribute rather than myClassInstance.staticAttribute . Regardless, the variable would still need to be public.

As stultuske replied (it is a method), when you get an error for this:
myCircle.getRadius
95% of cases it was supposed to be like this: myCircle.getRadius() and someone forgot the parenthesis,
instead of forgetting to declare the variable: getRadius public.

The right way to write java is declare variables:
radius as private
and use get methods.

Even though you can, it is not best practice to name public variables: getRadius. I mean: Yes, maybe the poster had actually a public variable getRadius (haven't seen the code) and I would be wrong.

But the right way and the most common way to write java is have:
private variableName and
public getVariableName()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You mean public variable.

No, stultuske means: public method.

If it was public variable it should be declared: public double radius = 0; Instead it is: getRadius. So when AceAtch writes: myCircle.getRadius ,
probably has something like this:

private double radius = 0;

public double getRadius() {
  return radius;
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post more code and more information

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I still don't get the poll.
What does it mean if I vote the first and what does it mean if I vote the second?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The argument: Human h is the one to be added in the ArrayList.
The way you have it. no matter what the argument is, you will always insert the same values:

Human a = new Adult("Joe",43);
        Human b = new Adult("Sue",39);
        Human c = new Adult("Tracy",20);
        Human d = new Child("Sammy",17);
        Human e = new Child("Julie",12);
        Human f = new Child("Mona",6);
        hm.add(a);hm.add(b);hm.add(c);hm.add(d);
        hm.add(e);hm.add(f);

It is the argument that you need to insert. Of course before adding it do whatever checks are necessary.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all this makes no sense:

int percentage = 0;
int frequency = 0;
percentage = frequency * 60000;

And second the above code is written were you declare variables, so you cannot put commands like this there: percentage = frequency * 60000;
This thing needs to go inside a method

Your brackets are OK, that was my mistake

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i only just registered and this was my first post.

I didn't say this was your 100th post, I just said that your tone was demanding in the way you asked for other people to do your homework, because that is what you asked:
"complete this code"

You didn't even explain what the code does as if we will read tons of lines of code and waste even more time to understand it

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
public class Translate {
public static void main(String [] args){
String filename = null;
if(args != null && args.length() > 0){
filename = args[0];
}
}
}

The parameter args is never null and you didn't explain how to pass that ars[0] argument in the code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you have studied from day one of the class and practice with simple programs you could do it.
Plus you attitude was not the best encountered here. Your tone was demanding, you just posted only the code with no explanations whatsoever as if it is our job to do other people's homework and why should we waste our time to complete this?

VernonDozier commented: Well said. +8
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
public static void main(String [] args) {

}

The (String [] args) is the argument of the main. When you call the java program with arguments at the command line those arguments are stored in the args array:


>java Translate arg1 arg2 "arg 3"

Now in your program the args array has values:
args[0] : arg1
args[1] : arg2
args[2] : arg 3

If you call the program with no arguments the array will have length zero
If you call it with 5 arguments it will have: args[0], args[1], args[2], args[3], args[4].
So use the args.length to check how many arguments the program has.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

percentage is an array and you do:
percentage = frequency * 60000;

Also I think you have a problem with your brackets (})

public class Assignment5 extends JApplet
{

....
....

// Multiply the frequency to 60000 to get the percentage
line 38	percentage = frequency * 60000;			
	
	public void init()
	{
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This is wrong:

public void showMovie() throws MoviePropertiesNotSetException
    { 
        movieNew.setName("Comedy");
        movieNew.setRating("G");
        movieNew.setTitle("Mr Bin");
        if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
        {
             System.out.println("Now Showing... "+movieNew.getTitle());             
        }
        else
        {
            throw new MoviePropertiesNotSetException ("The  movie properties are not set.");
        }            
    }

The movie is already set by the setMovie method. And here you are going and changing what it was entered with fixed values: (Comedy,G,MR Bin). All you need to do is check if the movie attribute was set:

public void showMovie() throws MoviePropertiesNotSetException
    { 
        if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null))
        {
             System.out.println("Now Showing... "+movieNew.getTitle());             
        }
        else
        {
            throw new MoviePropertiesNotSetException ("The  movie properties are not set.");
        }            
    }

You are making the same mistake with addMovieGoers. You are supposed to take the argument and add it to the ArrayList. The user calling the method will set which "humans" will "watch" the movie. And again you are using fixed values. It is as if the method has no meaning of existence since it makes no difference what the argument is since you don't use it