469,341 Members | 8,051 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,341 developers. It's quick & easy.

Köll & Sam Classroom Blog

Dököll
2,364 Expert 2GB
Hello Dudes and Dudettes!

I am excited to announce I am embarking on a new Journey, JAVA baby. This is a new arena for me, hope I survive and able to muscle through it.

Will post articles within three months from now.

Wish me luck!

Köll
Sep 5 '07
216 12242
JosAH
11,448 Expert 8TB
ps. also have a look of the JFormattedTextField; it takes care of most
those nasty mechanics.

kind regards,

Jos
Mar 6 '08 #151
SammyB
807 Expert 512MB
ps. also have a look of the JFormattedTextField; it takes care of most
those nasty mechanics.

kind regards,

Jos
Yes, I looked at it after I finished my little demo, but it allows you to enter characters outside of its preferred set and then rejects them at the end as opposed to my class which doesn't even allow you to enter non-numeric characters. However, JFormattedTextField would have been a better base class to use instead of JTextField. I'll leave that as an exercise for the reader. ;o)>>> Sam
Mar 6 '08 #152
JosAH
11,448 Expert 8TB
btw, for a not-so-large range of integers I normally use a JSpinner; I like it
to force the user into correct behaviour ;-)

kind regards,

Jos
Mar 6 '08 #153
Dököll
2,364 Expert 2GB
I think I may have something here guys, I've been putting it down and getting back into it to free my mind. Needs more work, I seem to be looping fine but I move on to next InputDialog even after data were found to be invalid. I do get the InputBox again to re-enter but only after I have gone through the others. I may have to go away and come back again, but what do you see happening:

// Gets three input values--loan amount, interest rate, and
// loan period

Expand|Select|Wrap|Line Numbers
  1.   private void getInput() {
  2.  
  3.  
  4.       double  loanAmount = 0, annualInterestRate = 0;
  5.       int     loanPeriod = 0;
  6.       String  inputStr; 
  7.  
  8.  
  9.       //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
  10.       while (loanAmount < 100 || loanAmount > 1000000 ) {
  11.           inputStr  = JOptionPane.showInputDialog(null, "Loan Amount (Dollars + Cents):");
  12.  
  13.           try {
  14.               loanAmount = Double.parseDouble(inputStr);
  15.               // catch the user's entry here and throw an error
  16.               //User will need to re-enter data to continue
  17.               if (loanAmount < 100 || loanAmount > 1000000 ) {
  18.                   throw new Exception("Loan Amount is invalid, please try again...");
  19.               }
  20.  
  21.              //Catch instances were user either adds in vowels or leaves pop up empty
  22.              //Our user will need to re-enter information but the correct data type, digits only...
  23.           } catch (NumberFormatException e) {
  24.  
  25.               JOptionPane.showMessageDialog(null, "'" + inputStr
  26.                                       + "' is an invalid data type\n"
  27.                                       + "Please enter digits only");
  28.               //Catch instances were user either adds in vowels or leaves pop up empty
  29.               //Our user will need to re-enter information but the correct data type, digits only...
  30.           } catch (Exception e) {
  31.  
  32.               JOptionPane.showMessageDialog(null, "Error: "
  33.                                               + e.getMessage());             
  34.  
  35.           }
  36.  
  37.       while (annualInterestRate < 5 || annualInterestRate > 9 ) {
  38.           inputStr  = JOptionPane.showInputDialog(null, "Annual InterestRate (9.5 %):");
  39.  
  40.           try {
  41.               annualInterestRate = Double.parseDouble(inputStr);
  42.               // catch the user's entry here and throw an error
  43.               //User will need to re-enter data to continue
  44.               if (annualInterestRate < 5 || annualInterestRate > 9 ) {
  45.                   throw new Exception("Loan Amount is invalid, please try again...");
  46.               }
  47.  
  48.              //Catch instances were user either adds in vowels or leaves pop up empty
  49.              //Our user will need to re-enter information but the correct data type, digits only...
  50.           } catch (NumberFormatException b) {
  51.  
  52.               JOptionPane.showMessageDialog(null, "'" + inputStr
  53.                                       + "' is an invalid data type\n"
  54.                                       + "Please enter digits only");
  55.               //Catch instances were user either adds in vowels or leaves pop up empty
  56.               //Our user will need to re-enter information but the correct data type, digits only...
  57.           } catch (Exception b) {
  58.  
  59.               JOptionPane.showMessageDialog(null, "Error: "
  60.                                               + b.getMessage());             
  61.  
  62.  
  63.           }
  64.  
  65.  
  66.       while (loanPeriod < 1 || loanPeriod > 30 ) {
  67.           inputStr  = JOptionPane.showInputDialog(null, "Loan Period (Number of years):");
  68.  
  69.           try {
  70.               loanPeriod = Integer.parseInt(inputStr);
  71.               // catch the user's entry here and throw an error
  72.               //User will need to re-enter data to continue
  73.               if (loanPeriod < 1 || loanPeriod > 30 ) {
  74.                   throw new Exception("Loan Period is invalid, please try again...");
  75.               }
  76.  
  77.              //Catch instances were user either adds in vowels or leaves pop up empty
  78.              //Our user will need to re-enter information but the correct data type, digits only...
  79.           } catch (NumberFormatException i) {
  80.  
  81.               JOptionPane.showMessageDialog(null, "'" + inputStr
  82.                                       + "' is an invalid data type\n"
  83.                                       + "Please enter digits only");
  84.               //Catch instances were user either adds in vowels or leaves pop up empty
  85.               //Our user will need to re-enter information but the correct data type, digits only...
  86.           } catch (Exception i) {
  87.  
  88.               JOptionPane.showMessageDialog(null, "Error: "
  89.                                               + i.getMessage());             
  90.  
  91.  
  92.                        }
  93.  
  94.                }
  95.  
  96.         }
  97.  
  98. }
  99.  
  100.  
I guess it is okay to loop through other InputDialog boxes to again arrive at the original InputDialog that errored, then re-enter the data. It'd be cool to get it to just go right back to the loanAmount Dialog after data were found to be incorrect, then move on to next Dialog, it's possible right!

Almost forgot to mention, I am also trying to salvage ideas from your code to see if that remedies the issue...

Thanks for posting:-)
Mar 27 '08 #154
Dököll
2,364 Expert 2GB
I still cannot shake it guys, I am trying to have my InputDialog to loop back to me so I can re-enter, and only after that part is handled, errors checked should I move on to the next Input
Expand|Select|Wrap|Line Numbers
  1.  
  2.       //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
  3.       while (loanAmount < 100 || loanAmount > 1000000 ) {
  4.           inputStr  = JOptionPane.showInputDialog(null, "Loan Amount (Dollars + Cents):");
  5.  
  6.           try {
  7.               loanAmount = Double.parseDouble(inputStr);
  8.               // catch the user's entry here and throw an error
  9.               //User will need to re-enter data to continue
  10.               if (loanAmount < 100 || loanAmount > 1000000 ) {
  11.                   throw new Exception("Loan Amount is invalid, please try again...");
  12.               }
  13.  
  14.              //Catch instances were user either adds in vowels or leaves pop up empty
  15.              //Our user will need to re-enter information but the correct data type, digits only...
  16.           } catch (NumberFormatException e) {
  17.  
  18.               JOptionPane.showMessageDialog(null, "'" + inputStr
  19.                                       + "' is an invalid data type\n"
  20.                                       + "Please enter digits only");
  21.               //Catch instances were user either adds in vowels or leaves pop up empty
  22.               //Our user will need to re-enter information but the correct data type, digits only...
  23.           } catch (Exception e) {
  24.  
  25.               JOptionPane.showMessageDialog(null, "Error: "
  26.                                               + e.getMessage());             
  27.  
  28.           }
  29.  
  30.  
Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)

Thanks much!

Dököll
Apr 3 '08 #155
Dököll
2,364 Expert 2GB
http://java.sun.com/j2se/1.4.2/docs/...TextField.html
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class NumericTest extends JApplet
  4. {
  5.    public void init()
  6.    {
  7.       JPanel p = new JPanel();
  8.       p.setLayout(new GridLayout(1,2));
  9.       JLabel lbl = new JLabel("Enter Number:");
  10.       NumericField tb = new NumericField(1);
  11.       p.setBackground(Color.LIGHT_GRAY);
  12.       p.add(lbl);
  13.       p.add(tb);
  14.       this.getContentPane().add(p);
  15.       this.setSize(200, 40);
  16.    }
  17. }
  18.  
Freaking fantastic, Sam!

I can work with this. Please disregard my previous post, actually still throw your hat in, why not. Let's see what other option there are:-)

But thanks for this piece of code.

In a bit!

Köll
Apr 3 '08 #156
SammyB
807 Expert 512MB
Freaking fantastic, Sam!
Köll
About time that you liked it! ;o)>>>

Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.
Apr 4 '08 #157
Dököll
2,364 Expert 2GB
About time that you liked it! ;o)>>>

Java class is all over for me. Squeeked by with a 106 average! LOL! Now, I'm taking an intro to XML. It's taught by the same teacher as the Java course, but it's all on-line and should be much less work.
106 average! LOL! a definite WOW to me, good job... You can now relax... We have six more labs about and one final:-)

Thanks again!
Apr 4 '08 #158
JosAH
11,448 Expert 8TB
Now, I'm taking an intro to XML. It's taught by the same
teacher as the Java course, but it's all on-line and should be much less work.
Famous last words ...

kind regards,

Jos ;-)
Apr 4 '08 #159
SammyB
807 Expert 512MB
Famous last words ...

kind regards,

Jos ;-)
Just in case you didn't see it: If a programming language was a boat.. That Java cargo ship looks just like my final project!
Apr 4 '08 #160
JosAH
11,448 Expert 8TB
Just in case you didn't see it: If a programming language was a boat.. That Java cargo ship looks just like my final project!
If Java were a boat this would'be been the slick, smooth development experience.

