• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling Positive String Data, Negative String Data (Invalid Data) in Computer Programming

 
Ranch Hand
Posts: 104
Netbeans IDE Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Team,
Thanks in advance. Sorry for inconvenience If any

a) I studied Computer Science (Tamil Medium) in Higher Secondary Certificate course in 1996-1998.
That time I studied BASIC language.
I learned about testing using positive data and negative data.
Example:
Enter age of the person: 10
10 is positive number. Allowed positive data
Enter age of the person: -5
-5 is negative number is not valid number
We should throw error message, If negative age is entered

b) Similarly for String input, positive data scenario, negative data scenario  (Invalid Data scenario) needs to be handled

In Java, I am giving example

Below program taken from website  https://www.w3schools.com/java/java_user_input.asp





Run:              
Enter username
Babu.M

Username is: Babu.M

If the username is entered as "Idiot", Then how to handle String invalid data or String negative data? Please guide me
If the username is entered as "I****", Then how to handle String  invalid data or String negative data? Please guide me

Special characters in String input may handled via Regular Expression

c) I feel Data mining concepts may help in handling invalid String data or negative String Data or junk Data
From 1996 to till date I am fearing about How to handle difficult customer, Life taking negotiations in Computer Programming when collecting input data from customer?

Please guide me on this

Thank you
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd need to start with a clear specification as to what constitutes a valid String. Only then can you decide on a technique for implementation and test cases.
 
Babu Muthukrishnan
Ranch Hand
Posts: 104
Netbeans IDE Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Team, I posted this query in Chat GPT. I got response as follows
--------------------------------------
Hello! I’m glad you reached out to me for help. I’ll do my best to assist you.

a) It’s great that you learned about testing using positive and negative data in your Computer Science course. To handle negative age input in Java, you can use an if statement to check if the input is negative. If it is, you can throw an exception with a message indicating that negative age is not valid. Here’s an example:

Java
AI-generated code. Review and use carefully. More info on FAQ.

Scanner scanner = new Scanner(System.in);
System.out.println("Enter age of the person: ");
int age = scanner.nextInt();
if (age < 0) {
   throw new IllegalArgumentException("Negative age is not valid.");
}
b) Similarly, to handle invalid string input, you can use an if statement to check if the input is valid. If it is not, you can throw an exception with a message indicating that the input is invalid. Here’s an example:

Java
AI-generated code. Review and use carefully. More info on FAQ.

Scanner scanner = new Scanner(System.in);
System.out.println("Enter username: ");
String username = scanner.nextLine();
if (!username.matches("[a-zA-Z]+")) {
   throw new IllegalArgumentException("Invalid username.");
}
In the above example, the matches() method checks if the input string contains only alphabets. If it doesn’t, an exception is thrown with a message indicating that the input is invalid.

c) Data mining concepts can be useful in handling invalid string data or negative string data. However, they may not be necessary for simple input validation tasks. In general, it’s a good idea to validate user input as early as possible in your program to prevent errors from propagating further down the line.

Regarding your concern about handling difficult customers and negotiations, it’s important to remain calm and professional when dealing with customers. Active listening, empathy, and clear communication can go a long way in resolving conflicts and building positive relationships with customers. Additionally, having a well-defined process for handling customer complaints can help ensure that issues are addressed in a timely and effective manner.

I hope this helps! Let me know if you have any other questions.
-------------------------------------------

Thank you
 
Marshal
Posts: 28140
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And did its response help you?
 
Babu Muthukrishnan
Ranch Hand
Posts: 104
Netbeans IDE Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul Clapham,
Thanks for reply.
Problem is communicated via this Forum and Post.
I received some answer via Chat GPT.
How various companies implementing solution to problem, How companies handling customer and customer data are not in my control.
Here after I will focus on my work etc.

Thank you
 
Babu Muthukrishnan
Ranch Hand
Posts: 104
Netbeans IDE Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Team,

I posted my other query to Chat GPT
Chat GPT response is as follows
------------------------------------------------------
In Java program, when user collecting input from customer, If user entered bad word in input, what to do? Please guide me

If you want to censor bad words in Java, you can use the replaceAll() method to replace the bad words with asterisks. Here’s an example:

Java
AI-generated code. Review and use carefully. More info on FAQ.

import java.util.Scanner;

class Main {
 public static void main(String[] args) {
   Scanner myObj = new Scanner(System.in);
   String userName;
   
   // Enter username and press Enter
   System.out.println("Enter username: ");
   userName = myObj.nextLine();  
       
   if (userName.matches(".*\\b(Idiot|stupid|fool|dummy|moron)\\b.*")) {
     userName = userName.replaceAll("\\b(Idiot|stupid|fool|dummy|moron)\\b", "****");
     System.out.println("Censored username is: " + userName);
   } else {
     System.out.println("Username is: " + userName);
   }
 }
}
In this example, if the user enters a bad word such as “Idiot”, “stupid”, “fool”, “dummy”, or “moron”, the program will replace the bad word with asterisks and print the censored username. Otherwise, it will print the username.

You can add more bad words to the regular expression by separating them with the | character. For example, to add the bad word “jerk”, you can modify the regular expression to ".*\\b(Idiot|stupid|fool|dummy|moron|jerk)\\b.*".

I hope this helps! Let me know if you have any other questions.

-----------------------------------------------------



Thank you
 
Marshal
Posts: 79063
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Babu Muthukrishnan wrote:. . . How various companies implementing solution to problem[?] . . .

My experience is that many companies implement such input checking incorrectly. For example, many sites will complain if I enter my card number 4567 8910 1112 1314, even though that is the correct format as on the card, and it would be very easy to remove the spaces. No, they insist on 4567891011121314. I had difficulties with a parcel last year because I mistakenly entered the recipient's phone number wrongly, with ten keystrokes. It would have been very easy to show an error message saying the phone number should have eleven digits.
Verifying names may not be possible. Yes, you can create a Set of prohibited words and reject any input in that set. Yes, you can divide the name into first and last names, but that won't allow for places where people only have a single name, nor Russia where everybody has a three‑part name. Yes, you can verify that the name contains letters, but that will fail to verify François (French), José (Spanish), Anne‑Marie (I think English: definitely common in England), O'Neill (Irish: note capital letter in the middle of the name), Jaružełski (Polish), nor that fact that Mr Jaružełski is married to Mrs Jaružełska. Look at this blog, including No 31

By the way: please don't use positive to mean valid nor negative to mean invalid. And also remember that numbers cannot be exhaustively described as positive or negative. If you are very young, then 0 is a valid age. Fortunately, ChatGPT seems to have worked that out.

[edit]Addition:- I see ChatGPT has tried to censor things by editing your regular expression. Did it takes case into consideration? I think you will get better performance and reliability from a Set of banned words. Not that you will notice a few microseconds' difference when it takes several seconds for the users to write their names.
 
Campbell Ritchie
Marshal
Posts: 79063
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Babu Muthukrishnan wrote:. . . the program will replace the bad word with asterisks . . .

A long time ago I was at a well‑known University in LA and the email system had such censorship. I had something to drink and then thought I would wash it down with some beer. And the system changed that to, “waXX XX down…”
 
Paul Clapham
Marshal
Posts: 28140
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, the famous Scunthorpe problem.
 
Campbell Ritchie
Marshal
Posts: 79063
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scunthorpe is a problem in its own right
 
reply
    Bookmark Topic Watch Topic
  • New Topic