How do I get while input (except when the user inputs 0 that terminates the program) in Java?java.util.NoSuchElementException - Scanner reading user inputHow slow are Java exceptions?How to get an enum value from a string value in Java?The case against checked exceptionsHow to get the current date/time in JavaHow can I get the user input in Java?Google Gson - deserialize list<class> object? (generic type)How to wait for user input when using JTextFieldsboolean value will not WHILE loop java programmingCode after while loop not accepting input [Java]Java ArrayList to store user input and terminate when -999 is entered
My Sixteen Friendly Students
Do oversize pulley wheels increase derailleur capacity?
Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?
Can you turn a recording upside-down?
Light Switch Neutrals: Bundle all together?
Names of the Six Tastes
Crime rates in a post-scarcity economy
How can it be that ssh somename works, while nslookup somename does not?
Why doesn't Dany protect her dragons better?
Where do 5 or more U.S. counties meet in a single point?
Magical Modulo Squares
What computer port is this?
How to start your Starctaft II games vs AI immediatly?
Why doesn't increasing the temperature of something like wood or paper set them on fire?
Why did Ham the Chimp push levers?
As a small race with a heavy weapon, does enlage remove the disadvantage?
What happens when the drag force exceeds the weight of an object falling into earth?
How can I test a shell script in a "safe environment" to avoid harm to my computer?
Creating Stored Procedure in local db that references tables in linked server
Employee is self-centered and affects the team negatively
My perfect evil overlord plan... or is it?
History: Per Leviticus 19:27 would the apostles have had corner locks ala Hassidim today?
Why does this pattern in powers happen?
How do I minimise waste on a flight?
How do I get while input (except when the user inputs 0 that terminates the program) in Java?
java.util.NoSuchElementException - Scanner reading user inputHow slow are Java exceptions?How to get an enum value from a string value in Java?The case against checked exceptionsHow to get the current date/time in JavaHow can I get the user input in Java?Google Gson - deserialize list<class> object? (generic type)How to wait for user input when using JTextFieldsboolean value will not WHILE loop java programmingCode after while loop not accepting input [Java]Java ArrayList to store user input and terminate when -999 is entered
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm new to Java. I want to get loop input except when the user inputs 0 that terminates the program in Java. I know how to implement this in C++ (as shown below), but the Java code I wrote doesn't work.
C++:
while (cin >> n, n)
GraphAdjList G;
CreateAdjListGraph(G, n);
n = 0;
Java:
Scanner sc = new Scanner(System. in );
n = sc.nextInt();
while (n != 0)
Graph G = new Graph();
G.CreateAdjListGraph(n);
//G.print();
n = sc.nextInt();
This is what I want. The program terminates only when the user inputs 0.
2
qRj dIm
aTy oFu
4
qRj aTy
qRj oFu
oFu cLq
aTy qUr
0
java
|
show 2 more comments
I'm new to Java. I want to get loop input except when the user inputs 0 that terminates the program in Java. I know how to implement this in C++ (as shown below), but the Java code I wrote doesn't work.
C++:
while (cin >> n, n)
GraphAdjList G;
CreateAdjListGraph(G, n);
n = 0;
Java:
Scanner sc = new Scanner(System. in );
n = sc.nextInt();
while (n != 0)
Graph G = new Graph();
G.CreateAdjListGraph(n);
//G.print();
n = sc.nextInt();
This is what I want. The program terminates only when the user inputs 0.
2
qRj dIm
aTy oFu
4
qRj aTy
qRj oFu
oFu cLq
aTy qUr
0
java
1
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
1
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17
|
show 2 more comments
I'm new to Java. I want to get loop input except when the user inputs 0 that terminates the program in Java. I know how to implement this in C++ (as shown below), but the Java code I wrote doesn't work.
C++:
while (cin >> n, n)
GraphAdjList G;
CreateAdjListGraph(G, n);
n = 0;
Java:
Scanner sc = new Scanner(System. in );
n = sc.nextInt();
while (n != 0)
Graph G = new Graph();
G.CreateAdjListGraph(n);
//G.print();
n = sc.nextInt();
This is what I want. The program terminates only when the user inputs 0.
2
qRj dIm
aTy oFu
4
qRj aTy
qRj oFu
oFu cLq
aTy qUr
0
java
I'm new to Java. I want to get loop input except when the user inputs 0 that terminates the program in Java. I know how to implement this in C++ (as shown below), but the Java code I wrote doesn't work.
C++:
while (cin >> n, n)
GraphAdjList G;
CreateAdjListGraph(G, n);
n = 0;
Java:
Scanner sc = new Scanner(System. in );
n = sc.nextInt();
while (n != 0)
Graph G = new Graph();
G.CreateAdjListGraph(n);
//G.print();
n = sc.nextInt();
This is what I want. The program terminates only when the user inputs 0.
2
qRj dIm
aTy oFu
4
qRj aTy
qRj oFu
oFu cLq
aTy qUr
0
java
java
edited Mar 23 at 8:00
karel
2,36392832
2,36392832
asked Mar 23 at 7:47
superman22superman22
81
81
1
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
1
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17
|
show 2 more comments
1
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
1
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17
1
1
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
1
1
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17
|
show 2 more comments
2 Answers
2
active
oldest
votes
nextInt()
won't work for your case, If your input contains a non-integer word it throws InputMismatchException
. You better use nextLine()
and try to convert each word in to int
with Integer.parseInt
.
For example:
int n = -1;
Scanner sc = new Scanner(System.in);
String line;
while (n != 0)
line = sc.nextLine();
String[] splits = line.split(" ");
System.out.println(Arrays.toString(splits));
for (String split : splits)
try
n = Integer.parseInt(split);
if (n == 0)
break;
//Graph G = new Graph();
//G.CreateAdjListGraph(n);
catch (NumberFormatException e)
// handling
This should work even if you give all inputs in the same line.
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
add a comment |
You should not use scan.nextInt()
because in your sample program run you took some non-inetger values
as input, so, in that case you code will fail.
Use scan.nextLine()
this will take argument as String
and not as Integer
. Now you can change compare the results for your While loop
.
Working Code:
import java.util.Scanner;
public class stackScanner
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// as you are now taking "String" from user so you have to compare it with "0" not 0
while(!(input.equals("0"))) // while input is not 0
// your code here
input = scan.nextLine();
Note: I am using String.equals()
and not ==
operator, reasons:
- As
String.equals()
always returnboolean value
it doesn't through any exception. - We can use
==
operators for reference comparison (address comparison) andStinrg.equals()
method for content comparison. In simple words,==
checks if both objects point to the same memory location whereasString.equals()
evaluates to the comparison of values in the objects.
For more on String.equals()
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311729%2fhow-do-i-get-while-input-except-when-the-user-inputs-0-that-terminates-the-prog%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
nextInt()
won't work for your case, If your input contains a non-integer word it throws InputMismatchException
. You better use nextLine()
and try to convert each word in to int
with Integer.parseInt
.
For example:
int n = -1;
Scanner sc = new Scanner(System.in);
String line;
while (n != 0)
line = sc.nextLine();
String[] splits = line.split(" ");
System.out.println(Arrays.toString(splits));
for (String split : splits)
try
n = Integer.parseInt(split);
if (n == 0)
break;
//Graph G = new Graph();
//G.CreateAdjListGraph(n);
catch (NumberFormatException e)
// handling
This should work even if you give all inputs in the same line.
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
add a comment |
nextInt()
won't work for your case, If your input contains a non-integer word it throws InputMismatchException
. You better use nextLine()
and try to convert each word in to int
with Integer.parseInt
.
For example:
int n = -1;
Scanner sc = new Scanner(System.in);
String line;
while (n != 0)
line = sc.nextLine();
String[] splits = line.split(" ");
System.out.println(Arrays.toString(splits));
for (String split : splits)
try
n = Integer.parseInt(split);
if (n == 0)
break;
//Graph G = new Graph();
//G.CreateAdjListGraph(n);
catch (NumberFormatException e)
// handling
This should work even if you give all inputs in the same line.
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
add a comment |
nextInt()
won't work for your case, If your input contains a non-integer word it throws InputMismatchException
. You better use nextLine()
and try to convert each word in to int
with Integer.parseInt
.
For example:
int n = -1;
Scanner sc = new Scanner(System.in);
String line;
while (n != 0)
line = sc.nextLine();
String[] splits = line.split(" ");
System.out.println(Arrays.toString(splits));
for (String split : splits)
try
n = Integer.parseInt(split);
if (n == 0)
break;
//Graph G = new Graph();
//G.CreateAdjListGraph(n);
catch (NumberFormatException e)
// handling
This should work even if you give all inputs in the same line.
nextInt()
won't work for your case, If your input contains a non-integer word it throws InputMismatchException
. You better use nextLine()
and try to convert each word in to int
with Integer.parseInt
.
For example:
int n = -1;
Scanner sc = new Scanner(System.in);
String line;
while (n != 0)
line = sc.nextLine();
String[] splits = line.split(" ");
System.out.println(Arrays.toString(splits));
for (String split : splits)
try
n = Integer.parseInt(split);
if (n == 0)
break;
//Graph G = new Graph();
//G.CreateAdjListGraph(n);
catch (NumberFormatException e)
// handling
This should work even if you give all inputs in the same line.
edited Mar 23 at 8:12
answered Mar 23 at 7:56
HariUserXHariUserX
1,1231515
1,1231515
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
add a comment |
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
thank you! But it didn't work either. it throws java.util.NoSuchElementException I think it's because it the while loop I used println and the Scanner receive the newline rather than waiting for my input
– superman22
Mar 23 at 8:05
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Added an example. Check now.
– HariUserX
Mar 23 at 8:06
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
Thank you!I found My Bug, it's because i used another Scanner in a method and turn it down. Now I know that you only need to close scanner once.
– superman22
Mar 25 at 6:55
add a comment |
You should not use scan.nextInt()
because in your sample program run you took some non-inetger values
as input, so, in that case you code will fail.
Use scan.nextLine()
this will take argument as String
and not as Integer
. Now you can change compare the results for your While loop
.
Working Code:
import java.util.Scanner;
public class stackScanner
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// as you are now taking "String" from user so you have to compare it with "0" not 0
while(!(input.equals("0"))) // while input is not 0
// your code here
input = scan.nextLine();
Note: I am using String.equals()
and not ==
operator, reasons:
- As
String.equals()
always returnboolean value
it doesn't through any exception. - We can use
==
operators for reference comparison (address comparison) andStinrg.equals()
method for content comparison. In simple words,==
checks if both objects point to the same memory location whereasString.equals()
evaluates to the comparison of values in the objects.
For more on String.equals()
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
|
show 1 more comment
You should not use scan.nextInt()
because in your sample program run you took some non-inetger values
as input, so, in that case you code will fail.
Use scan.nextLine()
this will take argument as String
and not as Integer
. Now you can change compare the results for your While loop
.
Working Code:
import java.util.Scanner;
public class stackScanner
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// as you are now taking "String" from user so you have to compare it with "0" not 0
while(!(input.equals("0"))) // while input is not 0
// your code here
input = scan.nextLine();
Note: I am using String.equals()
and not ==
operator, reasons:
- As
String.equals()
always returnboolean value
it doesn't through any exception. - We can use
==
operators for reference comparison (address comparison) andStinrg.equals()
method for content comparison. In simple words,==
checks if both objects point to the same memory location whereasString.equals()
evaluates to the comparison of values in the objects.
For more on String.equals()
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
|
show 1 more comment
You should not use scan.nextInt()
because in your sample program run you took some non-inetger values
as input, so, in that case you code will fail.
Use scan.nextLine()
this will take argument as String
and not as Integer
. Now you can change compare the results for your While loop
.
Working Code:
import java.util.Scanner;
public class stackScanner
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// as you are now taking "String" from user so you have to compare it with "0" not 0
while(!(input.equals("0"))) // while input is not 0
// your code here
input = scan.nextLine();
Note: I am using String.equals()
and not ==
operator, reasons:
- As
String.equals()
always returnboolean value
it doesn't through any exception. - We can use
==
operators for reference comparison (address comparison) andStinrg.equals()
method for content comparison. In simple words,==
checks if both objects point to the same memory location whereasString.equals()
evaluates to the comparison of values in the objects.
For more on String.equals()
You should not use scan.nextInt()
because in your sample program run you took some non-inetger values
as input, so, in that case you code will fail.
Use scan.nextLine()
this will take argument as String
and not as Integer
. Now you can change compare the results for your While loop
.
Working Code:
import java.util.Scanner;
public class stackScanner
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// as you are now taking "String" from user so you have to compare it with "0" not 0
while(!(input.equals("0"))) // while input is not 0
// your code here
input = scan.nextLine();
Note: I am using String.equals()
and not ==
operator, reasons:
- As
String.equals()
always returnboolean value
it doesn't through any exception. - We can use
==
operators for reference comparison (address comparison) andStinrg.equals()
method for content comparison. In simple words,==
checks if both objects point to the same memory location whereasString.equals()
evaluates to the comparison of values in the objects.
For more on String.equals()
edited Mar 23 at 8:26
answered Mar 23 at 8:20
Zain ArshadZain Arshad
7871315
7871315
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
|
show 1 more comment
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
Thank you! But i am so sorry that I didn't write clearly in my question. when I want user to type a int, there is only this int on the currently line. It's more like this 3n A Bn C Dn C Bn 0n and the program terminates
– superman22
Mar 23 at 8:26
where n means the following is on a newline
– superman22
Mar 23 at 8:27
where n means the following is on a newline
– superman22
Mar 23 at 8:27
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
I don't understand actually what you want to do with this code? You want to take input as long as user inputs "0", then your while-loop should terminate. My code is exactly doing this. However, a little more information is required so that I can solve your problem.
– Zain Arshad
Mar 23 at 8:30
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
This a Graph problem. the n indicate this graph has n edges, in CreateAdjListGraph() method, the user input n sets of edges like"A B"n "A C",than I put this edges in my class and create a digraph. I use another Scanner in this method and closed it when I'm done. In this method I deleted all print statement but the problem remains
– superman22
Mar 23 at 8:36
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
It's an Online Judge title. It wants this way to check more samples during one inputs. I thought this is an popular way when coding in OJ
– superman22
Mar 23 at 8:39
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311729%2fhow-do-i-get-while-input-except-when-the-user-inputs-0-that-terminates-the-prog%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Define "didn't work". What are yo doing precisely, what do you expect to happen, and what happens instead?
– JB Nizet
Mar 23 at 7:53
it throws Exception in thread "main" java.util.NoSuchElementException
– superman22
Mar 23 at 7:55
I cannot continue to input more "n" in this program
– superman22
Mar 23 at 7:56
1
You're probably closing System.in somewhere inside CreateAdjListGraph. What are you typing? What does this method do?
– JB Nizet
Mar 23 at 8:00
The Problem seems to be somewhere inside CreateAdjListGraph(n);
– Adnan Ahmad Khan
Mar 23 at 8:17