kind regards,

Jos ;-)
Apr 5 '08 #161
Dököll
2,364 Expert 2GB
Looks like my only option thus far is go through all InputDialogs. Can anyone give me an idea how to finish checking loanAmount for valid data and if invalid to loop back to its InputDialog to re-enter stuff, pheew that was a long one:-)

Thanks much!

Dököll
Please disregard this one; rahter than ending the loop at the end of the class, ending it as an item by itself allows the loop to continue and demands for legal data. I think I was trying to envelope all loops together, together this time did not work:

Expand|Select|Wrap|Line Numbers
  1.  
  2.       //An attempt to ensure user adds in amount from 100 to 1000000, no more, no less...
  3.       while (loanAmount < 100 || loanAmount > 1000000 ) {
  4.           inputStr  = JOptionPane.showInputDialog(null, "Loan Amount (Dollars + Cents):");
  5.  
  6.           try {
  7.               loanAmount = Double.parseDouble(inputStr);
  8.               // catch the user's entry here and throw an error
  9.               //User will need to re-enter data to continue
  10.               if (loanAmount < 100 || loanAmount > 1000000 ) {
  11.                   throw new Exception("Loan Amount is invalid, please try again...");
  12.               }
  13.  
  14.              //Catch instances were user either adds in vowels or leaves pop up empty
  15.              //Our user will need to re-enter information but the correct data type, digits only...
  16.           } catch (NumberFormatException e) {
  17.  
  18.               JOptionPane.showMessageDialog(null, "'" + inputStr
  19.                                       + "' is an invalid data type\n"
  20.                                       + "Please enter digits only");
  21.               //Catch instances were user either adds in vowels or leaves pop up empty
  22.               //Our user will need to re-enter information but the correct data type, digits only...
  23.           } catch (Exception e) {
  24.  
  25.               JOptionPane.showMessageDialog(null, "Error: "
  26.                                               + e.getMessage());             
  27.  
  28.           }
  29.  
  30. }//This braket is therefore needed to end the loop for the loanAmount.  
  31.  //Even though the InputDialog does in fact come it is annoying having to wait
  32.  //to see the box again.  This one works better:-)
  33.  
  34.  
In a bit, I have another brain tease; to my standards anyway:-)
Apr 6 '08 #162
Dököll
2,364 Expert 2GB
If I were to have the need to set my arrary size I would achieve it by doing this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Automobile []    automobile;         //declare automobile array
  3.         automobile = new Automobile[5];    //create automobile array
  4.  
  5.  
I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         int automobile;  //declare automobile array
  3.         int [] number;    //declare array size/number of automobiles here
  4.  
  5.         number = Integer.parseInt(
  6.                       JOptionPane.showInputDialog(null,"Enter as many automobiles needed, we suggest 3 at most:"));
  7.  
  8.         number = new int[automobile];
  9.  
  10. ...
  11.  
  12.  
I added the number right in here: [ ] , just to see, it did not like it there:-)

What are your thoughts?

Köll
Apr 7 '08 #163
JosAH
11,448 Expert 8TB
If I were to have the need to set my arrary size I would achieve it by doing this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Automobile []    automobile;         //declare automobile array
  3.         automobile = new Automobile[5];    //create automobile array
  4.  
  5.  
I have been toying with this bit of code in my book to allow a non-fixed array to become fixed and vise versa, just need some direction of how to go about it:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         int automobile;  //declare automobile array
  3.         int [] number;    //declare array size/number of automobiles here
  4.  
  5.         number = Integer.parseInt(
  6.                       JOptionPane.showInputDialog(null,"Enter as many automobiles needed, we suggest 3 at most:"));
  7.  
  8.         number = new int[automobile];
  9.  
  10. ...
  11.  
  12.  
I added the number right in here: [ ] , just to see, it did not like it there:-)

What are your thoughts?

Köll
My thoughts are that you don't really understand what objects and arrays are
all about. When you declare a non-primitive variable you actually have a remote
control to your television but you don't have a television yet. All TVs so to speak,
in Java, have to be 'new'd. That creates a TV for you and you can assign it to
your remote control (variable).

Also with arrays, which are objects. you have to 'new' them before you can
manipulate them with your remote control. Arrays always have a fixed size that
you set when you 'new' the array. There are no 'unfixed' arrays, they don't exist.

Java uses two different syntaxes for array remote controls:

Expand|Select|Wrap|Line Numbers
  1. int cStyle[];
  2. int[] javaStyle;
  3.  
The first style is made to make those old C programmers feel more comfortable
but the second style is the prefered one. Both styles create a TV remote control
but there's no TV yet.

If you want to make an array larger or shorter you have to 'new' another array,
copy the corresponding elements from the old to the new array and set the
remote control to point to the new array. That's exactly what the ArrayList does.
There is no magic behind the scenes.

Arrays are a bit special objects w.r.t. their methods: arrays don't have them, i.e.
they're plain data objects and you even can't extend them by subclassing them
(there is nothing to subclass).

When you 'new' an array you can specify any number >= 0 because creating
arrays is done at runtime, not at compile time; that adds a bit more flexibility
than you have in C because in that language the size of the array must be
known to the compiler and therefore must be the value of a constant expression.

kind regards,

Jos
Apr 7 '08 #164
BigDaddyLH
1,216 Expert 1GB
If I were to have the need to set my arrary size I would achieve it by doing this:
Expand|Select|Wrap|Line Numbers
  1. Automobile []  automobile;         //declare automobile array
  2. automobile = new Automobile[5];    //create automobile array
  3.  
What JosAH said. Note how close you are from getting it right. Take your fixed size example and rewrite it as:
Expand|Select|Wrap|Line Numbers
  1. Automobile []  automobile;       
  2. int  automobileSize = ...; //compute size
  3. automobile = new Automobile[automobileSize];   
  4.  
or
Expand|Select|Wrap|Line Numbers
  1. int  automobileSize = ...; //compute size
  2. Automobile [] automobile = new Automobile[automobileSize];   
  3.  
Apr 7 '08 #165
Dököll
2,364 Expert 2GB
What outpouring support, guys, twas a brain tease at best for me and I agree Jos, 'the remote control' scenario is what makes it work best when dealing with arrays, rest assured I was kidding about adding it to [ ] Well, you know I did, not denying that, and did it out of anger, after muscling through it a bit and could not figure it out:-) but this works fine:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Automobile []  automobile;         //declare automobile array
  3. automobile = new Automobile[5];    //create automobile array
  4.  
  5.  
And I seem to understand how it works. I call it fixed, but if it is as:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int cStyle[];
  3. int[] javaStyle;
  4.  
  5.  
I was under the impression it was somewhat not-fixed, because I would give user the power to decide the size of the array, not setting it as in the case above it...

I am not too savvy in these types of arrays just yet, I had to set the above by hand, from the book, but by hand..

