nextLine() terminates program

Thread Solved

Join Date: Nov 2007
Posts: 14
Reputation: Java-Newbie is an unknown quantity at this point 
Solved Threads: 0
Java-Newbie Java-Newbie is offline Offline
Newbie Poster

nextLine() terminates program

 
0
  #1
Nov 22nd, 2007
Hello and Happy Thanksgiving (to those who celebrate it),

I have a small program that needs to read data off of a text file (name, city and age) and store it in an array. The program asks the user for a city name, and it's supposed to loop through the array and print out the names of people from that city. Now, my problem starts when I ask for the town to look for, since there are two-word towns (e.g. San Juan). I was using input.next() but it was only accepting the first word. I changed it to input.nextLine() and now it skips that part; it just exits the program.

  1.  
  2. import java.io.FileReader;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.FileNotFoundException;
  6. import java.util.Scanner;
  7.  
  8. public class projectCOMP2315
  9. {
  10. public static void main( String [] args )
  11. {
  12. Scanner input = new Scanner (System.in);
  13.  
  14. // Declaracion de variables
  15. String nombreDocTXT = null,
  16. arregloTotalDatos [] = new String [600],
  17. stringPueblo = null,
  18. trimmedElement = null;
  19. int totalDatos = 0,
  20. opcion = 0,
  21. x = 0,
  22. nuevoIndice = 0;
  23. boolean flag = false;
  24.  
  25. try
  26. {
  27. System.out.print( "¿Cual es el nombre del archivo a leer? " );
  28. nombreDocTXT = input.next();
  29.  
  30. FileReader reader = new FileReader( nombreDocTXT );
  31. BufferedReader in = new BufferedReader( reader );
  32.  
  33. String leeUnoAUno = in.readLine();
  34.  
  35. while( leeUnoAUno != null )
  36. {
  37. arregloTotalDatos[ totalDatos ] = leeUnoAUno;
  38. leeUnoAUno = in.readLine();
  39. totalDatos = totalDatos + 1;
  40. }
  41.  
  42. System.out.print(" \n");
  43. for ( x = 1 ; x <= 45 ; x++ )
  44. {
  45. System.out.print( "*" );
  46. }
  47. System.out.print(" M E N U ");
  48. for ( x = 1 ; x <= 45 ; x++ )
  49. {
  50. System.out.print( "*" );
  51. }
  52. System.out.print(" \n1. Print the names of people from a particular town. ");
  53. System.out.print(" \n2. Imprimir el nombre de todas …. ");
  54. System.out.print(" \n3. Imprimir los nombres de todas …. ");
  55. System.out.print(" \n4. Imprimir la lista completa de ….. ");
  56. System.out.print(" \n5. Indicar cuántas personas hay en un …… ");
  57. System.out.print(" \n");
  58. for ( x = 1 ; x <= 101 ; x++ )
  59. {
  60. System.out.print( "*" );
  61. }
  62. System.out.print(" \n9. Salir del programa ");
  63. System.out.print(" \n");
  64. for ( x = 1 ; x <= 101 ; x++ )
  65. {
  66. System.out.print( "*" );
  67. }
  68. System.out.print(" \nSeleccion: ");
  69. opcion = input.nextInt();
  70.  
  71. in.close( );
  72. }
  73.  
  74. catch( FileNotFoundException filenotfoundexxption )
  75. {
  76. System.out.println( nombreDocTXT + " no existe." );
  77. }
  78.  
  79. catch( IOException ioexception )
  80. {
  81. ioexception.printStackTrace( );
  82. }
  83.  
  84. switch (opcion)
  85. {
  86. case 1:
  87. {
  88. System.out.println( "\n¿De que pueblo desea buscar informacion? " );
  89. String pueblo = input.nextLine();
  90.  
  91. stringPueblo = pueblo.trim();
  92.  
  93. for ( x = 0 ; x < arregloTotalDatos.length ; x++ )
  94. {
  95. trimmedElement = arregloTotalDatos[ x ].trim();
  96.  
  97. if ( (stringPueblo) .equals (trimmedElement) )
  98. {
  99. nuevoIndice = x - 1;
  100. System.out.println(arregloTotalDatos[ nuevoIndice ]);
  101. flag = true;
  102. }
  103. }
  104.  
  105. if ( flag == false )
  106. {
  107. System.out.println( pueblo + "\nNo hay un record con ese pueblo." );
  108. }
  109. }
  110. case 9:
  111. {
  112. break;
  113. }
  114. } // termina switch
  115. } // termina main
  116. } // termina projectCOMP2315
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: Java-Newbie is an unknown quantity at this point 
Solved Threads: 0
Java-Newbie Java-Newbie is offline Offline
Newbie Poster

Re: nextLine() terminates program

 
0
  #2
Nov 22nd, 2007
I just read cscgal message about people posting their homework problems in hopes of getting a quick solution. Just wanted you to know that I've tried everything I can think of to get a solution for this, you were my last resort.

I tried using two next() statements but it didn't work for single-word towns since it was expecting the user to write a second word...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: nextLine() terminates program

 
0
  #3
Nov 23rd, 2007
Have you used Scanner.nextLine()??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: Java-Newbie is an unknown quantity at this point 
Solved Threads: 0
Java-Newbie Java-Newbie is offline Offline
Newbie Poster

Re: nextLine() terminates program

 
0
  #4
Nov 23rd, 2007
I tried that and got " non-static method nextLine() cannot be referenced from a static context". Thanks... :-)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: nextLine() terminates program

 
0
  #5
Nov 24th, 2007
if you didn't got the exception here
  1. nombreDocTXT = input.next();
then you can't get it when calling nextLine(); because it's just another method of Scanner class. However, can you provide your updated code that threw exception??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: nextLine() terminates program

 
0
  #6
Nov 24th, 2007
>Now, my problem starts when I ask for the town to look for, since there are two-word towns (e.g. San Juan).

It's pretty simple. Read in the file as lines.

Take that line, assign it to a String then use the appropriate 'string find' methods to get the part you want.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: nextLine() terminates program

 
0
  #7
Nov 24th, 2007
This was interesting. It was really skipping the readLine(). So try this..
  1. System.out.println( "\n¿De que pueblo desea buscar informacion? " );
  2. input = new Scanner(System.in);
  3. String pueblo = input.nextLine();
It will work. I'm still trying to know the reason why it was skipping the instruction.

You will have to use some different logic here
  1. nuevoIndice = x - 1;
  2. System.out.println(arregloTotalDatos[ nuevoIndice ]);
because you are reducing the current index 0 to -1 and trying to print element on -1st index...
[/code]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: Java-Newbie is an unknown quantity at this point 
Solved Threads: 0
Java-Newbie Java-Newbie is offline Offline
Newbie Poster

Re: nextLine() terminates program

 
0
  #8
Nov 26th, 2007
lookof2day, thanks for your input, that did the trick... The reason I have this code here

  1. nuevoIndice = x - 1;
  2. System.out.println(arregloTotalDatos[ nuevoIndice ]);

is because the data in the text file is in the format,
Name
Town
Age
Name
Town
Age
etc....

so I already know that if I find a town with that name (e.g. San Juan), the element above it is a person's name.

Thanks everybody for reading my post and helping out...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC