Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   changing size of an array (http://www.programmingforums.org/showthread.php?t=9185)

Eric the Red Apr 3rd, 2006 4:32 PM

changing size of an array
 
I'm new to java and I’m trying to change the dimension of an array. So I basically use this " String inputStringFinal [] = new String [intNumberofLines + 1];" (blue writing) command every time I want to change the size of the array. the yellow writing is just there to provide a default value if it hasn't been declared already. I know the blue writing deletes everything in the array but that's what I want. If you look at the orange writing it gives a declaration error. Apparently the array has been declared twice (orange and blue writing).

My question is: how am I going to resize the array on the bottom (blue writing) if it needs to be declared on the top in consequence, resetting the array.


:

// The "License" class.
import java.awt.*;
import hsa.Console;
import java.util.Random;
import java.io.*;

public class License
{
    static Console c;          // The output console

    public static void main (String [] args) throws IOException
    {
        c = new Console ();
        boolean blnMusic = false; // use for controlling the music
        boolean blnQuit = false; // used to control the while loop during the program.
        boolean blnWriteFile = true;
        int intNumberofLines = 0;
        boolean blnStringDeclared = false;
       
       
        while (blnQuit == false)
        {
            blnWriteFile = true; // back to default
            /// the menu which i'd like to change to actual GUI later
            c.println ("Welcome. What would you like to do?");
            c.println ("1. View all of the license plates");
            c.println ("2. Randomize and register a license plate");
            c.println ("3. Create and register a Custom licence plate.");
            c.println ("4. Change a license plates standard expiry date.");

            if (blnMusic == false)
            {
                c.println ("5. Begin music");
            }
            else
            {
                c.println ("5. Stop Music");
            }
            c.println ("6. Quit");


            int inputAns = c.readInt ();
            String tempLicense = new String (""); // the temp license from each iteration

            // end of menu
            if (inputAns == 1)
            {
                c.print ("the list is as Follows");
                if (blnStringDeclared == false)
                {
                String inputStringFinal [] = new String [1];  // just so that we have a default array.
                blnStringDeclared = true;
                }
               
                for (int arraysize = 0 ; arraysize < intNumberofLines + 1 ; arraysize++)
                {
                    c.println ("Licence plates:");
                    c.println ("#" + arraysize + 1 + " is: " + inputStringFinal [arraysize]);
                }



            }

            if (inputAns == 2)
            {
                int tempinttoString [] = new int [4]; // used to hold the string before it converts to a string

                Random x = new Random (); // instantiates x a from the random class

                // filling the array with int values; (later to be converted to ascii char)
                c.println ("go");

                for (int i = 0 ; i < 4 ; i++)
                {
                    tempinttoString [i] = x.nextInt (26) + 65; //
                    if (tempinttoString [i] > 91 || tempinttoString [i] < 65)
                    {
                        //error = true;
                        c.println ("tempstring:" + tempinttoString [i]);
                    }
                }
                //}
                c.print ("done");

                String strLicense = new String ("");
                // here we add all the letters that we are working with
                for (int m = 0 ; m < 4 ; m++)
                {
                    String aChar = new Character ((char) tempinttoString [m]).toString ();
                    strLicense = strLicense + aChar;
                }
                // c.println ("the entire 3 letters are:" + strLicense);
                // c.println ("rover out");
                strLicense = strLicense + " "; // the space before integers are applied.

                int rand = 1;
                for (int intcount = 0 ; intcount < 3 ; intcount++)
                {

                    rand = x.nextInt (9) + 0;
                    strLicense = strLicense + rand;
                }
                tempLicense = strLicense;
                c.print ("strLicense is:" + strLicense);
            }


            else if (inputAns == 3)
            {
                //c.print ("okay");
                int tempLength;
                boolean blnValidPlate = false; // so that the default of the plate is not valid.

                while (blnValidPlate == false) // runs until valid
                {
                    //c.print ("okay");
                    c.println ("What is the license plate you would like?");
                    //c.println ("");
                    //String tempLicense = new String ("");


                    tempLicense = c.readLine (); // the user's license plate
                    tempLength = tempLicense.length (); // takes length and subtracts 1
                    if (tempLength < 9)
                    {
                        c.println ("your license plate is valid.");
                        c.println ("your plates expiry date is set to 10 minutes.");
                        c.println ("Feel free to change the default date.");
                        c.println ("");
                        blnValidPlate = true; // plate is now valid
                    }

                    else
                    {
                        c.println ("Your plate is not valid. What are you trying to pull here?");
                        c.println ("");
                    }
                }
            }

            else if (inputAns == 6)
            {
                c.println ("Don't forget about the expiry date on your license plates.");
                c.println ("Have a great day");
                blnQuit = true;
                blnWriteFile = false; // so that data isn't written to that file

            }
            else
            {
                blnWriteFile = false; // so that we don't write to the file
            }



            /// this process is done no matter what at the end of everything
            //c.println ("i'm at blnWritefile");
            if (blnWriteFile == true)
            {
                c.println ("i'm at blnWritefile");
                BufferedReader input2 = new BufferedReader (new FileReader ("C:\\output.txt"));

                //int intNumberofLines = 0; // default 0 because there is always 1 line to read

                // first we get all the data so that it isn't written over
                intNumberofLines = Integer.valueOf (input2.readLine ()).intValue ();
                c.print (intNumberofLines);

                String inputString [] = new String [intNumberofLines]; // array large enough to hold all text data
                for (int y = 0 ; y < (intNumberofLines) ; y++) // +1 because we've already read the int for line 1.
                {
                    inputString [y] = input2.readLine (); // because array starts at 0 not 1.
                    c.print ("String is: " + inputString [y]);
                }

                String inputStringFinal [] = new String [intNumberofLines + 1];  // +1 because the tempLicense is added to the array.
                blnStringDeclared = true; // so it can be used when (inputAns = 1)
               
                    // cloning array and adding the final License
                    for (int i = 0 ; i < intNumberofLines ; i++) // cloning the array
                {
                    inputStringFinal [i] = inputString [i];
                }

                inputStringFinal [intNumberofLines] =  tempLicense; // the last boundary is used for the license just created
               
                //File licenseList = new file ("C:\licensePlate.text");
                //PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter ("C:\\output.txt")));
                //out.print (tempLicense);
                //out.close ();
            }
        }


        // Place your program here.  'c' is the output console
    } // main method
} // License class


Xyhm Apr 3rd, 2006 5:46 PM

You can't declare the same variable twice. But you can, I think, declare "String[] inputStringFinal" somewhere in the beginning, and then assign to "inputStringFinal".

This doesn't work:

:

String [] s = new String[1];
String [] s = new String[2];


But this does:

:

String[] s;
s = new String[1]; 
s = new String [2];


jaeusm Apr 3rd, 2006 6:44 PM

Or you could just use an ArrayList, which takes care of the resizing for you.

Eric the Red Apr 3rd, 2006 8:19 PM

Quote:

Originally Posted by Xyhm
You can't declare the same variable twice. But you can, I think, declare "String[] inputStringFinal" somewhere in the beginning, and then assign to "inputStringFinal".

This doesn't work:

:

String [] s = new String[1];
String [] s = new String[2];


But this does:

:

String[] s;
s = new String[1]; 
s = new String [2];


Thanks!! That solves the problem.


All times are GMT -5. The time now is 12:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC