-1

Hello first post here hopefully I'm doing it right. I am currently stuck in a problem figuring out if everything in the string are integers or not. I managed to get the yes for each character of the string checked but each time it loops through each character in string i get a new "yes" and need just a yes if EVERY character in string is an integer.

import java.util.Scanner; 

public class LabProgram {
public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  String userString;
  int i;

  userString = scnr.next();

  for (i = 0; i < userString.length(); ++i) {
     if (Character.isDigit(i) == true) {

     }
     System.out.print("yes");
  }      
}
}

If I input "1995" my output is "yesyesyesyes", while I need an output of just "yes".

Yayo
  • 15
  • 1
  • 7
  • "hopefully im doing it right" => Your "question" is hard to read with nearly no punctuation and no uppercase letters and no paragraphs. Additionally, please explain detailed where your program is failing. – Seelenvirtuose Apr 27 '20 at 06:40
  • It might be easier to check the opposite and then break the loop. And you should present the result after the loop, not inside it. – Joakim Danielson Apr 27 '20 at 06:43
  • @Seelenvirtuose there fixed it, first post, didn't think grammar would affect what I was asking too much since only one short paragraph. At the bottom I put where I'm failing and what I need. – Yayo Apr 27 '20 at 06:46
  • @JoakimDanielson Check for the opposite as in checking if its false instead? – Yayo Apr 27 '20 at 06:50
  • Does this answer your question? [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – T.N. Apr 27 '20 at 06:54
  • Yes that is the opposite of true :). The point is that you can break the loop as soon as you get the first false since then the whole string is not valid. – Joakim Danielson Apr 27 '20 at 07:02

2 Answers2

1

The problem in your code is that the System.out.println is inside the for loop, that's why the output is "yesyesyesyes" for a four length string.

In this case you can suppose that every character in the string is a digit and check every one until you find one that aren't a digit, let's use a while:

    String word = "1992";
    boolean isDigit = true;
    int i = 0;

    while (i < word.length() && isDigit) {
        isDigit = Character.isDigit(word.charAt(i));
        i++;
    }

    if(isDigit) {
        System.out.println("yes");
    }

I suppose that your code is for class so the above example should be enough, but you also could use a more functional approach like:

    if(word.chars().allMatch(Character::isDigit)) {
        System.out.println("yes");
    }
  • 1
    Thanks! That ran perfectly. Was debating of switching over to while loop and get it to run each character of the string till like you said, one popped out that wasn't a digit but still believed for loop was the way to go. I understood most of your code and will go over and test myself to redo on my own making sure I understood every step. – Yayo Apr 27 '20 at 07:09
  • That will be error if word = '1992w'. when a char is not digit, you should break the loop. – meng Apr 27 '20 at 08:07
0

Using Apache Comms lang, you can use StringUtils.isNumeric(yourString) This will return a boolean value.

OR

if you want to do it your way, try this

  userString = scnr.next();
  boolean isStringNumeric=true;

  for (i = 0; i < userString.length(); ++i) {
     isStringNumeric = Character.isDigit(userString[i]);
     if (!isStringNumeric) {
         break;
     }
  } 
 if(isStringNumeric){
    System.out.println("Yes");
} else{
     System.out.println("No");   
}
T.N.
  • 87
  • 10