This should have worked, in my mind, it should have:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     public static void main (String[] args) {
  3.  
  4.         Automobile[]    automobile;         //declare the Automobile array...
  5.         automobile = new Automobile[automobile];    //and then create Automobile array...
  6.  
  7.         String         name;  //declare variables...
  8.         double         rating1;  //declare variables...
  9.         double         rating2;  //declare variables...
  10.         double         rating3;  //declare variables...
  11.         float          averageRatingScale;  //declare variables...
  12.  
  13.         for (int i = 0; i < automobile.length; i++) { //run loop as if there's no tomorrow:-)
  14.  
  15.             automobile = Integer.parseInt(
  16.                     JOptionPane.showInputDialog(null,"Enter Number of Automobiles:")); //grab Automobile instances
  17.             name    = JOptionPane.showInputDialog(null,"Enter Favourite Automobile name:"); //demand for automobile name or preference
  18.             rating1     = Double.parseDouble(
  19.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto # 1:")); //ask to enter rating scale 1 of favourite auto
  20.             rating2     = Double.parseDouble(
  21.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto# 2:"));  //ask to enter rating scale 2 of favourite auto
  22.             rating3     = Double.parseDouble(
  23.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto # 3:"));  //ask to enter rating scale 3 of favourite auto
  24.             //create a new Automobile and assign values to each instance
  25.  
  26.             Automobile = new int[automobile];
  27.             automobile[i] = new Automobile ();  //create your new Automobile
  28.  
  29.             automobile[i].setName  ( name   );   //create instances of each favourite new Automobile
  30.             automobile[i].setrating1   ( rating1    );    //create instances of each favourite new Automobile rating scale 1
  31.             automobile[i].setrating2   ( rating2    );    //create instances of each favourite new Automobile rating scale 2
  32.             automobile[i].setrating3   ( rating3    );    //create instances of each favourite new Automobile rating scale 3
  33.  
  34.         float sum = 0; 
  35.         for (int i = 0; i < automobile.length; i++) {
  36.  
  37.             sum += automobile[i].getRating1();
  38.         }
  39.  
  40.         for (int i = 0; i < automobile.length; i++) {
  41.  
  42.             sum += automobile[i].getRating2();
  43.         }
  44.  
  45.         for (int i = 0; i < automobile.length; i++) {
  46.  
  47.             sum += automobile[i].getRating3();
  48.         }
  49.  
  50.  
  51.  
  52.  
  53.         averageRatingScale = sum / (float) Automobile.length / 3;
  54.         System.out.println("Maximum Rating Scale: " + sum);
  55.         System.out.println("Average Rating Scale: " + averageRatingScale);
  56.  
  57.       //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  58.  
  59.         Automobile    undoubtablySporty,   //points to the Semi-Sporty Automobile
  60.                        stupendouslySporty;         //points to the Sportiest Automobile
  61.  
  62.         undoubtablySporty = stupendouslySporty = automobile[0];
  63.  
  64.         for (int i = 1; i < automobile.length; i++) {
  65.  
  66.             if ( automobile[i].getRating1() < undoubtablySporty.getRating1() ) {
  67.                 //found a weakest Automobile
  68.                 undoubtablySporty   = Automobile[i];
  69.             }
  70.             else if ( automobile[i].getRating1() > stupendouslySporty.getRating1() ) {
  71.                 //found a strongest Automobile in this class...
  72.                 stupendouslySporty     = automobile[i];
  73.             }
  74.  
  75.         }  //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  76.  
  77.  
  78.  
  79.         System.out.println("Rating Scale for Automobile named: " + stupendouslySporty.getName()
  80.                     + " is " +   (stupendouslySporty.getRating1()+ stupendouslySporty.getRating2()+ stupendouslySporty.getRating3())
  81.                     / 3 + " grade point average.");
  82.         System.out.println("Rating Scale for Automobile: " + undoubtablySporty.getName()
  83.         + " is " +   (undoubtablySporty.getRating1()+ undoubtablySporty.getRating2()+ undoubtablySporty.getRating3())
  84.         / 3 + " grade point average."); 
  85.  
  86.           //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  87.  
  88.  
  89.         String fecthAuto = JOptionPane.showInputDialog(null,"Name Automobile to fetch:");
  90.  
  91.         int i = 0;
  92.  
  93.         while ( i < automobile.length &&     //still more Automobiles to search
  94.                 !automobile[i].getName().equals( fecthAuto ) ) {
  95.             i++;
  96.         }
  97.  
  98.         if (i == automobile.length) {
  99.             //not found - unsuccessful search
  100.             System.out.println( fecthAuto + " was not in the array" );
  101.  
  102.         } else {
  103.             //found - successful search
  104.             System.out.println("Looks like we found " + fecthAuto + i + " level in the storage " + );
  105.             System.out.println("Looks like we found " + fecthAuto + " having " + averageRatingScale);
  106.         }
  107.  
  108.     }
  109.  
  110.  
  111.  }
  112.  
  113.  
  114. }
  115.  
  116.  
  117.  
I think I lost it after that, so thank you BigDaddyLH for posting and to let me know I am not that far off, or pretty close, the above is pretty uggly, of course you know it does not work. But your examples/adivce will surely help pin it down


Expand|Select|Wrap|Line Numbers
  1.  
  2. Automobile []  automobile;       
  3. int  automobileSize = ...; //compute size
  4. automobile = new Automobile[automobileSize];
  5.  
  6. or
  7.  
  8. int  automobileSize = ...; //compute size
  9. Automobile [] automobile = new Automobile[automobileSize];
  10.  
  11.  
My book is a true exercise-based book, where you get mismathed bit of code and some explaination on how it works and why, and it moves on to examples and samples of non-related code, important code like the one posted.

But you guys are right, I am probably reading too much into it. There's probably no reason to make the code do what it is not supposed to do.

Thanks for posting, I'll give examples a whirl;-)

Will let you know:-)

Köll
Apr 8 '08 #166
Dököll
2,364 Expert 2GB
It would not have worked Gang, I need to enter the size before making up each Auto, my noodle was probably a bit sketchy, back to drawing board...

In a bit;-)
Apr 10 '08 #167
Dököll
2,364 Expert 2GB
Almost there; can anyone tell me why only one instance of an Automobile is being read:

Expand|Select|Wrap|Line Numbers
  1.  
  2. No errors but I am not telling it to do it properly...
  3.  
  4.  
  5. public static void main (String[] args) {
  6.  
  7.  
  8.         String          name;  //declare variables...
  9.         double         rating1;  //declare variables...
  10.         int               numTypeAuto = 0;  //declare variables...
  11.         double         rating2;  //declare variables...
  12.         double         rating3;  //declare variables...
  13.         float            averageRatingScale;  //declare variables...
  14.  
  15.             numTypeAuto = Integer.parseInt(
  16.                     JOptionPane.showInputDialog(null,"Enter Number of Automobiles:")); //grab Automobile instances
  17.             name    = JOptionPane.showInputDialog(null,"Enter Favourite Automobile name:"); //demand for automobile name or preference
  18.             rating1     = Double.parseDouble(
  19.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto Type # 1:")); //ask to enter rating scale 1 of favourite auto
  20.             rating2     = Double.parseDouble(
  21.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto Type # 2:"));  //ask to enter rating scale 2 of favourite auto
  22.             rating3     = Double.parseDouble(
  23.                     JOptionPane.showInputDialog(null,"Enter Rating Scale For Auto Type # 3:"));  //ask to enter rating scale 3 of favourite auto
  24.             //create a new Automobile and assign values to each instance
  25.  
  26.  
  27.         for (int i = 0; i < automobile.length; i++) { //run loop as if there's no tomorrow:-)
  28.  
  29.  
  30.          Automobile[]    automobile;         //declare the Automobile array...
  31.          automobile = new Automobile[numTypeAuto];    //and then create Automobile array...
  32.  
  33.             automobile[i].setName  ( name   );   //create instances of each favourite new Automobile
  34.             automobile[i].setrating1   ( rating1    );    //create instances of each favourite new Automobile rating scale 1
  35.             automobile[i].setrating2   ( rating2    );    //create instances of each favourite new Automobile rating scale 2
  36.             automobile[i].setrating3   ( rating3    );    //create instances of each favourite new Automobile rating scale 3
  37.  
  38. automobile[i].setNumTypeArray   ( arraySize    );    //create instances of several favourite Automobiles
  39.  
  40.  
  41.         float sum = 0; 
  42.         for (int i = 0; i < automobile.length; i++) {
  43.  
  44.             sum += automobile[i].getRating1();
  45.         }
  46.  
  47.         for (int i = 0; i < automobile.length; i++) {
  48.  
  49.             sum += automobile[i].getRating2();
  50.         }
  51.  
  52.         for (int i = 0; i < automobile.length; i++) {
  53.  
  54.             sum += automobile[i].getRating3();
  55.         }
  56.  
  57.  
  58.  
  59.  
  60.         averageRatingScale = sum / (float) Automobile.length / 3;
  61.         System.out.println("Maximum Rating Scale: " + sum);
  62.         System.out.println("Average Rating Scale: " + averageRatingScale);
  63.  
  64.       //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  65.  
  66.         Automobile    undoubtablySporty,   //points to the Semi-Sporty Automobile
  67.                        stupendouslySporty;         //points to the Sportiest Automobile
  68.  
  69.         undoubtablySporty = stupendouslySporty = automobile[0];
  70.  
  71.         for (int i = 1; i < automobile.length; i++) {
  72.  
  73.             if ( automobile[i].getRating1() < undoubtablySporty.getRating1() ) {
  74.                 //found a weakest Automobile
  75.                 undoubtablySporty   = Automobile[i];
  76.             }
  77.             else if ( automobile[i].getRating1() > stupendouslySporty.getRating1() ) {
  78.                 //found a strongest Automobile in this class...
  79.                 stupendouslySporty     = automobile[i];
  80.             }
  81.  
  82.         }  //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  83.  
  84.  
  85.  
  86.         System.out.println("Rating Scale for Automobile named: " + stupendouslySporty.getName()
  87.                     + " is " +   (stupendouslySporty.getRating1()+ stupendouslySporty.getRating2()+ stupendouslySporty.getRating3())
  88.                     / 3 + " grade point average.");
  89.         System.out.println("Rating Scale for Automobile: " + undoubtablySporty.getName()
  90.         + " is " +   (undoubtablySporty.getRating1()+ undoubtablySporty.getRating2()+ undoubtablySporty.getRating3())
  91.         / 3 + " grade point average."); 
  92.  
  93.           //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  94.  
  95.  
  96.         String fecthAuto = JOptionPane.showInputDialog(null,"Name Automobile to fetch:");
  97.  
  98.         int i = 0;
  99.  
  100.         while ( i < automobile.length &&     //still more Automobiles to search
  101.                 !automobile[i].getName().equals( fecthAuto ) ) {
  102.             i++;
  103.         }
  104.  
  105.         if (i == automobile.length) {
  106.             //not found - unsuccessful search
  107.             System.out.println( fecthAuto + " was not in the array" );
  108.  
  109.         } else {
  110.             //found - successful search
  111.             System.out.println("Looks like we found " + fecthAuto + i + " level in the storage " + );
  112.             System.out.println("Looks like we found " + fecthAuto + " having " + averageRatingScale);
  113.         }
  114.  
  115.     }
  116.  
  117.  
  118.  }
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
Will be here all night. Thanks for you help, will report my findings, I'm pumped:-)
Apr 10 '08 #168
JosAH
11,448 Expert 8TB
Why are you creating that automobile array *inside* a loop that needs the length
of the array? Does that code even compile? btw, you're not creating Automobiles
for each arrray entry; i.e. your code erroneously assumes they are just there.
If your code compiles it crashes horribly at runtime.

kind regards,

Jos
Apr 10 '08 #169
SammyB
807 Expert 512MB
If Java were a boat this would'be been the slick, smooth development experience.

kind regards,

Jos ;-)
Actually, some of those disembarkations looked similar to the beginnings of my Java projects: “Ah, here it is JList…now add some more items to the list… what, why is it immutable?!xxx!?” Or the time I said, “just need a couple of option buttons… ah,JRadioButton… now,click on the other option button… arggg they’re both on!”

XML is going smoothly, but the multiple-guess questions are already killing me. The weather is getting warm and I’m itching to go off-road biking again, hopefully, I won’t do any more of these falls (halfway down, just after the jet ski), but it’s sort of part of the fun when the trail is wet. Took the road bike for 14 miles last night, then I was going to run. Ran the first block which was up hill and immediately realized that I could barely walk, so I went back home. Training for a triathlon is not fun, but I am losing weight.
Apr 10 '08 #170
Dököll
2,364 Expert 2GB
Why are you creating that automobile array *inside* a loop that needs the length
of the array? Does that code even compile? btw, you're not creating Automobiles
for each arrray entry; i.e. your code erroneously assumes they are just there.
If your code compiles it crashes horribly at runtime.

kind regards,

Jos
It runs quite nicely actually, it just allows me one entry alone even though I enter 3 for example, I wanted user instance to feed the array whereby 3 added to InputBox would allow 3 Automobiles to be entered, then the program would create the Automobiles and their ratings and there's a way to fetch any auto name and reaveal it's position, looks like a done deal. If you can say that three times:-) But what is the direction you think I should be going?

Thanks for your reply Jos!

Köll
Apr 11 '08 #171
JosAH
11,448 Expert 8TB
It runs quite nicely actually, it just allows me one entry alone even though I enter 3 for example, I wanted user instance to feed the array whereby 3 added to InputBox would allow 3 Automobiles to be entered, then the program would create the Automobiles and their ratings and there's a way to fetch any auto name and reaveal it's position, looks like a done deal. If you can say that three times:-) But what is the direction you think I should be going?

Thanks for your reply Jos!

Köll
If your code runs nicely then it's not the code you showed us in reply #168. Have
a look at that loop e.g:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < automobile.length; i++) { //run loop as if there's no tomorrow:-)
  2.  
  3.  
  4.    Automobile[]    automobile;
  5.    automobile = new Automobile[numTypeAuto];
  6.  
  7.    automobile[i].setName  ( name   );  
  8.  
Your automobile array doesn't even exist in the header part of your for loop. In
that loop you create an array at every loop pass but no automobiles for the
entries of that array. This code doesn't even compile and most certainly doesn't
run.

kind regards,

Jos
Apr 11 '08 #172
Dököll
2,364 Expert 2GB
If your code runs nicely then it's not the code you showed us in reply #168. Have
a look at that loop e.g:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < automobile.length; i++) { //run loop as if there's no tomorrow:-)
  2.  
  3.  
  4.    Automobile[]    automobile;
  5.    automobile = new Automobile[numTypeAuto];
  6.  
  7.    automobile[i].setName  ( name   );  
  8.  
Your automobile array doesn't even exist in the header part of your for loop. In
that loop you create an array at every loop pass but no automobiles for the
entries of that array. This code doesn't even compile and most certainly doesn't
run.

kind regards,

Jos
Hey Jos!

Yes you have the version that did not work, using both my notebook and home workstation. Anyway, had work out some quirks, have since renamed certain variables to make them more readable:-)

I have some constructors running in the main class, the code you are looking at is my package code, I think it is called the OOD technique.

My constructors look like like they're doing what they're there for:

(1) I get the data I need with the get method,
(2) set values with the set method and so on,

so the code runs well, I just can't put my finger on the problem:

Automobile class

Expand|Select|Wrap|Line Numbers
  1.  
  2.    //Get variables to load to program...
  3.  
  4.   public String getName( ) {
  5.         return name;
  6.     }
  7.  
  8.   public int getRating1( ) {
  9.         return rating1;
  10.     }
  11.  
  12.  
  13.  
  14.   public int getRating2( ) {
  15.         return rating2;
  16.     }
  17.  
  18.  
  19.  
  20.   public int getRating3( ) {
  21.         return rating3;
  22.     }
  23.  
  24.  
  25.   public int getNumTypeArray   ( ) {
  26.         return numTypeAuto;
  27.     }
  28.  
  29.  
  30.  
  31.    //Set variables to load to program...
  32.  
  33.  
  34.   public void setName( String name ) {
  35.         this.name = name;
  36.     }
  37.  
  38.   public void setNumTypeArray  ( int numTypeAuto) {
  39.         this.numTypeAuto= numTypeAuto;
  40.     }
  41.  
  42.  
  43.   public void setRating1( int rating1 ) {
  44.         this.rating1 = rating1;
  45.     }
  46.  
  47.  
  48.   public void setRating2( int rating2 ) {
  49.         this.rating2 = rating2;
  50.     }
  51.  
  52.  
  53.  
  54.   public void setRating3( int rating3 ) {
  55.         this.rating3 = rating3;
  56.     }
  57.  
  58.  
  59.  
  60.  
  61.  
Data do get loaded in, still cannot shake it. But is it is the week-end, by midnight, I am dracula on Java, I'll get it to work. Surely appreciate your throwing your hat in:-)

In a bit...

Köll
Apr 12 '08 #173
JosAH
11,448 Expert 8TB
Hey Jos!

Yes you have the version that did not work, using both my notebook and home workstation. Anyway, had work out some quirks, have since renamed certain variables to make them more readable:-)

I have some constructors running in the main class, the code you are looking at is my package code, I think it is called the OOD technique.
This is never going to work out: you post code that is not the code what you're
talking about; you post wallpapers full of code and ramble about it; we can't help
you like that. Post a short self-contained compilable example, better known as a
SSCCE and we can discuss matters adequately.

kind regards,

Jos
Apr 12 '08 #174
Dököll
2,364 Expert 2GB
This is never going to work out: you post code that is not the code what you're
talking about; you post wallpapers full of code and ramble about it; we can't help
you like that. Post a short self-contained compilable example, better known as a
SSCCE and we can discuss matters adequately.

kind regards,

Jos
I was posting the part with the problem Jos, isn't that the rule when it involves school work, I wanted to get an idea if my construction of the array looked right so you got bits and pieces of the code...

the code works...

I have to rewrite it on this machine, my wife has the notebook.

Should only be a second
Apr 12 '08 #175
JosAH
11,448 Expert 8TB
I was posting the part with the problem Jos, isn't that the rule when it involves school work, I wanted to get an idea if my construction of the array looked right so you got bits and pieces of the code...

the code works...
Not the code you have shown us; we are not psychic and can't know that you
have some code somewhere on some computer that happens to work nicely.
Programming and debugging doesn't work that way. You can't just dump random
pieces of code and say "the problem is in there" while the rest of the code
doesn't make any sense. That's why I asked you to post an SSCCE (see my
previous reply) if you want any adequate help.

kind regards,

Jos
Apr 12 '08 #176
Dököll
2,364 Expert 2GB
Alright, Jos, stop it now will you!

Weren't you able to provide assistance with minimal information, the above post 164, or 169 regarding arrays; that was good, I gave minimal info, you helped, I learned something from it.

As said, I wanted to do it on my own so adding as little information would allow me to achieve it, thought this was for site rules; when it regards school activities, direction, examples and so on, must show ability to come with ideas on your own! No, isn't that it?

Still, Jos, I cannot loop to grab all instances added to an input dialog that would allow user to decide how many automobiles to enter, and that would make up the size of the array... instead I get one instance/chance and one result:

(1) Below code is that of our Automobile class
(2) Code is associated with a template
(3) Template code follows main class code

Automobile class

Expand|Select|Wrap|Line Numbers
  1.  
  2. package processAutomobile;
  3.  
  4. /**
  5.  * @author Köll S. Cherizard
  6.  * @professor :-)
  7.  * @Lab 7
  8.  * @version 200800000408
  9. */
  10.  
  11. /**
  12.  * The Automobile class used in Chapter 10
  13.  */
  14. class Automobile {
  15.  
  16.     /**
  17.      * The name of this automobile
  18.      */
  19.     private String  name;
  20.  
  21.     /**
  22.      * The Rating Scale of each Auto being entered
  23.      */
  24.     private int     rating1;
  25.     private int     rating2;
  26.     private int     rating3;
  27.     private char     autoSpeed;
  28.  
  29.     private int     numTypeAuto;
  30.  
  31.      /**
  32.      * Default constructor
  33.      */
  34.     public Automobile() {
  35.         this("Not Given", 0,'U');
  36.     }
  37.  
  38.     /**
  39.  
  40.      * @param name   name of the new automobile
  41.      */
  42.     public Automobile(String name, int numTypeAuto, char autoSpeed) {
  43.  
  44.         this.name          = name;
  45.         this.numTypeAuto   = numTypeAuto;
  46.         this.autoSpeed     = autoSpeed;
  47.  
  48.     }
  49.  
  50.     /**
  51.      * Returns the Rating 1 of each automobile.
  52.      */
  53.     public int getRating1( ) {
  54.         return rating1;
  55.     }
  56.  
  57.  
  58.     /**
  59.      * Returns the Rating 2 of each automobile.
  60.      */
  61.     public int getRating2( ) {
  62.         return rating2;
  63.     }
  64.  
  65.  
  66.     /**
  67.      * Returns the Rating 3 of each automobile.
  68.      */
  69.     public int getRating3( ) {
  70.         return rating3;
  71.     }
  72.  
  73.     /**
  74.      * Returns the number of automobiles being entered...
  75.      */
  76.     public int getSizeOfArray( ) {
  77.         return numTypeAuto;
  78.     }
  79.  
  80.     /**
  81.      * Returns the name of this automobile.
  82.      */
  83.     public String getName( ) {
  84.         return name;
  85.     }
  86.  
  87.  
  88.  
  89.     public char autoSpeed( ) {    
  90.         return autoSpeed;
  91.     }
  92.  
  93.     /**
  94.      * Set Rating of each automobile.
  95.      */
  96.     public void setRating1( int rating1 ) {
  97.         this.rating1 = rating1;
  98.     }
  99.  
  100.     /**
  101.      * Set Rating of each automobile.
  102.      */
  103.     public void setRating2( int rating2 ) {
  104.         this.rating2 = rating2;
  105.     }
  106.  
  107.     /**
  108.      * Set Rating of each automobile.
  109.      */
  110.     public void setRating3( int rating1 ) {
  111.         this.rating1 = rating1;
  112.     }
  113.  
  114.     /**
  115.      * Set the name of this automobile.
  116.      */
  117.     public void setName( String name ) {
  118.         this.name = name;
  119.     }
  120.  
  121.     public void setSizeOfArray( int numTypeAuto ) {
  122.         this.numTypeAuto = numTypeAuto;
  123.     }
  124.  
  125.     public void setAutoSpeed( char autoSpeed) {
  126.         this.autoSpeed = autoSpeed;
  127.     }
  128.  
  129. }
  130.  
  131.  
  132.  

