so rather then continuing the same bloated thread, i started a new one.
I have (with a lot of help) created an j applet that will add a list of names to an arraylist and then randomly choose 1 and display it as a j label.
this all works but my issue is the next step.
i want to allow the user to then read about the samurai name that was just generated.
to do this i added a new jbutton then will give the user the option to go to the website where the samurai is listed or generate a new name.
the exact problem is this error that shows up in the console when i attempt to open the url.
Saigo TsugumichiException in thread "AWT-EventQueue-1" java.lang.IllegalArgumentException at java.net.URI.create(URI.java:842) at japanese_samurai.actionPerformed(japanese_samurai.java:83) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6038) at javax.swing.JComponent.processMouseEvent(JComponent.java:3260) at java.awt.Component.processEvent(Component.java:5803) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4410) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) Caused by: java.net.URISyntaxException: Illegal character in query at index 54: http://wiki.samurai-archives.com/index.php?title=Saigo Tsugumichi at java.net.URI$Parser.fail(URI.java:2809) at java.net.URI$Parser.checkChars(URI.java:2982) at java.net.URI$Parser.parseHierarchical(URI.java:3072) at java.net.URI$Parser.parse(URI.java:3014) at java.net.URI.<init>(URI.java:578) at java.net.URI.create(URI.java:840) ... 25 more
Saigo Tsugumichi is obviously the name that was selected at the time.
i don't know how exactly to fix this issue as most online forums iv looked at don't exactly fit this problem.
i'm wondering if i should make another class just for the url?
the entire source code is here:
main class:
//name: Ryan Jolin
//Purpose: to randomly generate the name of a famous samurai
//date created: Thur, Nov 10, 2011 -12:24am-
//date revised: Fri, Nov 11, 2011 -5:21pm-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class japanese_samurai extends JApplet implements ActionListener
{
Container pane = getContentPane();
Container con = getContentPane();
JLabel headline = new JLabel("Click the randomizer to recieve a famous japanese samurai name");
JLabel response = new JLabel("");
Font hf1 = new Font("Helvetica", Font.BOLD,35);
Font hf2 = new Font("Times New Roman", Font.BOLD,22);
Font hf3 = new Font("Arial", Font.BOLD,22);
Font hf4 = new Font("Candara", Font.BOLD,22);
Font hf5 = new Font("Gothic", Font.BOLD,22);
JButton gen = new JButton("Generate");
JButton info = new JButton("Click here to read about this Samurai");
String name;
public japanese_samurai()
{
japanese_samurai_scan samuraiScan = new japanese_samurai_scan();
name = samuraiScan.getSamuraiName();
}
public void init()
{
pane.setLayout (new FlowLayout());
con.setLayout (new FlowLayout());
pane.setBackground(Color.BLACK);
headline.setFont(hf1);
headline.setForeground(Color.WHITE);
pane.add(headline);
//my generator button
con.add(gen);
gen.setActionCommand("gen");
gen.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//new generated name
if ("gen".equals(e.getActionCommand ()))
{
japanese_samurai_scan samuraiScan = new japanese_samurai_scan();
name = samuraiScan.getSamuraiName();
response.setText(name);
response.setFont(hf2);
response.setForeground(Color.YELLOW);
pane.add(response);
gen.setEnabled(true);
con.add(info);
info.setActionCommand("info");
info.addActionListener(this);
validate();
}
//my code for redirection.
if ("info".equals(e.getActionCommand ()))
{
System.out.print(name);
try
{
String url = "http://wiki.samurai-archives.com/index.php?title=" + name;
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException d)
{
System.out.println(d.getMessage());
}
}
}
}
scanner class:
import java.util.*;
import java.io.File;
public class japanese_samurai_scan
{
ArrayList<String> names = new ArrayList<String>();
Random random = new Random();
Scanner scanner;
public japanese_samurai_scan()
{
try
{
scanner = new Scanner(new File("japanese_samurai.txt"));
}
catch (java.io.FileNotFoundException e)
{
System.out.println(e.getMessage());
}
while (scanner.hasNext())
{
names.add(scanner.nextLine().trim());
}
}
public String getSamuraiName()
{
String name = names.get(random.nextInt(names.size()));
return name;
}
}
any suggestions would be greatly appreciated.

New Topic/Question
Reply




MultiQuote




|