Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
can anybody help me to stopp this infity loop?
This loop shoud stopp if an input is a String.
All digits should be added to a list.
Thank you!
public static void main(String[] args) {
addDigitsToList();
}
public static void addDigitsToList() {
ArrayList<Integer> list = new ArrayList<Integer>();
int input = 0;
while (true) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
input = Integer.parseInt(reader.readLine());
list.add(input);
} catch (NumberFormatException | IOException e) {
for (Integer integer : list) {
System.out.println(integer);
}
}
}
}
If you want to stop the infinity loop than use the keyword
break;
on which condition you want to stop it. You told us to stop when the input is a string than you should update your code-
Right after the for loop add this :
break;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My requirement is to check divisibility of the input by 7, for the various test cases and I have written this code but it is throwing me NumberFormat Exception
class Solution{
int isdivisible7(String num){
// code her
long i= Long.parseLong(num);
// to convert string into long
if(i%7==0)
return 1;
else
return 0;
}
}
How can I handle the exception and return the result for any (both valid and invalid) input ?
If the input num is not number, then it will throw NumberFormatException, so you just have to catch it. Also, function names should be in camel case. And finally, it's better to make the function return boolean rather then int of values 0 and 1.
boolean isDivisibleBy7(String num){
try {
long i = Long.parseLong(num);
return i % 7 == 0;
} catch (NumberFormatException e) {
// print some error message if you want
System.out.println("You haven't passed number");
return false;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
public class ReverseString {
public static void main(String[] args) {
String reverse = "";
String obj = new String("BOOKS");
for ( int i = obj.length() - 1 ; i >= 0 ; i-- )
reverse = reverse + obj.charAt(i);
System.err.println("Orignal string is: "+obj);
System.out.println("Reverse string is: "+reverse);
}
}
Import scanner, then create its object and use it to take user input.
import java.util.Scanner;
//Statements of your code
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String obj = sc.nextLine();
//Statements of your code
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Java Hashmap -- Is it possible to read key value pairs from standard input directly into a hash map structure?
Say user types in
4 3
1 1
3 2
2 2
4 3
My idea is to do some sort of loop with repeated put.
Not really sure how you want to add them, as int's or Strings, but remember they have to be Objects.
class Inp {
public void scan() {
Scanner sc = new Scanner(System.in);
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
while (true) {
String sr = sc.nextLine();
String[] tk = sr.split(" ");
Integer key = 0, value = 0;
try {
key = Integer.parseInt(tk[0]);
value = Integer.parseInt(tk[1]);
}
catch (NumberFormatException e) {
break;
}
map.put(key,value);
}
}
public static void main(String[] args) {
Inp inp = new Inp();
inp.scan();
}
}
VERRRRYYYY long winded but you get the idea. Put in anything besides an integer to break out of the loop. Didn't know what you wanted.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to create a method to introduce an int with NetBeans, but I have a problem when I run the method, the order of console messages is not correct, someone knows what the problem is:
public static void main(String[] args)
{
Scanner teclado = new Scanner(System. in );
int num;
boolean error;
public int introducirDatos()
{
do
{
error = false;
try
{
System.out.println("Introduzca un número entero: ");
num = Integer.valueOf(teclado.nextLine());
}
catch (NumberFormatException e)
{
System.err.println("Debe introducir un número y sin decimales, vuelve a intentarlo.\n");
error = true;
}
} while (error == true);
return num;
}
}
Thanks.
I believe you are running the code in an IDE - IntelliJ IDEA or Eclipse. The line System.out.println("Introduzca un número entero: "); prints to standard OUT, but System.err.println("Debe introdu... prints to standard ERROR. That's why the output gets messed up. Replace System.err with System.out for the messages being printed nicely one after another.
BTW you haven't specified which language you are using. Is it Java? You might want to update the question tags.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program that reads a file in order to sort the file alphabetically. So the output of the program is displayed in an ascending order (From A-Z if applies). However, I want my program to just output the first 100 words by ignoring the rest of it. Is there a Unix command that allows me to carry out this function? or do I have to implement a code/algorithm within my program in order to accomplish it?
It should be something like:
Scanner sc2 = null;
try {
sc2 = new Scanner(new File("file.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int numberword=100;
int count=0;
while (sc2.hasNextLine() && count<numberword) {
Scanner s2 = new Scanner(sc2.nextLine());
boolean b;
while (b = s2.hasNext() && count<numberword) {
String s = s2.next();
count++;
System.out.println(s);
}
}