n my code there is a syntax error. i am trying to take out each character from inputted string and store each character into a linked list. i keep getting a syntax error stating that "non static method insertFirst(char) cannot be referenced in a static context" I have no idea why this is happening and i need help. I cannot change anything in the link and linkedlist objects i have instructions to rely rigidly to the textbooks code and i cannot change the data types. i would prefer you to explain the problem and solution in words. language is in Java CODE MAIN FUNCTION import java.util.Scanner; /** * Write a description of class test here. * * @author (your name) * @version (a version number or a date) */ public class test { // instance variables - replace the example below with your own public static void main(String[] args) { Scanner input = new Scanner(System.in); linkedList list = new linkedList(); char character; System.out.println("please enter a string."); String inString = input.nextLine(); for(int i = 0; i < inString.length(); i++) { character = inString.charAt(i); linkedList.insertFirst(character); } } } LINKED LIST OBJECT /** * Write a description of class linkedList here. * * @author (your name) * @version (a version number or a date) */ public class linkedList { private link first; public linkedList() { first = null; } public boolean isEmpty() { return (first==null); } public void insertFirst(char id) { link newLink = new link(id); newLink.next = first; first = newLink; } public link deleteFirst() { link temp = first; first = first.next; return temp; } public void displayList() { System.out.print("List (first -->last): "); link current = first; while(current != null) { current.displayLink(); current = current.next; } System.out.println(""); } } LINK OBJECT import java.util.Scanner; /** * Write a description of class test here. * * @author (your name) * @version (a version number or a date) */ public class test { // instance variables - replace the example below with your own public static void main(String[] args) { Scanner input = new Scanner(System.in); linkedList list = new linkedList(); char character; System.out.println("please enter a string."); String inString = input.nextLine(); for(int i = 0; i < inString.length(); i++) { character = inString.charAt(i); linkedList.insertFirst(character); } } }
in my code there is a syntax error. i am trying to take out each character from inputted string and store each character into a linked list. i keep getting a syntax error stating that "non static method insertFirst(char) cannot be referenced in a static context" I have no idea why this is happening and i need help. I cannot change anything in the link and linkedlist objects i have instructions to rely rigidly to the textbooks code and i cannot change the data types. i would prefer you to explain the problem and solution in words. language is in Java
CODE
MAIN FUNCTION
import java.util.Scanner;
/**
* Write a description of class test here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class test
{
// instance variables - replace the example below with your own
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
linkedList list = new linkedList();
char character;
System.out.println("please enter a string.");
String inString = input.nextLine();
for(int i = 0; i < inString.length(); i++)
{
character = inString.charAt(i);
linkedList.insertFirst(character);
}
}
}
LINKED LIST OBJECT
/**
* Write a description of class linkedList here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class linkedList
{
private link first;
public linkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first==null);
}
public void insertFirst(char id)
{
link newLink = new link(id);
newLink.next = first;
first = newLink;
}
public link deleteFirst()
{
link temp = first;
first = first.next;
return temp;
}
public void displayList()
{
System.out.print("List (first -->last): ");
link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println("");
}
}
LINK OBJECT
import java.util.Scanner;
/**
* Write a description of class test here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class test
{
// instance variables - replace the example below with your own
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
linkedList list = new linkedList();
char character;
System.out.println("please enter a string.");
String inString = input.nextLine();
for(int i = 0; i < inString.length(); i++)
{
character = inString.charAt(i);
linkedList.insertFirst(character);
}
}
}
Static methods are those methods which can be called in main() without any instance of the class. This means they can be called directly like a normal function call without creating a object of the class and using it to call the method.
While non-static methods are those which cannot be called independently. They have to be called through a object of the class.
Step by step
Solved in 2 steps