problem with codes..
Hello all.
Can anyone tell me what is wrong with this code? it wont compile.. it says there's a "(" missing..
Can anyone help?
import java.util.*;
import java.io.*;
publicclass Uno{
publicstaticvoid String insertar(){// here is twhere it says "(" missing..
BufferedReader buf =new BufferedReader(new InputStreamReader(System.in));
LinkedList lista =new LinkedList();
System.out.println("Inserte cuatro estudiantes");
lista.add(0, buf.readLine());
lista.add(1, buf.readLine());
lista.add(2, buf.readLine());
lista.add(3, buf.readLine());
System.out.println(lista);
}
publicstaticvoid borrar(){
System.out.println("Desea borrar un estudiante de la lista? s/n");
String x = buf.readLine();
int y;
if(x.equals(s)){
System.out.println("Digite el numero del estudiante a borrar: 0 al 3");
y = buf.readLine();
if(y > lista.size()){
System.out.println("Posicion inexistente! trate de nuevo.");
insertar();
}
else(y == lista.size()){
lista.remove(y);
}
}
else(x.equals(n)){
System.out.println("No se borro ningun estudiante.");
}
}
publicstaticvoid main(String args[]){
insertar();
borrar();
}
}
public static void String insertar(){ Doesn't that look a little strange to you? (Hint: pick one return type... ;-)
wow my head needs some rest lol i've been dealing with code all day long.. Thanks! :-D
> wow my head needs some rest lol i've been dealing> with code all day long.. Thanks! :-DHey, it happens ;-) Glad I could help.
Hello again!
I'm having problems with the try catch statements it says "Illegal start of type on line 12, and indentifier expected on line 88.."pls take a look
import java.util.*;
import java.io.*;
public class Uno{
try{
public static void insertar() {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Inserte cuatro estudiantes");
lista.add(0, buf.readLine());
lista.add(1, buf.readLine());
lista.add(2, buf.readLine());
lista.add(3, buf.readLine());
System.out.println(lista);
}
catch(IOException e){
System.out.println("I/O Exception...");
}
}
try{
public static void borrar(){
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Desea borrar un estudiante de la lista? digite si o no. s/n");
String x = buf.readLine();
int y;
if(x.equals("s")){
System.out.println("Digite el numero del estudiante a borrar: 0 al 3");
y = buf.read();
switch (y){
case 0:
lista.remove(0);
System.out.println(lista);
case 1:
lista.remove(1);
System.out.println(lista);
case 2:
lista.remove(2);
System.out.println(lista);
case 3:
lista.remove(3);
System.out.println(lista);
default:
System.out.println("Este elemento no se encuentra en la lista");
System.out.println("No se borro ningun estudiante.");
}
}
else if (x.equals("n")){
System.out.println("No se borro ningun estudiante.");
}
else{
System.out.println("Letra incorrecta! trate de nuevo..");
}
}
}
catch(Exception e){
System.out.println("I/O Exception...");
}
public static void main(String args[]){
this.insertar();
this.borrar();
}
}
Your try / catch must be inside your method.
Thanks Navy... let me go over again...
Hello Again... :-P
Um this code compiles just fine.. but there is a problem with the execution of the application. This is a program that uses a Linked List. The user has to input 4 students, then print the list with all the students.. Asking if the user wants to delete any of those students.
The program tells u to type from 0 to 4, to delete the students index or node in this case..
the app is printing the first student 4 times.. and also, when i try to delete a studente it says that the student is not in the list..
//*-Realize un programa que implemente una lista cuyo dato fundamental son las informaciones basicas
// de un estudiante.En esta lista ud. debe insertar el primer estudiante, insertar el ultimo estudiante,
// insertar un estudiante N"que no sea el primero ni el ultimo"y el programa debe tener una funcion que
// permita borrar cualquiera de los estudiantes.
import java.util.*;
import java.io.*;
public class Uno{
public static void insertar() {
try{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Inserte cuatro estudiantes");
lista.add(0, buf.readLine());
lista.add(1, buf.readLine());
lista.add(2, buf.readLine());
lista.add(3, buf.readLine());
System.out.println(lista);
}
catch(IOException e){
System.out.println("I/O Exception...");
}
}
public static void borrar(){
try{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Desea borrar un estudiante de la lista? digite si o no. s/n");
String x = buf.readLine();
int y;
if(x.equals("s")){
System.out.println("Digite el numero del estudiante a borrar: 0 al 3");
y = buf.read();
switch (y){
case 0:
lista.remove(0);
System.out.println(lista);
case 1:
lista.remove(1);
System.out.println(lista);
case 2:
lista.remove(2);
System.out.println(lista);
case 3:
lista.remove(3);
System.out.println(lista);
default:
System.out.println("Este elemento no se encuentra en la lista");
System.out.println("No se borro ningun estudiante.");
}
}
else if (x.equals("n")){
System.out.println("No se borro ningun estudiante.");
}
else{
System.out.println("Letra incorrecta! trate de nuevo..");
}
}
catch(Exception e){
System.out.println("I/O Exception...");
}
}
public static void main(String args[]){
insertar();
borrar();
}
}
A simple demo to show how to iterate through a list:
import java.util.LinkedList;
import java.util.List;
class Student {
private String name;
private int age;
public Student() { }
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String toString() {
return "Name= " + name + ", age= " + age;
}
}
public class Test {
public static void main(String[] argv) {
List<Student> studs = new LinkedList<Student>();
studs.add(new Student("Caleb", 24));
studs.add(new Student("Ned", 30));
studs.add(new Student("Sarah", 25));
studs.add(new Student("Sally", 18));
for( Student s : studs ) System.out.println(s);
}
}
Cool now i can delete the student.. but still there is an issue..When i print out the student list, it will show the first student 4 times again.. How can this be solved?
Post updated code with code tags.
> Cool now i can delete the student.. but still there
> is an issue..
>
> When i print out the student list, it will show the
> first student 4 times again..
>
> How can this be solved?
I just showed you how to iterate through a list and display each item ...
Here's the code...
import java.util.*;
import java.io.*;
public class Uno{
public static void insertar() {
try{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Inserte cuatro estudiantes");
lista.add(0, buf.readLine());
lista.add(1, buf.readLine());
lista.add(2, buf.readLine());
lista.add(3, buf.readLine());
System.out.println(lista);
}
catch(IOException e){
System.out.println("I/O Exception...");
}
}
public static void borrar(){
try{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
LinkedList lista = new LinkedList();
System.out.println("Desea borrar un estudiante de la lista? digite si o no. s/n");
String x = buf.readLine();
int y;
if(x.equals("s")){
System.out.println("Digite el numero del estudiante a borrar: 0 al 3");
y = buf.read();
for(int i = 0; i == y; i++ ){
lista.remove(y);
System.out.println(lista);
System.out.println("Estudiante Borrado/a");
}
}
else if (x.equals("n")){
System.out.println("No se borro ningun estudiante.");
}
else{
System.out.println("Letra incorrecta! trate de nuevo..");
}
}
catch(Exception e){
System.out.println("I/O Exception...");
}
}
public static void main(String args[]){
insertar();
borrar();
}
}
River_Plate:
You're creating and populating a LinkedList object in your insertar method. Then in your borrar method you're creating a different LinkedList object and expecting magic to happen, as if it's the same object created and populated earlier.
This and watching you for some time leads me to speak of a remedy:
Do the tutorials. You're getting basically nowhere here.
> This and watching you for some time leads me to speak> of a remedy: > > Do the tutorials. You're getting basically nowhere> here.Agree. I think you've reached the point of diminishing returns. Time to study the basics.
Sorry to bother you all...
Its just that i'm new to all this Data structures with objects.. this is one of a few problems i have to solve for my OOP class.. I've been working with this all weekend..
Java is different from C++ when dealing with structures.. i feel lost cuz here there are no pointers.
Well thanks for the help guys! i've learned a lot.