使用< a href>创建secuential linc的Java数组 [英] Java array that creates secuential lincs with <a href>
问题描述
使用文件名作为数组内容创建文件的代码,该文件在前面和后面的单词之前有单词lincs,目前它创建文件但是!迭代数组只打印数组内容作为由数组指定的位置,它应该迭代数组写入数组coontent作为单词前一个linc与之前的数组内容和neyt linc与neyt数组内容
是否可以插入图像这里想要插入图像并显示输出方法如何
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)
我的尝试:
使用for和代码折射的数组迭代
What I have tried:
array iteration with for and code refraction
推荐答案
现在你有一个14字的数组,只包含11个元素,所以只要y
等于12,程序就会崩溃。并且您也使用与Previous,Current和Next项完全相同的参考。
尝试并考虑每个领域中您想要的内容。如果当前项目是列表中的第一项,则没有上一个条目。如果当前项是列表中的最后一项,则没有下一个条目。
You now have a 14 word array that only contains 11 elements, so as soon asy
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.
bw.write("<a href=" + word[0] + ".html>");
...
bw.write(word[1]);
...
bw.write("<a href=" + word[2] + ".html>");
您在每个文件中写入相同的数据。
如果您尝试向前和向后添加链接,然后你需要使用一个简单的索引for循环。您还需要在最后一项之后和第一项之前添加虚拟元素,以便前向和后向链接不会导致最后一项和第一项的例外。
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.
这篇关于使用< a href>创建secuential linc的Java数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!