0

Hello everyone~ I'm a little new in this theme of programming. I'm here to expose a particular case, of my college project, hoping that can find a way to validate the java code.

Basically, I'm designing a system to sum the daily sales of a company and save them into a matrix. The code below corresponds to the first day only.

package fsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class class_Sist {

public class_Sist() {
}   
    ArrayList <Integer> MondayPrices = new ArrayList();
    int Week[][]= new int [2][7];

public void addPricesDay1(){        
    Scanner scn = new Scanner(System.in);
    String answer;
    Integer auxAddition=0;
    boolean flagNext= false, flagAgain= false;
    System.out.println("Next it is come to apply the amounts of sales made on the day "+Week[0][0]+". Please "
            + "indicate the price of each of the items that were sold.");

    do {
        System.out.println("Enter the price of the corresponding article.");
        MondayPrices.add(scn.nextInt());

        do {
            System.out.println("It requires enter the price of another article?");
            answer= scn.next();

            if (("Yes".equals(answer))||("yes".equals(answer))) {
                flagNext=false;
                flagAgain=true;
            }

            if (("No".equals(answer))||("no".equals(answer))){
                flagNext=false;
                flagAgain=false;
                System.out.println("Introduced sales prices have been stored successfully.");
            }
            if ((!"Yes".equals(answer))&&(!"yes".equals(answer))&&(!"No".equals(answer))&&(!"no".equals(answer))) {
                System.out.println("Error. Please respond using by answer only yes or no.");
                flagNext=true;
            }          

        } while (flagNext==true);           

    } while (flagAgain==true);

    for (int i=0; i<MondayPrices.size(); i++) {
        auxAddition= auxAddition+MondayPrices.get(i);
    }

    System.out.println("The total amount in sales for monday is "+auxAddition);

    Week[1][0]=auxAddition;
   }
}

So, what I need is to validate that the data inputted by the user be only numeric, and never otherwise, but I don't know completely how ArrayList works, therefore, I would greatly appreciate if someone could explain me how I can do that.

Suhaib Janjua
  • 3,273
  • 13
  • 52
  • 68
  • You could try something like this System.out.println("Enter the price of the corresponding article."); tmp = scn.nextLine(); try{ price = Integer.parseInt(tmp); MondayPrices.add(price); } catch (Exception e){ System.out.println("must be an integer"); } – tim Apr 13 '15 at 04:02
  • @tim thanks for your comment, I tried it and worked. Actually it's a function I never tried, and I don't know how to adapt it into a cycle to ask again the price if the user inputs a wrong character, and also without saving it in the Arraylist – Charjake08 Apr 13 '15 at 04:36

2 Answers2

0

You can validate the input before adding the integer in the ArrayList.

I hope this will help - How to use Scanner to accept only valid int as input

Community
  • 1
  • 1
user1879835
  • 71
  • 4
  • 7
0

Use Scanner hasNextInt() function. Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the nextInt() method.The scanner does not advance past any input.

Use it with while to check whether the input is integer.

Scanner stdin = new Scanner(System.in);   
while (!stdin.hasNextInt()) stdin.next();
MondayPrice.add(stdin.nextInt());

http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextInt%28%29

  • It works perfectly. To this case in particular, I was needing a validation like all I made in the code above, but even so, yours was really helpful, and also the link. Thank you very much~ – Charjake08 Apr 13 '15 at 05:22