Automobile template code...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package processAutomobile;
  4.  
  5. /**
  6.  * @author Köll S. Cherizard
  7.  * @professor :-)
  8.  * @Lab 7
  9.  * @version 200800000408
  10. */
  11.  
  12. import javax.swing.*; 
  13.  
  14.  
  15.  
  16.  
  17.     class Ch10ProcessAutomobileArray { 
  18.  
  19.      public static void main (String[] args) { 
  20.       String name, speed;         
  21.       int numTypeAuto = 0;  //declare variables...
  22.       int rating1;       //declare variables...
  23.       int rating2;       //declare variables...
  24.       int rating3;       //declare variables...
  25.       char autoSpeed;
  26.       //read in data values 
  27.       numTypeAuto = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Automobiles:")); 
  28.  
  29.       name = JOptionPane.showInputDialog(null,"Enter Favourite Automobile name:"); 
  30.  
  31.       rating1 = Integer.parseInt(JOptionPane.showInputDialog
  32.               (null,"Enter Rating Scale for Endurance (Example: 5 through 10):"));      //ask to enter rating scale 1 of favourite auto
  33.       rating2 = Integer.parseInt(JOptionPane.showInputDialog
  34.               (null,"Enter Rating Scale for Handling (Example: 5 through 10):"));       //ask to enter rating scale 2 of favourite auto
  35.       rating3 = Integer.parseInt(JOptionPane.showInputDialog
  36.               (null,"Enter Rating Scale for Dependability (Example: 5 through 10):"));  //ask to enter rating scale 3 of favourite auto
  37.  
  38.       speed = JOptionPane.showInputDialog(null,"Enter Speed of this Automobile name:"); //ask to enter speed of each favourite auto
  39.       autoSpeed = speed.charAt(0);
  40.  
  41.  
  42.       Automobile[] automobile; //declare the Automobile array 
  43.       automobile = new Automobile[numTypeAuto]; //and then create it       
  44.  
  45.       for (int i = 0; i < automobile.length; i++) { //create a new Automobile and assign values 
  46.  
  47.       automobile[i] = new Automobile( );       
  48.       automobile[i].setSizeOfArray( numTypeAuto );  //create user-defined number of Automobiles to be entered
  49.       automobile[i].setName  ( name   );            //create instances of each favourite new Automobile
  50.       automobile[i].setRating1   ( rating1    );    //create instances of each favourite new Automobile rating scale 1
  51.       automobile[i].setRating2   ( rating2    );    //create instances of each favourite new Automobile rating scale 2
  52.       automobile[i].setRating3   ( rating3    );    //create instances of each favourite new Automobile rating scale 3
  53.       automobile[i].setAutoSpeed ( autoSpeed    );  //create speed level of each favourite new Automobile
  54.  
  55.  
  56.       }
  57.  
  58.       //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  59.  
  60.       Automobile undoubtablySporty,   //points to the Semi-Sporty Automobile
  61.       stupendouslySporty;             //points to the Sportiest Automobile
  62.       undoubtablySporty = stupendouslySporty = automobile[0]; //initialize each strength to zero...
  63.  
  64.         //loop to make available for Rating Scale 1, 2, and 3
  65.         for (int i = 1; i < automobile.length; i++) { 
  66.  
  67.         if ( automobile[i].getRating1() < undoubtablySporty.getRating1() ) { //found a younger Automobile 
  68.             undoubtablySporty = automobile[i];
  69.         }
  70.  
  71.         else if ( automobile[i].getRating1() > stupendouslySporty.getRating1() ) { //found an older Automobile 
  72.             stupendouslySporty = automobile[i];
  73.         }
  74.         }
  75.            System.out.println("Rating Scale for Automobile named : " + stupendouslySporty.getName() + " is " + stupendouslySporty.getRating1()); 
  76.            System.out.println("Rating Scale for Automobile named: " + undoubtablySporty.getName() + " is " + undoubtablySporty.getRating1()); 
  77.  
  78.         //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...        
  79.  
  80.         String searchName = JOptionPane.showInputDialog(
  81.         null,"Name to search:"); 
  82.         int i = 0; 
  83.  
  84.         //still more Automobiles to search
  85.         while ( i < automobile.length && !automobile[i].getName().equals( searchName ) ) {i++;
  86.         }
  87.         if (i == automobile.length) { //not found - unsuccessful search 
  88.            System.out.println( searchName + " was not in the array" ); 
  89.         } 
  90.         else { 
  91.         //found - successful search 
  92.            System.out.println("Found " + searchName + " at position " + i); 
  93.         }
  94.     }
  95. }
  96.  
  97.  
I simply cannot shake it, runs without compile errors but the results not what I am looking for. Just a little guidance will do just fine, I am patient, I know I'll get it... Thanks for all your help.

Köll
Apr 13 '08 #177
JosAH
11,448 Expert 8TB
Alright, Jos, stop it now will you!
Why should I stop and stop with what? You're posting here asking for advice. I'm
trying to tell you to stop programming like a senile granny's knitting: she's knitting
but forgot what she was knitting but she keeps on knitting and hopes that all ends
up well; i.e. it lacks a plan, a design just like your coding.

There are a few stone old rules: a class or method should do one thing and it
should do it well. This immediately implies that a program that's supposed to
do quite a few things should have quite a few classes with quite a few methods.

There's another 'linquistic' trick: write down what you want in enough detail; the
verbs you're using are the methods and the nouns are the classes. Give it a try:

- ask user for numberOfCars
- setup storage for numberOfCars Cars
- for numberOfCars
--- ask CarDetails and store them

That's about it and this fragment basically translates to:

Expand|Select|Wrap|Line Numbers
  1. int numberOfCars= askNumberOfCars();
  2. Car[] cars= new Car[numberOfCars];
  3. for (int i= 0; i < numberOfCars; i++)
  4.    CarDetails carDetails= askCarDetails();
  5.    cars[i]= new Car(carDetails);
  6.  
Note that I used the artifact CarDetails which you may get around by a bit of
different coding but it does the job for now. It is called a 'DTO' (Data Transfer
Object) and is a well known aid in Java and other languages. In your simple
case the Car can be the CarDetails class itself; it's up to you.

There are a few simple methods that do their single job and they should do it well.
As you can see the program control flow is a one to one bijection between the
plans (the design or pseudo code) and the Java implementation. At this level
we're not interested in the details of the other methods: they have an isolated
functionality that won't interfere with the job at hand.

Look at the code above: it's just a few lines of Java and does what it has to do
with the help of a few other methods and classes. Keep on doing this in some
sort of 'fractal' way until you've designed and implemented it all, e.g. you should
already have a 'generic' method available that prompts the user for an integer.

If you read this very same thread you must've noticed it already so (re)use it. Stop
knitting like a granny and stop hoping that all that wallpaper ends up well; most of
the time it doesn't and programming is not knitting.

kind rregards,

Jos
Apr 13 '08 #178
Dököll
2,364 Expert 2GB
Good advice, I know you're kidding about thegranny part... most grannies are actually pretty skilled @ programming...

My design is flawless, Jos, you can say,"hey that looks like a lot of effort, but I would try it this way instead"

Or,

"...you may have a pretty cool design, but I would concentrate in this area..."

Something in that kind of a line... The idea is an advice is well retained if one can concentrate on that and not the fact that it was not well said, even though helpful...

I could have added classes for the 3 ratings below:
Expand|Select|Wrap|Line Numbers
  1.       Endurance (Example: 5 through 10):"));      //ask to enter rating scale 1 of favourite auto
  2.       Handling (Example: 5 through 10):"));       //ask to enter rating scale 2 of favourite auto
  3.       Dependability (Example: 5 through 10):"));  //ask to enter rating scale 3 of favourite auto "
The project at hand did not call for other classes, and I surely could not add what should never be put in, as per our professor... that's not the exercise.

I am proud of my work, it stinks, but it's mine, so help me understand where I have faultered, if that's a verb, but don't keep saying only what's not good about it, you do not know the trouble I went through to make the design a reality, for an apprentice by the way:-)

Thanks for you help!

Köll
Apr 14 '08 #179
JosAH
11,448 Expert 8TB
I am proud of my work, it stinks, but it's mine, so help me understand where I have faultered, if that's a verb, but don't keep saying only what's not good about it, you do not know the trouble I went through to make the design a reality, for an apprentice by the way:-)
Read this.

kind regards,

Jos
Apr 14 '08 #180
SammyB
807 Expert 512MB
Read this.

kind regards,

Jos
Does commandment #10 have anything to do with a senile granny's knitting? ROFL, but only because I'm currently trying to unravel some of my knitting that I did about 5 years ago. It's some visualization software in Excel for multi-dimensional data. I think that egoless programming is probably an oxymoron: the only programmers that can and do laugh at their awful code know that their code is actually head and shoulders above anyone else's. Have a great day!
Apr 15 '08 #181
NeoPa
32,182 Expert Mod 16PB
...the only programmers that can and do laugh at their awful code know that their code is actually head and shoulders above anyone else's. Have a great day!
I see wisdom :)
Apr 15 '08 #182
Plater
7,872 Expert 4TB
Shouldn't this be in the discussion section? Not much of a question.
Apr 15 '08 #183
Dököll
2,364 Expert 2GB
Why is my text all red up there, that's happened before?!?

I am still beating the beast even after reading the information, and I surely appreciate it. I learn by accepting critics, I get it from the professors, I see you guys as professors within your own means, gt it here as well

