Click here to Skip to main content
14,328,801 members
Rate this:
Please Sign up or sign in to vote.
See more:
Code that should print array contents , prints the array word contents as secuence but does not prints the satellite contents as numbers, prints cs and after four iterations prints satellite then after four iterations Navigation and then Honmgyan, should print
CS
Software
Satellite
Java
Navigation
Android
Hongyan
Code

public class ReadAndWriteFromArrays
  {     
    public static void main(String[] args)
    {
    BufferedWriter bw = null;
    FileWriter fw = null;                                 
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            String word[];
            word = new String[4];
            word[0] = "Software";
            word[1] = "Java";
            word[2] = "Android";
            word[3] = "Code";

            String Satellite[];
            Satellite = new String[4];
            Satellite[0] = "CS";
            Satellite[1] = "Satellite";
            Satellite[2] = "Navigation";
            Satellite[3] = "Hongyan";

                for(String Ad : Satellite)
                for(String Ac : word)
                {
                    String FILENAME = "F:\\"+ Ad +".html";
                    try
                    {
                        System.out.print("Word: " + Ad);
                        String French = reader.readLine();

                        System.out.print("Words:" + Ac);
                        String Android = reader.readLine();

                        System.out.print("a: ");
                        String a = reader.readLine();   

                        System.out.print("b: ");
                        String b = reader.readLine();
                    }

                    catch(IOException d)
                    {                                             
                        d.printStackTrace();
                    }
                    finally{
                        try {
                            if (bw != null)
                                bw.close();

                            if (fw != null)
                                fw.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            try {
                if (bw != null)
                    bw.close();

                if (fw != null)
                    fw.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
}              
}


What I have tried:

changed for loops , there is another way to write several for loops so that different array contents are printed as numbered
Posted
Updated 2-Aug-18 2:56am
Comments
Richard MacCutchan 2-Aug-18 8:50am
   
I just tested your code and it appears to work correctly. However, from your description I am guessing that you need to use array index values in a single for loop rather than foreach constructs.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have a nested loop. So the inner code will be executed 4 * 4 = 16 times.

If you know for sure that both arrays have the same size and corresponding items at the same index just use one loop and print the items:
for (int i = 0; i < word.length; i++)
{
    System.out.println(Satellite[i]);
    System.out.println(word[i]);
}
The above would print it out as requested in your question.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

for(int i = 0; i < 4; ++i)
{
//    String FILENAME = "F:\\"+ Ad +".html";
    System.out.println("Words:" + Satellite[i]);
    System.out.println("Word: " + word[i]);
}
   
Comments
four systems 2-Aug-18 13:25pm
   
Thancs, what about the code what it does is it takes user nput and then prints from Array Satellite and Word, so if user enters Android the arrays automatically add values
Richard MacCutchan 3-Aug-18 3:29am
   
You need to use a List<t> or similar dynamic collection type, so you can add items to it as the program is running.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100