Problems to validate a number in try-catch block in Java [closed] - java

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.

Related

How to handle Number format Exception? [closed]

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;
}
}

Stopping while loop [closed]

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;

How to Recurse to print [closed]

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
Here is the question:
Write a recursive void method that has one parameter which is an integer and that writes to the screen the number of asterisks “*” given by the argument. The output should be all in one line. Assume that the argument is positive.
It is not a homework or assignment. Just a question in the slides with no answers...
I just don't know how to do it. I am a noob in this
here is my code and what i know
import java.util.Scanner;
public class Ez {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String words[] = str.split("\\s");
int length = words.length;
int clength = str.length();
System.out.println(length);
System.out.println(clength);
}
public static void asterisksCounter (int n) {
}
}
Any big brains that know how to solve this question?
Thank you :)
While this is not a place to just ask a question and get an answer, since you mentioned that you have no idea how recursion works, giving you the code.
static void write(int n){
if(n == 0){
return;
}
System.out.print("*");
write(n-1);
}
General expectation is for the OP to try the problem by themselves, inform the community of the effort they put in and where exactly they are stuck. We would have been happier to help you after seeing some code from your side in the write() method.

Displaying the First 100 Words Of a Program that Contains a File - Java [closed]

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);
}
}

Java Program find string length from user input [closed]

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 9 years ago.
Improve this question
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}
So i want to make a very simple program that takes in a user string and returns the length, but the program does not even run. I am using eclipse. Any tips?
Your program works fine.
Are you forgetting to type something into the console? It might appear to not be running but it actually is.
ALSO: If you are new to java/programming I would suggest using Netbeans over Eclipse. Netbeans offers a bit more support, although it is less flexible (something you shouldn't need to worry about right now).
Make sure that your are providing an input to calculate the length of word.
This is working fine if you providing an input, it is better to change your code as follows:
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your word: \n");
String word1 = sc.next();
System.out.println(word1.length());
sc.close();
}
}

Categories