javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can't you convert what you read as String into a byte?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Read the file line by line.
When the line you have read is equals to: START_IMAGE, then the next lines will be the image. Then when the line read becomes equals to: END_IMAGE then exit the while loop that you had for reading the file:

String line = readLine();
boolean image=false;
while (line!=null) {

if (line.equals("END_IMAGE")) {
  break;
}

if (image) {
 //then the line has info about the image
}

if (line.equals("START_IMAGE")) {
  image=true;
  //the next line will be the image
}

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

I think your professor simply wants you to catch what may potentially generate the indexOutOfBounds exception and handle it by making the number accessing the indice set to some value (for example, if the number is constantly incrementing, in the catch block set the number to zero, if decrementing set it to the array length - 1 ).

Either way, this is a poor way of doing it though it may please your unreasonable professor.

That is no way to teach someone exceptions or proper java and I think that we both agree on that.
If you want to teach someone how to use exception, you use the NombarFormatException, not ArrayIndexOutOfBounds. What's the difference. One happens at runtime and cannot be avoided, so that is why you catch it and the other is not suppose to happen. If it happens then the code is wrong.

Suppose that you have a program that accepts 2 inputs and it must add them. The main has Strings as arguments: main (String [] args) . So since you don't what the user might enter you use the NombarFormatException.
But with ArrayIndexOutOfBounds you are supposed to check the index of the array. It will be the programmer's mistake to have such an exception thrown. I doubt that there is any case where using ArrayIndexOutOfBounds is better than doing something else. And NO ONE will ever write something like this:

try {
  for (int i=0;i<10;i++) {
    System.out.println(array[i]);
  }
} catch (ArrayIndexOutOfBounds e) …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This:

FileReader inputStream = null;
			 PrintWriter outfile = null;
			 
			 try
			 {
			 
          FileReader inputStream = new FileReader("text.txt");
          PrintWriter outfile =
                  new PrintWriter(new FileWriter("textCh.out"));
..
..
}

Should be:

FileReader inputStream = null;
PrintWriter outfile = null;
			 try
			 {
          inputStream = new FileReader("text.txt");
          outfile = new PrintWriter(new FileWriter("textCh.out"));
..
..
}

The 3 methods writeTotal, chCount, copyText should be declared outside the main method. Meaning that you have a small problem with the parenthesis. You forgot to put one:
Add it there:


System.out.println(e.getMessage()) ;
}

} this one closes main

static void copyText(FileReader infile, PrintWriter outfile,


Also I don't know what the IntClass does? But try to fix the above first and see what happens. I think that it will work. Sometimes one misplaced '}' can give many errors

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

post you final code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have to create a program that reads the text, outputs the text as is and prints the number of lines and the number of times each letter appears in the text. I have to include an exception, so that if the array index goes out of bounds when the program accesses the array letterCount, it throws and handles the ArrayIndexOutOfBoundsException. I have the program completed and working , but I'm having trouble adding the exception to the program. Here is my code:

Hi it's me again. Sorry for not checking what you wrote, I was only looking at the code you gave. It seems that I suggested not to use ArrayIndexOutOfBoundsException even though that is what you wanted.
Who told you to use this exception handling? Because what I suggested was generally correct: you don't want to catch such exception, but instead make sure it doesn't happen by checking the length of the array.
Anyway if you want ArrayIndexOutOfBoundsException here it is.:

static void chCount(char ch, int[] letterC) throws ArrayIndexOutOfBoundsException {
          int index;
          int i;

          ch = Character.toUpperCase(ch);   //Step a
          index = (int) ch - 65;            //Step b
          if (index >= 0 && index < 26)      //Step c
            letterC[index]++;
    }

    static void writeTotal(PrintWriter outfile, int lines,
                           int[] letters)
    throws ArrayIndexOutOfBoundsException  {
          int i;

          outfile.println();
          outfile.println("The number of lines = " + lines);

          for (i = 0; i < 26; i++)
              outfile.println((char)(i + 65) + " count = "
                             + letters[i]);
    }