Truth is I am still going to ask questions, rest assured I am not calling myself Java programmer until I can actually write this stuff, for now, I am modifying the works of our great professor, making up a desgn that best attack the lab she has set forth. I try to find bits and pieces of code from past and even future labs to give her what she needs, then I come here and talk about it.

My friends, I still cannot shake it...

I will attempt to load a zip containing only the design, the original code called for fixed array:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         Person[]    person;         //declare the person array
  3.         person = new Person[5];    //and then create it
  4.  
  5.  
As shown, the code beyond this one was with the help of the professor and you, but it got interesting when I had to make it work when I went to work on the ideas I got.

Will post later, being pulled away here;-)

Thanks!

Köll
Apr 15 '08 #184
NeoPa
32,182 Expert Mod 16PB
Why is my text all red up there, that's happened before?!?
...
That's because you have included code which had unbalanced quotes in it.

The code-specific tags are not 100% so when your code stopped in the middle of a string, they didn't handle returning from string mode when the tags were closed.

Fixed now - but you may want to look at what you're posting for clues like that. It may also help you to fix some code possibly. I'm no Java expert so I can't be sure on that one ;)
Apr 16 '08 #185
Dököll
2,364 Expert 2GB
What do you know, pretty simple, thanks on that... I'll fetch my other one and attempt to fix it. Yes will let you know if I cannot fix...

Thanks again NeoPa!

Köll
Apr 17 '08 #186
Dököll
2,364 Expert 2GB
It was my last day today at work, pretty somber leave but my colleagues were so nice, I could not begin to tell you... thing is they understand my having to go, it's actually for a Java position, I am being paired up with folks who know and will be sent to training, pretty interesting quite honestly, seems like it's all falling into place. I am babbling but I am happy that the group I left is even now stronger, moved up to a bigger IT division, which we had to contact now and then for the more specific IT stuff, we're/my colleagues are now right there. I don't feel so bad anymore, send my boss the code library to the database built for the clan. Hopefully I did not leave any work behind, tried not to, 'til the last hour, should be okay, I think:-)

On a different note, here is what I could salvage out of my design, could not do the zip, silly me:-)

Expand|Select|Wrap|Line Numbers
  1.  
  2. Köll S. Cherizard
  3. Lab 7
  4. Version 200800000408
  5.  
  6. Automobiles need to be entered via pop up menus or else.  Entries at this point should dictate size of array/InputDialog pop up in the program; if user enters 1, only 1 automobile should carry ratings of three types, ID and name should also be vital.  Each automobile’s own ID, Rating ranging from Endurance, Dependability and handling, and name of each automobile must be stored according to number of instances added to array or InputDialog.  Must enter speed of each vehicle, names and so on ought to further differentiate from Semi-Sporty autos to the Sportiest of them all.  
  7.  
  8. IMPORTANT: Make available number format exception to fend off bogus data such as vowels, characters as a dash, period and more, in pop up menus. 
  9.  
  10. Sort data result or view using bubble sort method; add a search method, sort by user ID and search by auto name. Distinguish ratings to make available Semi-Sporty and Sportiest automobile average rating levels, display rating levels of either with Ids sorted.
  11.  
  12. Declare variables: NumberOfAutomobiles, AutoName, ID, Speed, Rating1, Rating2, Rating3
  13.  
  14. NumberOfAutomobiles as integer
  15. AutoName as string
  16. ID as integer
  17. Speed as char
  18. Rating1 as int or double
  19. Rating2 as int or double
  20. Rating3 as int or double
  21.  
  22. Add Loop to make available for Rating Scale 1, 2, and 3
  23. Use for loop to add Ratings one by one then find length of search, any type of user input, necessary to restrict user entries at this point.
  24.  
  25.     if automobile Rating1 less than average and is specific to the name of auto for Rating1 and data valid, user has found a Semi-Sporty Auto Automobile 
  26.  
  27.  
  28.     if automobile Rating1 higher than average and is specific to the name of auto for Rating1 and data valid, user has found a Sportiest Auto Automobile 
  29.  
  30. WARNING:  The ratings will need match ID numbers of each auto
  31.            Add a bubble to sort by ID and bring in a linear search option, it's a must
  32.  
  33.  
  34. Add Loop to fetch all automobile added into the program
  35. Use for loop to search and find length of text user need searching, compare against already added data to InputDialog.
  36.  
  37.  
  38.  
  39.  
  40.  
  41. IMPORTANT:  The ratings will need match ID numbers of each auto
  42.            Add a bubble to sort by ID, and bring in a linear search option
  43.  
  44.  
  45. TO DO:  Add/distinguish Rating 1 from Rating 2, and so on, and output ALL
  46.        separately for all to see… Make program scalable in case you need
  47.        to add stuff; build templates if needed…
  48.  
  49. RESEARCH:
  50.  
  51. ·    Use LAB6 for fixed-array code – in class lab
  52. ·    Use Book for undefined array examples – homework # 5 example
  53. ·    Use LAB 5 for Number Format exception – in class lab
  54. ·    Use entry instances to determine array size – user InputDialog
  55. ·    Use bubble sort method near the end – Discussion, Web site search
  56. ·    Use Linear search method code example – discussion, Web site search
  57. ·    MISC:  Print to system of MessageDialog
  58.  
  59.  
  60.  
  61.  
I have a long way to go, I got some leads on getting the program to loop for more entries. I am attempting usng String [] out of my:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void main(String[] args) {
  3.  
  4.  
  5. ...
  6.  
  7.  
  8. ...
  9.  
  10.  
  11. then adding an istance where it should fetch/equal entries made
  12.  
  13.  
  14. String[] drivers = new String[numTypeAuto];
  15.  
  16. Something like that...
  17.  
  18.  
Still researching, but please jump in, I could be doing this in a simpler way.

In a bit, and thanks for everything. Wish me luck on 2morrow;-)

Köll
Apr 17 '08 #187
Dököll
2,364 Expert 2GB
Good heavens I did it again, UNbelievable:-)

Alright, let me figure this out...
Apr 17 '08 #188
Dököll
2,364 Expert 2GB
Good heavens I did it again, UNbelievable:-)

Alright, let me figure this out...
Now that it's fixed, above does not make sense... My text was red again, so I removed the JAVA, not sure why I had to do that there. Anyway, thanks NeoPa, I re-read your post and I nailed it, it seems:-)

In a bit!

Köll
Apr 17 '08 #189
NeoPa
32,182 Expert Mod 16PB
No worries Köll.

I'd suggest checking the code you post first though. It's possible to get around the problem using simple [ CODE ] tags (text) rather than the Java ones, but as we all (developers) know very well, understanding and fixing the root cause of a problem (the code posted) is far more reliable than always skipping around the problems caused.

Think of it as more of an opportunity to find bugs than as a problem posting.
Apr 17 '08 #190
Dököll
2,364 Expert 2GB
No worries Köll.

I'd suggest checking the code you post first though. It's possible to get around the problem using simple [ CODE ] tags (text) rather than the Java ones, but as we all (developers) know very well, understanding and fixing the root cause of a problem (the code posted) is far more reliable than always skipping around the problems caused.

Think of it as more of an opportunity to find bugs than as a problem posting.
That indeed, Ade, the answer is usually right there, just have to focus now and again:-)

Reporting a small victory since then, the array is working, silly me:

template code...

