-1

I want to create a class that simulates registers of a market and the project requires that after the user gives us how many registers he wants , he must give a string in the form below:

for exaple : 0 0 0 1 1 1 2 2 2 3 3 5 5 6 6

which means that 3 customers entered the store at monent 0 , 3 customers entered the store at moment 1 etc.

Here is my code:

import java.util.Scanner;
import java.io.*;


public class QueueSimulation
{
public static void main(String[] args)
{
int a;
int i = 0;

String input;

Scanner s = new Scanner(System.in);

int A[] = new int[10000];

System.out.println("This programs simulates a queue of customers at registers.");


System.out.println("Enter the number of registers you want to simulate:");

a = s.nextInt();
while(a==0 || a <0){
System.out.println("0 registers or no registers is invalid. Enter again: ");

a = s.nextInt();

}
int fifo[] = new int[a];

System.out.println("Enter how many customers enter per second.For example: 0 0 1 1 2 2 2 3 3.       
Enter : ");
  input = s.nextLine();


  while(s.hasNext()){
 while(s.hasNextInt()){
 A[i] = s.nextInt();
 i++;

 }
 s.close(); 
 }


s.close();  
}

}

The code compiles fine , but when i run it, something goes very wrong with the 2 while loops and the programs nevers end or stop. Complile if you have the time and you will understand. I dont know maybe i placed the close() method somewhere wrong? Please help.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • "something goes very wrong " this is what your debugger is for. You step through the code looking at what each line is doing and when this happens you can see what has gone wrong and work out why. – Peter Lawrey Nov 16 '14 at 11:02
  • the problem is a runtime one and its not even a problem that can been seen directly. Thats why i am asking for help cauze i am not very familiar with editing strings methods.. – Aggelos Sfakianos Nov 16 '14 at 11:22
  • 2
    It can be seen, but understanding how to use a debugger and what it is telling you takes practice. The sooner you start the better because it is something you will be using for years to come. – Peter Lawrey Nov 16 '14 at 11:24
  • The other thing you need to do is to learn to indent your code properly. A lot of people will refuse to read code that is not is properly formatted. And you will get a LOT of negative feedback if your code is code reviewed. – Stephen C Nov 16 '14 at 11:48

1 Answers1

0

You are missing a s.next(); in the given code.

while(s.hasNext()){
while(s.hasNextInt()){
A[i] = s.nextInt();
i++;
}
// here s.next() is missing...
s.close(); 
}

Please add the s.next(); statement before the end of second-while loop of your program for proper iteration of your program...

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • It did not work... Once i enter the second input, the programs does nothing and i just can enter numbers as many times as possible. It only stops by throwing a runtime exception if i enter anything else but integers... – Aggelos Sfakianos Nov 16 '14 at 11:09
  • Check this to know why it is so---http://stackoverflow.com/questions/19101776/scanner-hasnext-results-in-an-infinite-loop – Am_I_Helpful Nov 16 '14 at 11:42
  • You need to manually do Ctrl+Z and then Enter key on Windows machine OR Ctrl+D on Unix type machine. The answer is this only,you need to manually end the input by applying `EOF character`. Hence,my answer stands correct! – Am_I_Helpful Nov 16 '14 at 11:56
  • And what if i run programs through cmd? What then? – Aggelos Sfakianos Nov 16 '14 at 12:00
  • Then also the terminal will be terminal. Hence,your EOF characters will be same---`Ctrl+Z with Enter`. Happy coding with this program... – Am_I_Helpful Nov 16 '14 at 13:22