printStackTrace() is a method in the Exception class. You need to add the object: e. in front of it:
e.printStackTrace();
Where is the print out of the value of the input variable on line 14?
printStackTrace() is a method in the Exception class. You need to add the object: e. in front of it:
e.printStackTrace();
Where is the print out of the value of the input variable on line 14?
ok done that but still showing
List.java:20: cannot resolve symbol
symbol : method printLn (java.lang.String)
location : class java.io.PrintStream
System.out.printLn("Invalid");
That's strange. Your code uses that method many times doesn't it? why is it misspelled this one time?
Copy a good one and paste it to replace this one.
I did as you suggested and copied and pasted that line of code that had previously worked and everything now compiles file, however its still not allowing me to enter text, if i try for example to enter "paris" into the list it comes up with the following message
java.lang.NumberFormatException: For input string: "paris"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Interger.parseInt(Integer.java:468)
at java.lang.Interger.parseInt(Integer.java:518)
at List.readValue(List.java:15)
at List.insert(List.java:39)
at Listtest.main(Listtest.java:20)
The message spells out that your code reads the input into a String then tries to parse that as an Integer.
"paris" is not an integer.
You now have two values to read, one is an integer the other is a string, you can't use exactly the same code for both.
ok so how do i begin to change these sections of code
It's time for fewer questions and more thinking. You have to try to work this out for yourself.
honestly I have been trying I've tried to change the line
value = Integer.parseInt(input);
to
value = Integer.toString(input);
but that doesnt seem to work and also tried many other alternatives but nothing I try to do seems to work
that doesnt seem to work
Please explain what happens. Post the full contents of the error messages.
Post the ccnsole showing the program's output.
It shows error message
List.java:15: cannot resolve symbol
symbol : method toString (java.lang.String)
location : class java.lang.Integer
value = Integer.toString(input);
1 error
You need to read the API doc for the Integer class to get the correct syntax and parameters for its toString() method.
well i've updated all the code now and getting more errors than ever before I've been at this all day, the new code looks as follows
list.java
import java.io.*;
public class List {
public static Listnode head;
public List() {
head = new Listnode("");
}
public static int readValue( )throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input="";
String value ="";
boolean OK = false;
while (!OK){
try{
input = in.readLine();
value = input;
OK = true;
}
catch (Exception e){
System.out.println("Invalid - please enter integer value");
}
}
return value;
}
public static boolean isEmpty() {
if (head.next == null)
return true;
else
return false;
}
public static void insert() throws IOException {
Listnode current, previous, temp;
String value;
System.out.print("Enter number to insert, 0 if no more: ");
value = readValue();
while (value != 0) {
temp = new Listnode(value);
current = head;
previous = head;
while ((current.next != null) && (value > current.value)) {
previous = current;
current = current.next;
}
if (value < current.value) {
temp.next = current;
previous.next = temp;
} else
current.next = temp;
System.out.print("Enter number to insert, 0 if no more: ");
value = readValue(); }
}
public static void search() throws IOException {
Listnode current = head.next;
String sought;
boolean found = false;
if (isEmpty())
System.out.println("List is empty ");
else {
System.out.print(" Value sought: ");
sought = readValue();
while ((!found) && !(current == null)) {
if (current.value == sought) {
System.out.println(sought + " is in the list ");
found = true;
}
current = current.next;
}
if (!found)
System.out.println("Item is not in the list ");
}
}
public static void deleteItem() throws IOException {
Listnode current, previous, temp;
String sought;
boolean found = false;
if (isEmpty())
System.out.println("List is empty ");
else {
System.out.print("Item to delete: ");
sought = readValue();
current = head;
previous = head;
while (!found && !(current == null)) {
if (current.value == sought) {
previous.next = current.next;
System.out.println(sought + " deleted from list ");
found = true;
}
else {
previous = current;
current = current.next;
}
}
if (!(found))
System.out.println("Item is not in the list ");
}
}
public static void writeList() {
Listnode current = head.next;
if (isEmpty())
System.out.println("List is empty ");
else {
while (current != null) {
System.out.println(current.value);
current = current.next;
}
}
}
}
Listtest.java
import java.io.*;
public class Listtest {
public static int readValue( ) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input ="";
String value ="";
boolean OK = false;
while (!OK){
}
return value;
}
public static void main(String[] args) throws IOException {
List mylist = new List();
int choice = 7;
System.out.println("Start by adding some items to the list...");
mylist.insert();
while (choice != 0) {
System.out.println(
"------------------------------------------");
System.out.print(
"1..Write out list 2..Insert items 3..Search 4..delete 0..Quit: ");
choice = readValue();
if (choice == 1)
mylist.writeList();
else if (choice == 2)
mylist.insert();
else if (choice == 3)
mylist.search();
else if (choice == 4)
mylist.deleteItem();
}
}
}
Listnode.java
public class Listnode {
String value;
Listnode next;
public Listnode(String data) {
value = data;
next = null;
}
}
The error messages are as follows
Listtest.java:13:incompatible types
found : java.lang.String
required: int
return value;
List.java:23: incompatible types
found : java.lang.String
required: int
return value;
List.java:38: incompatible types
found : int
required: java.lang.String
value = readValue();
List.java:39:operator !=cannot be applied to java.lang.String,int
while (value !=0 {
List.java:43: operator (cannot be applied to java.lang.String,java.lang.String
while ((current.next !=null) && (value ) current.value)){
List.java:47:operator (cannot be applied to java.lang.String,java.lang.String
if (value < current.value){
List.java:53: incompatible types
found : int
requires: java.lang.String
value = readValue();
List.java:65: incompatible types
found : int
requires: java.lang.String
sought = readValue();
List.java:86: incompatible types
found : int
requires: java.lang.String
sought = readValue();
The "required" and "found" errors mean you are using the wrong type of data in the statements with those errors.
You need to change the datatype of the variables in those statements to be what the "required" type is.
Look at each error message and change the variable to be the "required" type.
i thought String was a class name?
String is a class name. But you cannot use string instead of String in java it is case sensistive so String and string are not the same as NormR1 had already explained.
Refer Java API for further clarification.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.