Expand|Select|Wrap|Line Numbers
  1.       package processAutomobile;
  2.  
  3. /**
  4.  * @author Köll S. Cherizard
  5.  * @professor :-) 
  6.  * @Lab 7
  7.  * @version 200800000408
  8. */
  9.  
  10. import javax.swing.*; 
  11.  
  12.     class ProcessAutomobileArray { 
  13.  
  14.      public static void main (String[] args) { 
  15.       String autoName, speed;         
  16.       int numTypeAuto = 0, autoIDNumber;  //declare variables...
  17.       int rating1;                        //declare variables...
  18.       int rating2;                        //declare variables...
  19.       int rating3;                        //declare variables...
  20.       char autoSpeed;
  21.       //read in data values 
  22.       numTypeAuto = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Automobiles:")); 
  23.  
  24.       Automobile[] automobile; //declare the Automobile array 
  25.       automobile = new Automobile[numTypeAuto]; //and then create it      
  26.       for (int i = 0; i < automobile.length; i++) { //create a new Automobile and assign values      
  27.  
  28.       autoName = JOptionPane.showInputDialog(null,"Enter Favourite Automobile name:"); 
  29.       autoIDNumber = Integer.parseInt(JOptionPane.showInputDialog
  30.               (null,"Enter Automobile ID Number:")); 
  31.  
  32.       rating1 = Integer.parseInt(JOptionPane.showInputDialog
  33.               (null,"Enter Rating Scale for Endurance (Example: 5 through 10):"));      //ask to enter rating scale 1 of favourite auto
  34.       rating2 = Integer.parseInt(JOptionPane.showInputDialog
  35.               (null,"Enter Rating Scale for Handling (Example: 5 through 10):"));       //ask to enter rating scale 2 of favourite auto
  36.       rating3 = Integer.parseInt(JOptionPane.showInputDialog
  37.               (null,"Enter Rating Scale for Dependability (Example: 5 through 10):"));  //ask to enter rating scale 3 of favourite auto 
  38.       speed = JOptionPane.showInputDialog(null,"Enter Speed of this Automobile name:"); //ask to enter speed of each favourite auto
  39.       autoSpeed = speed.charAt(0);     
  40.       automobile[i] = new Automobile( );       
  41.       automobile[i].setSizeOfArray( numTypeAuto );  //create user-defined number of Automobiles to be entered
  42.       automobile[i].setAutoName  ( autoName   );            //create instances of each favourite new Automobile
  43.       automobile[i].setAutoID   ( autoIDNumber    );    //create instances of each favourite new Automobile ID number
  44.       automobile[i].setRating1   ( rating1    );    //create instances of each favourite new Automobile rating scale 1
  45.       automobile[i].setRating2   ( rating2    );    //create instances of each favourite new Automobile rating scale 2
  46.       automobile[i].setRating3   ( rating3    );    //create instances of each favourite new Automobile rating scale 3
  47.       automobile[i].setAutoSpeed ( autoSpeed    );  //create speed level of each favourite new Automobile speed
  48.  
  49.  
  50.       }
  51.  
  52.       //DONE: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
  53.  
  54.       Automobile undoubtablySporty,   //points to the Semi-Sporty Automobile
  55.       stupendouslySporty;             //points to the Sportiest Automobile
  56.       undoubtablySporty = stupendouslySporty = automobile[0]; //initialize each strength to zero...
  57.  
  58.         //loop to make available for Rating Scale 1, 2, and 3
  59.         for (int i = 1; i < automobile.length; i++) { 
  60.  
  61.         if ( automobile[i].getRating1() < undoubtablySporty.getRating1() ) { //found Automobile 
  62.             undoubtablySporty = automobile[i];
  63.         }
  64.  
  65.         else if ( automobile[i].getRating1() > stupendouslySporty.getRating1() ) { //found Automobile 
  66.             stupendouslySporty = automobile[i];
  67.         }
  68.         }
  69.            System.out.println("(1) Rating Scale # 1 for Automobile named : " + stupendouslySporty.getAutoName() + " is " + stupendouslySporty.getRating1()); 
  70.            System.out.println("(2) Rating Scale # 1 for Automobile named: " + undoubtablySporty.getAutoName() + " is " + undoubtablySporty.getRating1()); 
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.             //loop to make available for Rating Scale 1, 2, and 3
  78.             for (int o = 1; o < automobile.length; o++) { 
  79.  
  80.             if ( automobile[o].getRating2() < undoubtablySporty.getRating2() ) { //found Automobile 
  81.                 undoubtablySporty = automobile[o];
  82.             }
  83.  
  84.             else if ( automobile[o].getRating2() > stupendouslySporty.getRating2() ) { //found Automobile 
  85.                 stupendouslySporty = automobile[o];
  86.             }
  87.             }
  88.                System.out.println("(1) Rating Scale # 2 for Automobile named : " + stupendouslySporty.getAutoName() + " is " + stupendouslySporty.getRating2()); 
  89.                System.out.println("(2) Rating Scale # 2 for Automobile named: " + undoubtablySporty.getAutoName() + " is " + undoubtablySporty.getRating2()); 
  90.  
  91.  
  92.  
  93.  
  94.                 //loop to make available for Rating Scale 1, 2, and 3
  95.                 for (int b = 1; b < automobile.length; b++) { 
  96.  
  97.                 if ( automobile[b].getRating3() < undoubtablySporty.getRating3() ) { //found Automobile 
  98.                     undoubtablySporty = automobile[b];
  99.                 }
  100.  
  101.                 else if ( automobile[b].getRating3() > stupendouslySporty.getRating3() ) { //found Automobile 
  102.                     stupendouslySporty = automobile[b];
  103.                 }
  104.                 }
  105.                    System.out.println("(1) Rating Scale # 3 for Automobile named : " + stupendouslySporty.getAutoName() + " is " + stupendouslySporty.getRating3()); 
  106.                    System.out.println("(2) Rating Scale # 3 for Automobile named: " + undoubtablySporty.getAutoName() + " is " + undoubtablySporty.getRating3()); 
  107.  
  108.         //DONE: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...        
  109.  
  110.         String searchAutoName = JOptionPane.showInputDialog(null,"Name of Automobile to search:"); 
  111.         int i = 0; 
  112.  
  113.         //still more Automobiles to search
  114.         while ( i < automobile.length && !automobile[i].getAutoName().equals( searchAutoName ) ) {i++;
  115.         }
  116.         if (i == automobile.length) { //not found - unsuccessful search 
  117.            System.out.println( searchAutoName + " was not in the array" ); 
  118.         } 
  119.         else { 
  120.         //found - successful search 
  121.            System.out.println("You have searched "  + searchAutoName + ", looks like it is found at position " + i); 
  122.         }
  123.     }
  124. }
  125.  
  126.  
Below was in the wrong portion of the code, I asked for the integer to grab instances which would help determine size of array then I grabbed everything before looping through, thus

Expand|Select|Wrap|Line Numbers
  1.  
  2.       Automobile[] automobile; //declare the Automobile array 
  3.       automobile = new Automobile[numTypeAuto]; //and then create it      
  4.       for (int i = 0; i < automobile.length; i++) { //create a new Automobile and assign values    
  5.  
  6.  
Should be right after user has entered number of automobiles to work with:

Expand|Select|Wrap|Line Numbers
  1.  
  2.       //read in data values 
  3.       numTypeAuto = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Number of Automobiles:")); 
  4.  
  5.       Automobile[] automobile; //declare the Automobile array 
  6.       automobile = new Automobile[numTypeAuto]; //and then create it      
  7.       for (int i = 0; i < automobile.length; i++) { //create a new Automobile and assign values      
  8.  
  9.       autoName = JOptionPane.showInputDialog(null,"Enter Favourite Automobile name:"); 
  10.       autoIDNumber = Integer.parseInt(JOptionPane.showInputDialog
  11.               (null,"Enter Automobile ID Number:")); 
  12.  
  13.  

It should not be added as this...

Expand|Select|Wrap|Line Numbers
  1.  
  2.       Automobile[] automobile; //declare the Automobile array 
  3.       automobile = new Automobile[numTypeAuto]; //and then create it      
  4.       for (int i = 0; i < automobile.length; i++) { //create a new Automobile and assign values      
  5.  
  6.       //automobile[i] = new Automobile( );   // don't need this here    
  7.       automobile[i].setSizeOfArray( numTypeAuto );  //create user-defined number of Automobiles to be entered
  8.       automobile[i].setAutoName  ( autoName   );            //create instances of each favourite new Automobile
  9.       automobile[i].setAutoID   ( autoIDNumber    );    //create instances of each favourite new Automobile ID number
  10.       automobile[i].setRating1   ( rating1    );    //create instances of each favourite new Automobile rating scale 1
  11.       automobile[i].setRating2   ( rating2    );    //create instances of each favourite new Automobile rating scale 2
  12.       automobile[i].setRating3   ( rating3    );    //create instances of each favourite new Automobile rating scale 3
  13.       automobile[i].setAutoSpeed ( autoSpeed    );  //create speed level of each favourite new Automobile speed
  14.  
  15.  
I was getting one instance of an auto because my code never looped through anything. I'd already gotten what needed from each InputBox, so the code stopped after enterring 1 auto.

Now I need to use bubble sort example to see how to fit it in, wish me luck there:-)
Apr 28 '08 #191
Dököll
2,364 Expert 2GB
...I was getting one instance of an auto because my code never looped through anything. I'd already gotten what needed from each InputBox, so the code stopped after enterring 1 auto.

Now I need to use bubble sort example to see how to fit it in, wish me luck there:-)
Truth be told, I think I stunk pretty bad for the last exam; I actually thought I had one more month, was probably sleeping, a lot of sleepless nights. I think I did alright in the final exam, have yet to see the scores, gbut should surely pass. Above code was satisfactory as is, will see what score I get, part of the code was in fact a topic in the exam...

One should never stop programming, by the way, if that's your thing, school is out, well really 'til September but... I have what I think is a puzzle, not relating to school, I figured why not include in this blog... I muscled through this bit of code from my book at work, it works wonders, what a surprise;-) Tried it @ home just to get curious, lo and behold, it does not work; is this because my system is not part of a network?

What do you make of it?

Oh!

Here's the error I am getting:

Expand|Select|Wrap|Line Numbers
  1.  
  2. java.lang.NoClassDefFoundError: dbMailMePackage/MailMeSolid 
  3. Caused by: java.lang.ClassNotFoundException: dbMailMePackage.MailMeSolid 
  4.     at java.net.URLClassLoader$1.run(Unknown Source)
  5.     at java.security.AccessController.doPrivileged(Native Method)
  6.     at java.net.URLClassLoader.findClass(Unknown Source)
  7.     at java.lang.ClassLoader.loadClass(Unknown Source)
  8.     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  9.     at java.lang.ClassLoader.loadClass(Unknown Source)
  10.     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
  11. Exception in thread "main" 
  12.  
  13.  
