Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Scanner Delimiter without \n
    primarykey
    data
    text
    <p>I have a problem that's breaking my mind. I have a .txt file that looks like</p> <pre><code>fiat,regata,15*renault,seiscientos,25* </code></pre> <p>In my code I have this </p> <pre><code> Scanner sc=new Scanner(new File("coches.txt"); sc.useDelimiter("[,*]"); while(sc.hasNext()){ marca=new StringBuffer(sc.next()); modelo=new StringBuffer(sc.next()); marca.setLength(10); modelo.setLength(10); edad=sc.nextInt(); coche=new Coche(marca.toString(),modelo.toString(),edad); coches.add(coche); } </code></pre> <p>The problem here is that the While loop is working three times, so the third time marca=\n and It stops with a <code>java.util.NoSuchElementException</code>. So, How can I use my delimiter to stop de loop in the last * and avoid It to enter in that extra/problematic time?. </p> <p>I already tried things like </p> <pre><code>while(sc.next!="\n") </code></pre> <p>I also triyed this and doesn't work</p> <p>sc.useDelimiter("[,\*\n]");</p> <p>SOLVED!!!</p> <p>I finally found the solution, in part thanks to the advice of user1542723. The solution<br> is: </p> <pre><code>String linea; String [] registros,campos; File f=new File("coches.txt"); FileReader fr=new FileReader(f); BufferedReader br=new BufferedReader(fr);//ALL this need Try Catch that I'm not posting while((linea=br.readLine())!=null){ registros=linea.split("\\*"); } for (int i = 0; i &lt; registros.length; i++) { campos=registros[i].split(","); marca=campos[0]; modelo=campos[1]; edad=Integer.parseInt(campos[2]);//that's an Int, edad means Age coche=new Coche(marca.toString(),modelo.toString(),edad); coches.add(coche); } } </code></pre> <p>Thank you everybody who helped me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload