Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code that creates files with the file name as array contents, the file has word with lincs previous for the word before and next for the word after, at present it creates files but !does iterate the array just prints array content as the position specified by array, it should iterate array write array coontent as word a previous linc with previous array content and neyt linc with neyt array content

is it possible to insert images here would want to insert image and show how the output loocs


public class WrHtmlWithLincs 
   {
    public static void main(String args[]) throws IOException 
    { 
    BufferedWriter bw = null;
    FileWriter fw = null;         

    try
    {   
        String word[];       
        word = new String[11];                                              
        word[0] = "Software";                                                          
        word[1] = "Java";                                                   
        word[2] = "Android";                                                
        word[3] = "Code";                                                   
        word[4] = "Computer Science";                                                 
        word[5] = "Satellite Navigation";                                        
        word[6] = "Communications";    
        word[7] = "Hongyan"; 
        word[8] = "Calculator";                                             
        word[9] = "JavaScript";                                             
        word[10] = "Stanford";             

        for (int y = 0; y < word.length; y++)
        {            
        try
        {
        fw = new FileWriter("F:\\" + Ad + ".html");            
        bw = new BufferedWriter(fw);
        bw.write("<Table align='center' border='4' color = 'cyan'>");    

        bw.write("<TR>"); 

        bw.write("<TD>"); 
        bw.write("<a href=" + word[y] + ".html>");                                 
        bw.write("Previous"); 
        bw.write("</a>");  
        bw.write("</TD>"); 

        bw.write("<TD>"); 
        bw.write(word[y]); 
        bw.write("</TD>");          

        bw.write("<TD>"); 
        bw.write("<a href=" + word[y] + ".html>");                                 
        bw.write("Next"); 
        bw.write("</a>");
        bw.write("</TD>");             

        bw.write("</TR>");            
        bw.write("<Table>");                        
        }
        catch(IOException d)
        {
        d.printStackTrace();
        }
        finally
        {
        try {
            if (bw != null)
                bw.close();

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

        }          

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

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

    }
}
}


changed the code now but the error is :
java.lang.NullPointerException
	at java.io.Writer.write(Writer.java:157)
	at html.WrHtmlWithLincs.main(WrHtmlWithLincs.java:50)


What I have tried:

array iteration with for and code refraction
Posted
Updated 27-Aug-18 5:43am
v3

You now have a 14 word array that only contains 11 elements, so as soon as y equals 12 your program will crash. And you are also using the exact same reference for the Previous, current and Next items.

Try and think about exactly what you want in each field. If the current item is the first in the list then there is no Previous entry. And if the current item is the last in the list then there is no Next entry.
 
Share this answer
 
Comments
solar os 27-Aug-18 11:45am    
thats cool, now the program runs but it still does not iterates the array as it should Previous and Next does not move the pages forward and backwards as the array contents are ty
Richard MacCutchan 27-Aug-18 12:32pm    
No idea what that means. All your program does is to write some HTML to a file. What were you expecting to happen?
solar os 28-Aug-18 8:00am    
the program should write html with a word surrounded by previous and neyt lincs, the lincs are array contents which means that each array content has a corresponding named html document. Button Click event for next button is that it takes you to next page so if you are on ComputerScience.html it would take you to SatelliteNavigation.html and if you click on previous button that would take you to Code.html ty
Richard MacCutchan 28-Aug-18 8:08am    
Yes, and I have already explained what your code needs to do in order to write this information. Using the example above where variable y is the index to the current item. The previous item should be referred to as word[y-1]. However, if y equals zero, then there is no previous item, so it should not be written. Similarly there is no next item if y equals word.length - 1, i.e. this is the last item in the list.
solar os 30-Aug-18 11:35am    
did write word[y], word[y+1], word[y+2] but got the error "java.lang.ArrayIndexOutOfBoundsException: 11 at html.WrHtmlWithLincs.main(WrHtmlWithLincs.java:55)"
Java
        bw.write("<a href=" + word[0] + ".html>");  

...
        bw.write(word[1]); 

...
                               
        bw.write("<a href=" + word[2] + ".html>");                                 

You are writing the same data in every file.

If you are trying to add backward and forward links, then you need to use a simple indexed for loop. You also need to add dummy elements after the last item and before the first one, in order that your forward and backward links do not cause an exception for the last and first items.
 
Share this answer
 
v3
Comments
solar os 27-Aug-18 11:54am    
shouldnt u use variable instead of numbers !word[0] !word[1] !word[2] but word[y] word[y + 1] word[y + 2]
Richard MacCutchan 27-Aug-18 12:34pm    
Of course. That was what I was trying to explain to you.
solar os 30-Aug-18 12:11pm    
is there a site where you could get technical consultant for stoc options as shareholder what that would do is serve few purposes which are hasten the product development and more reliable robust software ty
solar os 30-Aug-18 12:11pm    
is there a site where you could get technical consultant for stoc options as shareholder what that would do is serve few purposes which are hasten the product development and more reliable robust software collaborative workforce is way forward ty
Richard MacCutchan 30-Aug-18 12:31pm    
You need to have a product and a business plan first. And I'm sorry to say that you are a long way short of that at the moment.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900