Code firing such error:-(

Expand|Select|Wrap|Line Numbers
  1.  
  2. package dbMailMePackage;
  3.  
  4. /**
  5.  * @author Köll S. Cherizard
  6.  * @project MailFrenzy
  7.  * @version 200800001405
  8. */
  9.  
  10. import java.util.*;
  11. import javax.mail.*;
  12. import javax.activation.*;
  13. import javax.mail.internet.*;
  14.  
  15. public class MailMeSolid {
  16.  
  17.     public static void main(String[] args) {
  18.         String to = "this is just forthe site_@gmail.com";
  19.         String from = "this is just forthe site_@gmail.com";
  20.         String host = "smtp.gmail.com";
  21.  
  22.         Properties properties = new Properties();
  23.         properties.put("mail.smtp.host", host);
  24.         properties.put("mail.debug", "false");
  25.         Session session = Session.getInstance(properties);
  26.         try {
  27.             Message msg = new MimeMessage(session);
  28.             msg.setFrom(new InternetAddress(from));
  29.             InternetAddress[] address = {new InternetAddress(to)};
  30.             msg.setRecipients(Message.RecipientType.TO,address);
  31.             msg.setSubject("&&&");
  32.             msg.setSentDate(new Date());
  33.             msg.setText("??? " +
  34.                         "!!!.\n"+
  35.                         "%%%");
  36.             Transport.send(msg);}
  37.         catch (MessagingException messenger) {
  38.             messenger.printStackTrace();}
  39.         }
  40.  
  41. }
  42.  
  43.  
Beats me guys, please let me know what you think is happening that I do not see.

Thanks for your help!

Dököll
May 15 '08 #192
JosAH
11,448 Expert 8TB
[quote=Dököll]
Expand|Select|Wrap|Line Numbers
  1.  
  2. java.lang.NoClassDefFoundError: dbMailMePackage/MailMeSolid 
  3. Caused by: java.lang.ClassNotFoundException: dbMailMePackage.MailMeSolid 
  4.     at java.net.URLClassLoader$1.run(Unknown Source)
  5.     at java.security.AccessController.doPrivileged(Native Method)
  6.     at java.net.URLClassLoader.findClass(Unknown Source)
  7.     at java.lang.ClassLoader.loadClass(Unknown Source)
  8.     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  9.     at java.lang.ClassLoader.loadClass(Unknown Source)
  10.     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
  11. Exception in thread "main" 
  12.  
  13.  
There was no need to post your code because the exception indicates that the
JVM can't even find your class. It's a classpath problem; your .class file should
be stored somewhere in a path /foo/bar/dbMailMePackage/MailMeSolid.class.
Then your classpath should contain the directory /foo/bar/

kind regards,

Jos
May 15 '08 #193
Dököll
2,364 Expert 2GB
[quote=JosAH]
Expand|Select|Wrap|Line Numbers
  1.  
  2. java.lang.NoClassDefFoundError: dbMailMePackage/MailMeSolid 
  3. Caused by: java.lang.ClassNotFoundException: dbMailMePackage.MailMeSolid 
  4.     at java.net.URLClassLoader$1.run(Unknown Source)
  5.     at java.security.AccessController.doPrivileged(Native Method)
  6.     at java.net.URLClassLoader.findClass(Unknown Source)
  7.     at java.lang.ClassLoader.loadClass(Unknown Source)
  8.     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  9.     at java.lang.ClassLoader.loadClass(Unknown Source)
  10.     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
  11. Exception in thread "main" 
  12.  
  13.  
There was no need to post your code because the exception indicates that the
JVM can't even find your class. It's a classpath problem; your .class file should
be stored somewhere in a path /foo/bar/dbMailMePackage/MailMeSolid.class.
Then your classpath should contain the directory /foo/bar/

kind regards,

Jos
Very cool Jos, thanks for your input on this one!

I wanted to demonstrate that indeed a class does exist, I just could not figure out why it was saying it is not there. So .class is not where it should be eh!

Super, let's try that, thanks Jos...

In a bit!
May 15 '08 #194
JosAH
11,448 Expert 8TB
[quote=Dököll]
Very cool Jos, thanks for your input on this one!

I wanted to demonstrate that indeed a class does exist, I just could not figure out why it was saying it is not there. So .class is not where it should be eh!

Super, let's try that, thanks Jos...

In a bit!
The classpath mechanism is extremely easy actually: if you have a class
foo.bar.YourClass (foo and bar are package names) then a .class file should
exist somewhere in a directory X/foo/bar/YourClass.class. The X part should
be an element in the classpath variable. Check your compiler flags for the
option that stores the compiled files in their correct directory.

kind regards,

Jos
May 15 '08 #195
Dököll
2,364 Expert 2GB
[quote=JosAH]

The classpath mechanism is extremely easy actually: if you have a class
foo.bar.YourClass (foo and bar are package names) then a .class file should
exist somewhere in a directory X/foo/bar/YourClass.class. The X part should
be an element in the classpath variable. Check your compiler flags for the
option that stores the compiled files in their correct directory.

kind regards,

Jos
Got it Jos, finnaly working... stupendous affair truly, I am so excited...

Looks like the class vanished when copied over. Why would a class vanish in this fashion... I am sure there are logical reasons, will post as I find them.

I also have a DHCP enabled connection to the internet, couldn't get pass my own network, doulble dose of reality there:-)

I am attaching a new piece to this project, will see you in a bit.

Thanks again, Jos!

Dököll
Jun 3 '08 #196
Dököll
2,364 Expert 2GB
[quote=Dököll]
Got it Jos, finnaly working... stupendous affair truly, I am so excited...

Looks like the class vanished when copied over. Why would a class vanish in this fashion... I am sure there are logical reasons, will post as I find them.

I also have a DHCP enabled connection to the internet, couldn't get pass my own network, doulble dose of reality there:-)

I am attaching a new piece to this project, will see you in a bit.

Thanks again, Jos!

Dököll
OK, the boss smelt blood, he likes what my GUI does but does not want a standard GUI...

I must now render a Servlet connection to database, grab data out of there email this data as a ticket.

The servlet works fine, the connection to database is a miss though, been at it for 6 hours, almost. I suspect I need to find the right library for MetaData:

import java.sql.SQLException;
import java.sql.Connection;


I had similar problems when I forgot to add the above for the SQL portion. Just getting our normal "...is not a defined type..." for:

Expand|Select|Wrap|Line Numbers
  1. (1) DatabaseMetaData dbMetaData = connection.getMetaData(); 
  2.  
  3. and 
  4.  
  5. (2) ResultSetMetaData resultsMetaData = resultSet.getMetaData(); 
  6.  
Can any of you tell me what if any the library MetaData relates to in Java.

Will post portion of the code for looks, in a moment; got an idea...

Thanks for your help!

Dököll
Jun 6 '08 #197
Dököll
2,364 Expert 2GB
Alright, it was nothing fancy, the idea did not work, I am also trying this using Tomcat 6... Servlet works with Tomcat also but still does not connect to database. At least there are no known issues with Servlets:

Expand|Select|Wrap|Line Numbers
  1.        protected void doGet(HttpServletRequest request, HttpServletResponse response) 
  2.            throws ServletException, IOException {
  3.             response.setContentType("text/html");
  4.     PrintWriter out = response.getWriter();
  5.                 out.println("Looks like it is working");    
  6.                 String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
  7.     String url = "jdbc:microsoft:sqlserver:DataCentral";
  8.     String username = "";
  9.     String password = "";
  10.     String tableName = 
  11.            request. getParameter ("tableName");
  12.     { if ((tableName == null) || (tableName.equals("")))
  13.     tableName = "Library";
  14.     }
  15.           showTable(driver,url,username,password, tableName, out);
  16.     out.println("</CENTER></BODY></HTML>");
  17.     } 
  18.  
Here are the libraries thus far included:

import java.awt.*;
//import java.lang.annotation.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;


Have a look at the portion of the code with the problem:

Expand|Select|Wrap|Line Numbers
  1.        private void showTable(String driver,
  2.     String url,
  3.     String username,
  4.     String password,
  5.     String tableName,
  6.         PrintWriter out) {
  7.         try {
  8.         Class.forName(driver);
  9.     Connection connection = DriverManager.getConnection(url, username, password);
  10.     DatabaseMetaData dbMetaData = connection.getMetaData();
  11.     out.println("<UL>");
  12.         String productName =
  13.         dbMetaData.getDatabaseProductName();
  14.     out.println(" <LI><B>Database:</B> " +
  15.             productName);
  16.                     String productVersion =
  17.         dbMetaData.getDatabaseProductVersion();
  18.     out.println(" <LI><B>Version:</B> " + productVersion );
  19.  
  20.         Statement statement = connection.createStatement();
  21.                 String query = "SELECT * FROM " + tableName;
  22.     ResultSet resultSet = statement.executeQuery(query);
  23.         out.println("<TABLE BORDER=1>");
  24.     ResultSetMetaData  resultsMetaData = resultSet.getMetaData();
  25.         int columnCount = resultsMetaData.getColumnCount();
  26.                    out.println("<TR>");
  27.     for(int i=1; i<columnCount+1; i++) {
  28.     resultsMetaData.getColumnName(i);
  29.     }
  30.                    out.println();
  31.     while(resultSet.next()) {
  32.                    out.println("<TR>");
  33.     for(int i=1; i<columnCount+1; i++) {
  34.                    out.print("<TD>" + resultSet.getString(i));
  35.           }
  36.                    out.println();
  37.       }
  38. }
  39.  
  40.  
This is not all of the code as you can tell tell, perhaps it'll give you an idea what library I must use; import java.lang.annotation did not seem to work, I am still researching that:-)

Again, thanks for your input, lines 10 and 24 or the portions I have issues with it seems.

In a bit...

Dököll
Jun 6 '08 #198
JosAH
11,448 Expert 8TB
Two things I found after a quick skim through the code:

- you don't need that annotation package.
- that loop in lines 27 ... 29 doesn't do anything.

What exactly is the problem? Does it compile? What does it do if it runs?

kind regards,

Jos
Jun 6 '08 #199
Dököll
2,364 Expert 2GB
Two things I found after a quick skim through the code:

- you don't need that annotation package.
- that loop in lines 27 ... 29 doesn't do anything.

What exactly is the problem? Does it compile? What does it do if it runs?

kind regards,

Jos
Greetings, Jos!

Thanks for replying... Below was an attempt to increment columns each time the program reads database columns and found addtional columns next marker, loops until there are none, I am probably reading it wrong:-)

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(int i=1; i<columnCount+1; i++) {
  3.     resultsMetaData.getColumnName(i);
  4.     }
  5.  
  6.  
But to answer your question, the code runs fine and fires a .jsp file, but when I run program using the actual code above, MetaData seems to be an issue.

I'll look at te loop again, and post the error.

Hey, have a good Friday...

In a bit!

Dököll
Jun 6 '08 #200

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Rich Wallace | last post: by
3 posts views Thread by Tony | last post: by
71 posts views Thread by iesvs | last post: by
reply views Thread by delhi institute of management & services | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.