Had to take down due to privacy concerns
-
I don't see the declaration of the variable `input`. What is it? Should it be the Scanner variable? – Riccardo Chimisso May 15 '20 at 15:38
-
@RiccardoChimisso yes that's right – aqnxqyfunialgrflfe May 15 '20 at 15:42
-
Do you have access to the `Process` class? Seeing the code in that class could be helpful. – Evan Bailey May 15 '20 at 15:45
-
@EvanBailey it just has variables inside, no methods – aqnxqyfunialgrflfe May 15 '20 at 16:43
-
@aqnxqyfunialgrflfe - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash Jun 07 '20 at 15:27
2 Answers
scan.next() function gets the next input string. That is why you get an empty line for process name because the name is already taken by the next function in the while condition. Either use hasNext() to check if there is a next line or get and put input to a string variable and compare it with the word 'finish'.
You can see the explanation in the documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
Quoting from the documentation: "next : Finds and returns the next complete token from this scanner"
![](../../users/profiles/9208140.webp)
- 171
- 7
The problem is because of using next()
instead of nextLine()
. Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn more about it.
Replace
while (!scan.next().equalsIgnoreCase("finish"))
with
while (!scan.nextLine().equalsIgnoreCase("finish"))
Also, it's better to use do...while
which guarantees to execute its body at least once i.e.
do {
Process p = new Process();
String pn = "";
String bt = "";
String at = "";
pn = input.nextLine();
bt = input.nextLine();
at = input.nextLine();
System.out.println("Process name, CPU Burst Time, Arrival time\n ");
p.process_name = pn;
p.burstTime = Float.parseFloat(bt);
p.arrivalTime = Float.parseFloat(at);
fcfs.add(p);
} while (!scan.nextLine().equalsIgnoreCase("finish"));
![](../../users/profiles/10819573.webp)
- 50,121
- 5
- 26
- 72