static void copyText(FileReader infile, PrintWriter …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And something that I forgot:
NEVER, NEVER "hard code" the length of an array when you access it :

This:

for (i = 0; i < 26; i++)
              outfile.println((char)(i + 65) + " count = "
                             + letters[i]);

Should become this:

for (i = 0; i < letters.length ; i++)
              outfile.println((char)(i + 65) + " count = "
                             + letters[i]);

And this:

if (index >= 0 && index < 26)      //Step c
            letterC[index]++;

To this:

if (index >= 0 && index < letters.length)      //Step c
            letterC[index]++;
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
IntClass next = new IntClass();

          FileReader inputStream = new FileReader("text.txt");
          PrintWriter outfile =
                  new PrintWriter(new FileWriter("textCh.out"));

          next.setNum(inputStream.read());

          while (next.getNum() != -1)
          {
              copyText(inputStream, outfile, next, letterCount);
              lineCount++;
              next.setNum(inputStream.read());
          }  //end while loop

          writeTotal(outfile, lineCount, letterCount);

          outfile.close();

The above code needs to be altered like this:

IntClass next = new IntClass();
FileReader inputStream = null;
PrintWriter outfile = null;

try {
           inputStream = new FileReader("text.txt");
           outfile = new PrintWriter(new FileWriter("textCh.out"));

          next.setNum(inputStream.read());

          while (next.getNum() != -1)
          {
              copyText(inputStream, outfile, next, letterCount);
              lineCount++;
              next.setNum(inputStream.read());
          }  //end while loop

          writeTotal(outfile, lineCount, letterCount);

          outfile.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all, this:
int[] letterCount = new int[26] ;
does not throw an exception.
Second: Let me guess your program does not compile. This is why:

try
			 {
          int[] letterCount = new int[26];
			 }
			 catch (ArrayIndexOutOfBoundsException e)
			 {
			 System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
			 e.printStackTrace();
			 }

You declare letterCount inside the try { ... }. Meaning that outside try{...} no one can "see" it. So when you try to use it at the while loop you will get an error. If you have to give value to a variable inside the try{...} but you will have to use it outside as well then:

int[] letterCount = null;
try
			 {
          letterCount = new int[26];
			 }
			 catch (ArrayIndexOutOfBoundsException e)
			 {
			 System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
			 e.printStackTrace();
			 }

You declare letterCount outside try{} so anyone can see it.
By the way the above is not necessary, since the declaration does not throw an ArrayIndexOutOfBoundsException, unless you use negative value. And it is not very correct to catch such exceptions. It is up to the programmer to check in the code and make sure that you will never try to access an element of an array with an index greater than the length.

//try {
          int[] letterCount = new int[26];
/*
			 } catch (ArrayIndexOutOfBoundsException e) {
			 System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
			 e.printStackTrace();
			 }
*/

Try it without the try-catch.

As for you question, remove the throws Exception, from the main. You use these at method declarations in order to …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Just a few tips:

Don't use:

} catch(Exception e)
   System.out.println(e);
}

The above will simply print the .toString() method of the Exception. Use e.getMessage() to print the Exception message to the user.

System.out.println(e.getMessage());

Also for debugging reasons you can e.printStackTrace() .
Assume you have method A() . And inside it you have B1() , B2() , B3() . And each one of them might throw an exception. If when you call A() , you get an exception how will you know which of the above methods (B1, B2, B3), threw that exception? Use 3 different try-catch? No, too much effort and it will not work in most cases. If you write this:

try {
  A();
} catch (Exception e) { 
  e.printStackTrace();
}

