Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: Data Structure Date: Sun, 09 Mar 2014 23:45:38 -0400 Organization: A noiseless patient Spider Lines: 279 Message-ID: References: <7366a5dd-ee0e-4b07-8664-45e58b399515@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 10 Mar 2014 03:46:22 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="6b9e92482af47766954d85fb77d372f5"; logging-data="11058"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iqZDcjPJkzgacQz8Sx6CZF/ggjVkNwrQ=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 In-Reply-To: <7366a5dd-ee0e-4b07-8664-45e58b399515@googlegroups.com> Cancel-Lock: sha1:iJwubHr98lGqK3N+9w13lLO1TCs= Xref: csiph.com comp.lang.java.programmer:27708 On 03/09/2014 01:13 PM, Michael Pablo wrote: > Hello Community! > > I would like some help with this code: > > The use of this code is merely demonstrative and academic, as everybody can see it is not too organized. > > I hope to get some help with this problem :) > > The problems is with the nextLine(); > and posteriorly with pointers. I would say anteriorly in this case > Are you having problems with PRODUTO proxProd? I've taken the liberty to translate: package scratch; import java.io.IOException; import java.util.Random; import java.util.Scanner; public class LS_Nao_Ordenada { // Defining class to represent each element of the list private static class PRODUTO { public int numProd; public String nomeProd; public char tipoDeProduto; public float percentuaisImposto; public float precoProd; PRODUTO proxProd; }; public static void main(String[] args) throws IOException { Scanner entrada = new Scanner(System.in); /* * the list is empty , then the object start have the value null the object * will contain the start address of the first element of the list */ PRODUTO inicioPROD = null; /* * close the object will contain the address of the last elemnteo the list */ PRODUTO fimPROD = null; // AuxPROD the object is a helper object PRODUTO auxPROD; // AnteriorPROD the object is a helper object PRODUTO anteriorPROD; // Variable to total product float totalPRODIMP = 0.0f; // Query product String consultaPROD = " "; // Showing the options menu int op, numero, found; do { System.out.println(" \nmenu options \n"); System.out .println(" 1 - Register the product at the beginning of the queue "); System.out.println(" 2 - Register product at the end of the queue "); System.out.println(" 3 - Consult the price of a product "); System.out.println(" 4 - Delete product"); System.out.println(" 5 - Empty list "); System.out.println(" 6 - Exit "); System.out.println(" Enter your choice : "); op = entrada.nextInt(); if (op < 1 || op > 6) { System.out.println(" Invalid option ! "); } if (op == 1) { PRODUTO novoPROD = new PRODUTO(); Random generatorPRO = new Random(); novoPROD.numProd = generatorPRO.nextInt(); /* * System.out.println ( " Enter the Product name : " ) ; * Entrada.nextLine = (); */ System.out .println(" Enter the type of product to be inserted at the beginning of the queue: "); novoPROD.tipoDeProduto = (char) System.in.read(); System.out.println(" Enter the price of the product to be inserted : "); novoPROD.precoProd = entrada.nextFloat(); System.out.println(" Enter the tax item inserted : "); novoPROD.percentuaisImposto = entrada.nextFloat(); if (null == inicioPROD) { // List was empty and the inserted element will be the first // and last inicioPROD = novoPROD; fimPROD = novoPROD; novoPROD.proxProd = null; } else { /* * the list already contains elements and the new element is inserted * at the beginning of the list */ // points to start the next list as eg element 2 prox <--- 1 // element . novoPROD.proxProd = inicioPROD; // " Pointer " to new element at the beginning of the list inicioPROD = novoPROD; } System.out .println(" Type of product and inserted at the beginning of the tax list ! "); } if (op == 2) { PRODUTO novoPROD = new PRODUTO(); Random generatorPRO = new Random(); novoPROD.numProd = generatorPRO.nextInt(); System.out.println(" Enter the Product name : "); novoPROD.nomeProd = entrada.nextLine(); System.out .println(" Enter the type of product to be inserted at the beginning of the queue: "); novoPROD.tipoDeProduto = (char) System.in.read(); System.out.println(" Enter the price of the product to be inserted : "); novoPROD.precoProd = entrada.nextFloat(); System.out.println(" Enter the tax item inserted : "); novoPROD.percentuaisImposto = entrada.nextFloat(); if (null == inicioPROD) { /* * List was empty and the inserted element will be the first and the * last */ inicioPROD = novoPROD; fimPROD = novoPROD; novoPROD.proxProd = null; } else { /* * the list already contains elements and the new element will be * inserted end of the list */ // Holds the record for the new list ( next) fimPROD.proxProd = novoPROD; // " Pointer " to last element. fimPROD = novoPROD; fimPROD.proxProd = null; } System.out.println(" Sign inserted at the end of the list ! "); } if (op == 3) { System.out.println(" Enter the item to be queried : "); consultaPROD = entrada.nextLine(); if (null == inicioPROD) { System.out.println(" List of empty registry ! "); } else { /* * The list contains elements and these will be shown from start the * end */ auxPROD = inicioPROD; while (auxPROD != null) { if (auxPROD.nomeProd.equals(consultaPROD)) { totalPRODIMP = (auxPROD.percentuaisImposto * auxPROD.precoProd) + auxPROD.precoProd; auxPROD.proxProd = null; auxPROD = auxPROD.proxProd; } else { auxPROD = auxPROD.proxProd; } } System.out.println(" Product Name : " + auxPROD.nomeProd); System.out.println(" The value of the product was R $ " + totalPRODIMP); } } if (op == 4) { if (null == inicioPROD) { /* List is empty */ System.out.println(" empty list ! "); } else { /* * The list contains elements and the element to be removed must be * entered */ System.out.println(" \nEnter the product to be removed : "); consultaPROD = entrada.nextLine(); // All occurrences of the list , equal to the number entered // , // will be removed auxPROD = inicioPROD; anteriorPROD = null; found = 0; while (auxPROD != null) { if (auxPROD.nomeProd.equals(consultaPROD)) { /* * The number entered was found in the list and will removed */ found = found + 1; if (auxPROD == inicioPROD) { /* the number to be removed is first in list */ inicioPROD = auxPROD.proxProd; auxPROD = inicioPROD; } else if (auxPROD == fimPROD) { /* * the number to be removed is the last of the list */ anteriorPROD.proxProd = null; fimPROD = anteriorPROD; auxPROD = null; } else { /* * The number to be removed is in the middle of the list */ anteriorPROD.proxProd = auxPROD.proxProd; auxPROD = auxPROD.proxProd; } } else { anteriorPROD = auxPROD; auxPROD = auxPROD.proxProd; } } if (found == 0) { System.out.println(" Product not found "); } else if (found == 1) { System.out.println(" Product 1 time removed "); } else { System.out.println(" Material removed " + found + " times "); } } } if (op == 5) { if (null == inicioPROD) { /* list is empty */ System.out.println(" Empty list "); } else { /* List is empty */ inicioPROD = null; System.out.println(" empty list ! "); } } } while (op != 6); } }