Hey guys!
I'm new to java, I have to write a program using a linked list, that will prompt a user to enter 10 names, and display them reversed. But is has to be a GUI.
Any idea where i can start, or give me some clues, i would really appreciate it.
Hey guys!
I'm new to java, I have to write a program using a linked list, that will prompt a user to enter 10 names, and display them reversed. But is has to be a GUI.
Any idea where i can start, or give me some clues, i would really appreciate it.
Start by making a JPanel within a JFrame with a text box to enter a name, a button to submit it, and a list to display them.
This is what i did. but it keeps giving me a NoSuchMethodError
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class LinkedList2 extends JFrame {
public LinkedList2() {
try{
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
String Response = JOptionPane.showInputDialog(null,"Please enter your 1st name:");
Response = reader.readLine();
JOptionPane.showMessageDialog(null, " your name is " + Response);
}
catch (IOException ioe){
//statement to execute if an input/output exception occurs
JOptionPane.showMessageDialog(null, "An unexpected error occured.");
}
}
public static void main(String[] args) {
new LinkedList2();
}
}
It also tells you which method invocation it's choking on. Read it closely.
I guess the way I do is different. Normally, I would start with the mechanism to accomplish the task without GUI first. In this case, I would implement and complete a class that can add a list of String, and return a reverse display as String. Then I will implement a GUI class that simply read String from GUI, pass it to the core class, and display the reverse display String for the class to the GUI panel again.
Hmm... You dump everything in your constructor??? As Ezzaral said, you should see the name of the missing method in the compiler as well.
Okay, thanks. I have 2 separate codes now, one for the liked list and one for the interface, I'm still trying to put them together.
Oh and it is in the main method
Could we see both of your class codes?
I still have some error here, but not sure where to fix them
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class MyNames {
//our linked list to store the names
static LinkedList<String> myNames = new LinkedList<String>();
int counter = 1;
public MyNames(){
while(counter <10){
addNames();
}
//print methods
System.out.println("As the names were enterd:");
printMe();
System.out.println("\n");
reverseMe(myNames);
System.out.println("\n");
}
private void addNames(){
if(counter == 1){
System.out.println("Please enter the 1st name:");
}
else if(counter == 2){
System.out.println("Please enter the 2nd name:");
}
else if(counter == 3){
System.out.println("Please enter the 3rd name:");
}
else{
System.out.println("Please enter the " + counter + "th name:");
}
//my old way of adding names to the linked list.
//Scanner sc = new Scanner(System.in);
//myNames.add(sc.next());
try{
//attempt at a regular expression
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s= br.readLine();
boolean matches = Pattern.matches("[a-zA-Z]+", s);
if( matches == true){
myNames.add(s);
counter++;
}
else{
System.out.println("error");
}
}catch (IOException e){}
}
//this method prints the names normally
private void printMe(){
for(String x: myNames){
System.out.printf("%s ", x);
}
}
//Method that reveres names in the linked list
private void reverseMe(LinkedList<String> myNames ){
System.out.println("The names in reverse:");
ListIterator<String> reverse = myNames.listIterator(myNames.size());
while(reverse.hasPrevious())
System.out.printf("%s ", reverse.previous());
}
//our main class
public static void main(String[] args) {
new MyNames();
}
}
Post exact error messages. Don't expect others to search and guess.
Type LinkedList does not take parameters
What Java version are you running this on?
1.5.0
It should work fine then. It runs without any errors on my machine on 1.6. The error message reads as though you're running a version older than 1.5.
Okay, maybe I should get the 1.6 and run it again
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.