The call hierarchy of methods called will be printed. Meaning that it will be printed the method that invoked the exception and which method called that method, and which method called that and so on. If you have too many methods calling others, this is the way to find out where the error happened. As I said it is usually used for debugging. For the programmer to find what went wrong wrong when testing the program. Once you get it to work, you can use the e.getMessage() . It is not pretty for the user to see the stack trace of the exception.

class TestStackTrace {

public static void main(String [] args) {
  try {
     A();
  } catch (Exception e) {
    System.out.println("Exception …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The
int numberValue = Integer.parseInt(number) ;
does not throw a IOException. You are right to use it in the catch since readLine() does throw the IOException, but the parseInt() throws a NumberFormatException:

/**
 * @(#)GetUserInput.java
 *
 *
 * @author 
 * @version 1.00 2008/8/2
 */
import java.io.*;

public class GetUserInput 
{
         private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

        
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {

     
            System.out.print( "Type some data for the program: " );


            // Read a line of text from the user.
            String input = stdin.readLine();
		


            // Display the input back to the user.
            System.out.println( "input = " + input );
            
            System.out.print( "Type a number for the program: " );

	
            // Read a line of text from the user.
            // then convert it to int but print error message if it goes wrong
             try
	    {
		String number = stdin.readLine();
	    int numberValue = Integer.parseInt(number);	    
            
            // Display the input back to the user.
            System.out.println( "Number Inputted = " + numberValue );
	
	    }
            // i want it to print the error message if if cant convert the string to an int
		catch ( IOException e )
	   {
	System.out.println(e);
      		System.out.println("Error while reading input: " + e.getMessage());
           }
catch ( NumberFormatException e )
	   {
	System.out.println(e);
      		System.out.println("Given input not a number: "+e.getMessage());
           }
   
    }
}
Alex Edwards commented: Helpful individual +3
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i have to do mini project on online share trading system... if have coding mail me....

Are serious?
Read jwenting's previous post.
And even if you started a new thread, why should we send you our code?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Since they are interfaces, you can implement more than 1:

public class MyFrame extends JFrame implements ActionListener, ItemListener {

}

Don't forget to add the methods that these interfaces have. Also to add the "listeners" to the components you are interested (buttons)

Alex Edwards commented: Credit is given where credit is due +3
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I did, but that still does not change my opinion.

If you want we can go on with PMs but should stay on topic here

No, it would be better to drop it before we are accused of spamming.

And this is for raj1108:
When you post your code with specific question, we will be happy to help.
But you must show some effort. Why do you expect us to do all the work for you?
If you have a project we will give you suggestions on how to proceed.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

@javaAddict - aren't there any younger better looking types then her that you would like to date?

Obviously you have never watched Baywatch

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

So not only you want project titles, (an issue that is discussed many times in this forum) but also you want the solution.
If I ask for a date with Pamela Anderson, you think that anyone will reply? :icon_smile:

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

It is not very efficient reading from ResultSet while generating the html.
Better to have separate class that returns as an array of objects the data you want and then use that data to generate the page.
And the class will be responsible of handling the opening and closing of the ResultSet as well as the connection.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can use the Locale to parse a certain Date object:

SimpleDateFormat.html

And use the constructor with the Locale as argument.

SimpleDateFormat(/*String pattern*/ "dd/MM/yyyy",  Locale.ENGLISH)

But I don't think that what you are asking is possible.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

main manu

1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division
5 for Choice not in list

You have posted the same question to a different thread:
http://www.daniweb.com/forums/post661955.html#post661955

When you were asked from sciwizeh to provide more information, why didn't reply, but started a new one?


Any way, please provide with information if it is going to be GUI or keyboard input, so for us to guide you to the right direction.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Probably he wants a menu with a while loop where the user can enter from the keyboard a number and a date several times

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Put all the averages in a new array. Then write two methods that accept an array as input and return the greater and the lower value.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Once you have the necessary information for a specific student:

double avg = (student[i][0] + student[i][1] + student[i][2])/3.0
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Instead of getting the last two values, take the first two. And don't forget that sorting the priceArray, doesn't sort the itemArray. And if you sort the itemArray, it will be sorted based on the values itemArray has, and the mapping between names and prices will be mixed up.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i tried using

import java.util.Arrays;

so as to use the method Arrays.sort(); but it only shows the highest instead, i would like the 2 lowest value plaease.

What do you mean the highest?
The Arrays.sort(int[] a) sorts the array. It's up to you to take the values you want

Edit: I would suggest to read my previous post

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Arrays class with its sort() methods is all you need

But if he sorts priceArray then the itemArray will also have to be sorted the same way.
It would be better to use the old fashion way to sort priceArray and do equivalent swapping to itemArray.
Or
Create an object with two attributes:
price, foodName:

class Food {
private String foodName;
private double price;

//add get, set methods OR make the above variables public
}

Then use the interface: Comparable:

class Food implements Comparable{
// code

public int compareTo(Food f) {

}
}

Comparable

Objects that implement Comparable can be used as arguments at the Arrays.sort() method

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know that the array will already have the values sorted? From what I see:
double[] priceArray = { 21.00, 17.00, 12.00, 10.00, 4.90, 4.90, 1.80, 1.80 } ;
The array is already sorted, so you just need to take the 2 last values.

If this is not the case then writing:

System.out.println("Most Economical Food:\t" + (loworderNo-1) +"\t\t"+ item[[B]loworderNo-1[/B]] + "\t" + lowest);
System.out.println("Most Economical Food:\t" + loworderNo +"\t\t"+ item[loworderNo] + "\t" + lowest);
is not quite correct, because since the array will not be sorted you don't know if the lowest prices will be next to each other.
Plus if the array doesn't have 2 lowest prices but 1 you will have an ArrayIndexOutOfBoundsException.
Check before you access an array if the values is in bounds. You are sure for variable: loworderNo but you don't know 100% that ( loworderNo - 1 ) will be OK.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Another version using YOUR do while loop is this:

for (int i=0;i<10;i++) {

do{
 prelim=JOptionPane.showInputDialog("Student Prelim["+i+"]");
 midterm=JOptionPane.showInputDialog("Student Midterm["+i+"]");
 finals=JOptionPane.showInputDialog("Student Finals["+i+"]");

  boolean bPrelim =(prelim<50) || (prelim>100) ;
  boolean bMidterm =(midterm<50) || (midterm>100) ; 
  boolean bFinals =(finals<50) || (finals>100) ;

  if (bPrelim ) {
    //print the appropriate error message
  }
  if (bMidterm) {
    //print the appropriate error message
  }
  if (bMidterm) {
    //print the appropriate error message
  }

  student[i][0]=Integer.parseInt(prelim);
  student[i][1]=Integer.parseInt(midterm);
  student[i][2]=Integer.parseInt(finals);  

}  while( bPrelim || bMidterm || bMidterm);

}

If any of the boolean variables are true, that means that the values are wrong. So the while will repeat and ask for the values again. The <i> will not chang since you are inside the same for loop. If all the boolean values are false, means that all the values are correct and the while will be false, so you will continue with the next for loop.

You could also try this:

for (int i=0;i<10;i++) {

do{
 prelim=JOptionPane.showInputDialog("Student Prelim["+i+"]");
 midterm=JOptionPane.showInputDialog("Student Midterm["+i+"]");
 finals=JOptionPane.showInputDialog("Student Finals["+i+"]");

  boolean bPrelim =(prelim<50) || (prelim>100) ;
  boolean bMidterm =(midterm<50) || (midterm>100) ; 
  boolean bFinals =(finals<50) || (finals>100) ;

  if (bPrelim ) {
    //print the appropriate error message
  }
  if (bMidterm) {
    //print the appropriate error message
  }
  if (bMidterm) {
    //print the appropriate error message
  }
}  while( bPrelim || bMidterm || bMidterm);

  student[i][0]=Integer.parseInt(prelim);
  student[i][1]=Integer.parseInt(midterm);
  student[i][2]=Integer.parseInt(finals);  
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
for(int rw=0; rw<10; rw++){
for(int col=0; col<3; col++){
do{
grad=JOptionPane.showInputDialog("Student Prelim["+mj+"]");
student[rw][col]=Integer.parseInt(grad);
if(student[rw][col]<50||student[rw][col]>100){
JOptionPane.showMessageDialog(null,"Inputed Grade of student is not allowed!!!");
}
grad=JOptionPane.showInputDialog("Student Midterm["+mj+"]");
student[rw][col]=Integer.parseInt(grad);
if(student[rw][col]<50||student[rw][col]>100){
JOptionPane.showMessageDialog(null,"Inputed Grade of student is not allowed!!!");
}
grad=JOptionPane.showInputDialog("Student Finals["+mj+"]");
student[rw][col]=Integer.parseInt(grad);
if(student[rw][col]<50||student[rw][col]>100){
JOptionPane.showMessageDialog(null,"Inputed Grade of student is not allowed!!!");
}
}while(student[rw][col]<50||student[rw][col]>100);
mj++;

Let's talk about your code:

You don't need 2 for loops. This is what you wrote:

grad=JOptionPane.showInputDialog("Student Prelim["+mj+"]");
student[rw][col]=Integer.parseInt(grad);
grad=JOptionPane.showInputDialog("Student Midterm["+mj+"]");
student[rw][col]=Integer.parseInt(grad);
grad=JOptionPane.showInputDialog("Student Finals["+mj+"]");
student[rw][col]=Integer.parseInt(grad);

You will put 3 different values:
1) JOptionPane.showInputDialog("Student Prelim["+mj+"]") ;
2) JOptionPane.showInputDialog("Student Midterm["+mj+"]")
3) grad=JOptionPane.showInputDialog("Student Finals["+mj+"]")
At the same location: student[rw][col]

You might want to try this:

for (int i=0;i<10;i++) {

grad=JOptionPane.showInputDialog("Student Prelim["+i+"]");
student[i][0]=Integer.parseInt(grad);

grad=JOptionPane.showInputDialog("Student Midterm["+i+"]");
student[i][1]=Integer.parseInt(grad);

grad=JOptionPane.showInputDialog("Student Finals["+i+"]");
student[i][2]=Integer.parseInt(grad);
}

Assuming that at the location:
[0] there will be the Preilim
[1] there will be the Midterm
[2] there will be the Finals

As for the check I would suggest first to take all the values, check them all and then put them in the array:


for (int i=0;i<10;i++) {

boolean ok = false;
while (!ok) {
 prelim=JOptionPane.showInputDialog("Student Prelim["+i+"]");
 midterm=JOptionPane.showInputDialog("Student Midterm["+i+"]");
 finals=JOptionPane.showInputDialog("Student Finals["+i+"]");

 if (/* check the above values. if they are ok*/) {
  ok=true;

  //put them in the array as showed above
  }

 }

}

The first time the check will be made, ok will be false and you will go in the while. If the values are valid, ok will be turned into true. So the while statement (!ok) will be …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know which part of the code needs to be replaced with JOptionPane? If you don't that probably means that you didn't write it. Tell us what you don't understand and we will help, but no one is going to email you the solution

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Instead of "Student.txt" you could have a String given from input. If you try a String that is not a file then you will get that error.

Even if you don't get that error, you don't know what might happen. I mean since there is a chance that: new PrintWriter( ... ) would throw an Exception, you should make that check in the finally.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you have never done java then why should you get that position?
And if you are looking for the entire code we are not going to help you. Besides we don't know if it swing or jsp, or if you want to use javascript to do the validation at the client.
All we can tell you is that when the user submits the input,
take it,
put it in String,
convert it into a number,
check if the number is negative or not

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

here this worked for me

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";
		PrintWriter out = null;
		try {
			out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

EDIT:
javaAddict just beat me to it :(

I would suggest you put this in finally:

if (out!=null) out.close();

If the student.txt file does not exist then an exception will be thrown without PrintWriter taking value. So in the finally you will have a NullPointerException.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

No you need to have a finally because even if an exception occurs you will still need to close the PrintWriter. The error happens because you declare PrintWriter inside try {....} and in the finally it is out of scope, it cannot be used outside try because this is where it declared. Try this:

import java.io.*;
import java.util.*;

public class Streams {

	public static void main(String[] args) {
		String studentFirstName = "Bob ";
		String studentLastName = " Smith ";
		String finalGrade = "A";

		PrintWriter out = null;

		try {
			out = new PrintWriter(new FileOutputStream("Student.txt"));
			out.print(studentFirstName);
			out.println(studentLastName);
			out.print(finalGrade);
							
		}
		catch (IOException e) 	{
			System.out.println(e);
		}
		finally {
			if (out!=null) out.close(); //in case you have an exception before  PrintWriter takes value
		}
	}
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And where do you get the error, because at line 111 I see only:

public static void main(String[]args)

And besides if you look at the code:

public void actionPerformed(ActionEvent e)
{
 if((JButton)e.getSource() == j)
 {
 /*jc1 = new JColorChooser();
 JColorChooser.showDialog(jp4,"Select The Color", Color.red);
 c2=jc1.getColor();*/
 p1.repaint();
 }
}

You have most of it comment it out, meaning that nothing will happen when you click the button.
By the way, the repaint() method, which I believe belongs to the class:
f p1 = new f();
You didn't post code for that class, and it is the only thing you call inside the actionPerformed method

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Math.random()

The above returns a random number between 0 and 1

a pseudorandom double greater than or equal to 0.0 and less than 1.0.

Math.random()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all we cannot count 111 lines. Post the part of the code that is around line 111, stating which one is line 111.

Also there is a symbol: '#' above this posting form. Press it. Then in the first tag add this:
=JAVA, no empty spaces.
Then post your code between these tags

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And no one is going to read you entire code, to find one single mistake, when you can simply post the line and the message you get from the console.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

At the line where you get the NullPointerException you are probably trying to use an object that is null and instantiated. See what the message says and you will find which object is that. Then instantiated correctly based on what is the object

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

investment duration interest rates offered

Less then 3 months 5%
3month-11 months 5.5%
1 year-23 months 7.6%
2 years and above 7.9%

Note: Interest = Total investment *Interest rates *Duration


Q: How to write a java application to compute the total accumulated investment after the investment periods?

The inputs of the program will be: duration and Total investment
Use if statements to compute the Interest rates based on the duration given. (use the above table)
Now that you all three values of the variables that are at left side of the formula:
Interest = Total investment *Interest rates *Duration, you can calculate the Interest

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

JTextField jtfVGap = new JTextField(40)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

hi again >>>

this web site 4 IT descussion >>


that mean if any 1 have any problem >>> u have to help him ...

thank u @ll for help >>>

i promis @ll of u to be a member in this web ,,
and i'm sorry 4 any noise ..

If you read the entire thread you will see that many people were willing to help him/her. We adviced him to post his code and tell what was his problem. But instead he kept posting the code for problems he had solved and worked OK, and he kept asking for help for problems he didn't solve without posting code for them to show effort.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Amazing, This code is really helpful.

It can be found in any book and tutorial. And I wrote this a long time ago, meaning that there are better versions of that

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Two arrays length 10. In one you will put the squares and at the other the cubes.
Use a for loop from 0 to 10. Calculate inside the loop theresults and put them in the array:

square[i] = i*i;
cube[i] = i*i*i;
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Inside a class declare the variable:

Connection conn=null;

In the same class:

Method for opening connection. Uses the myssql driver. download the jar and add it to the jars of your project

protected void openConnection() throws Exception {
        if ((conn==null)||(conn.isClosed())) {

String ip = "10.23.14.5"; //the ip of the database, in your case it would be localhost
String db_name="name_of_the_DB";
String user="username"; //the username that you use to login to the database
String pass="password"; //the password you use to login to the datatabase
 
            Class.forName("com.mysql.jdbc.Driver").newInstance();            conn=DriverManager.getConnection("jdbc:mysql://"+ip+"/"+db_name,user,pass);
        }
    }

Closing connection:

protected void closeConnection() throws Exception {
        if ((conn!=null)&&(!conn.isClosed()))
            conn.close();
    }

Use the above methods to open the conn and use it to run queries:

public boolean validatePassword(String user, String pass) throws Exception {
        this.openConnection();
        
        String query="select username, password from users where username='"+user+"' and password='"+pass+"'";
        Statement st=null;
        ResultSet rs=null;
        String u=null;
String p=null;
        
        try {
            st=conn.createStatement();
            rs=st.executeQuery(query);

                if (rs.next()) {
                    u=rs.getString(1);
p=rs.getString(2);
                }
                rs.close();
                rs=null;
                st.close();
                st=null;
this.closeConnection();
return ((u.equals(user))&&(p.equals(pass)));
        } catch (Exception e) {
            System.out.println(e.getMessage());
this.closeConnection();
        }
        return false;
    }
Alex Edwards commented: Very nice post! +2
peter_budo commented: Nice post. +9
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The code will be the same. It doesn't matter for what you are going to use it. I will post the code soon, after I finish some other issues I have.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you have solved a problem and you run it and it works then don't post it. Post only those that you are having problems with.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Create a method to calculate the value of the entire inventory.

It is the classic algorithm: sum += sum+VALUE. Write a method the takes as argument the array of DVDs and inside a for loop calculate the sum. As VALUE you should use:
dvd.value()

Create another method to sort the array items by the name of the product

Create a method that takes as argument an array of DVD and returns another array of DVD.
Surely you must know some basic sorting algorithms. Use the value: dvd.getDvdTitle() to sort the array, but don't forget that you will need to put the entire object in the new array not just the dvd.getDvdTitle()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

extend the char ,make it a object

Cannot extend char (Character class). It is final.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post only the code for the: FGInventoryProgram1.java and mark somehow line: 142 where you get the error. Don't expect us to count 142 lines to find where the errors.

And in general:
Keep only the DVD class and the FGInventoryProgram1 class. The other code is not necessary. The above classes should be in 2 different files.
If you have only those two classes your program should work. Try to make these two work before implementing the rest of my suggestions:

The code in FGInventoryProgram1 is correct from a certain point of view, but it is not what OOP is all about. Each time that you create a "new DVD(.....)" you "delete" the values of the previous. At the end of your program dvd has these values: dvd = new DVD ("Sweet Home Alabama",8,18.56,578)
Of course for what you are asked this is not wrong, but later you might you want to write a program where you want to access some of the previous created "dvds".

Again here is the same example:

DVD [] dvds = new DVD[5];
//dvds is an array, it is NOT null, but its elements: dvds[0], dvds[1], dvds[2], ... are null

//Its elements (dvds[0], dvds[1], dvds[2], ...) are DVD objects: 
dvds[0] = new DVD("How to Loose a Guy in 10 days", 4, 19.99, 685);
dvds[1] = new . . . . . . .
//you instantiated them 

for (int i=0;i<dvds.length;i++) {  //dvds is an array so you can use .length …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

And what is your question?
From a first look it seams that the program works OK.
Are you having any problems or the results you receive are not correct?
I would suggest adding some checks for the inputs:

if (args.length<10) {
System.out.println("Not enough arguments");
System.exit(1);
}

or code for checking if the votes given are positive numbers