Question

Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error mess
Write a class definition file that has a string field to hold a possible floating point number. Name this class Validate Flo
If the code does not compile the grade is a zero. documentation- Javadoc, inline comments, conventions, proper use of white s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to your question. I tried my best to solve your doubt, however, if you find it is not as good as expected by you. Please do write your further doubts regarding this question in the comment section, I will try to resolve your doubts regarding the submitted solution as soon as possible.
If you think, the solution provided by me is helpful to you please do an upvote.

ValidateFloat.java
1. 2. import java.util.*; import java.lang.StringBuilder; class ValidateFloat 3. 4. 5. boolean isvalid (String var, StringBui

import java.util.*;

import java.lang.StringBuilder;

class ValidateFloat

{

boolean isvalid(String var,StringBuilder err)

{

boolean validc=true;

int dptcnt=0;

int ecnt=0;

int signcnt=0;

boolean result=true;

for(int i=0;i<var.length();i+=1)

{

if(!((('0'<=var.charAt(i)) && (var.charAt(i)<='9')) || (var.charAt(i)=='E') || (var.charAt(i)=='.') || (var.charAt(i)=='+') || (var.charAt(i)=='-')))

{

validc=false;

}

if(var.charAt(i)=='E')

ecnt+=1;

if(var.charAt(i)=='.')

dptcnt+=1;

if(var.charAt(i)=='+' || var.charAt(i)=='-')

signcnt+=1;

}

if(validc==false)

{

result=result && false;

err.append("Conatins Invalid characters\n");

}

if(ecnt>1)

{

result=result && false;

err.append("E occurs more than once\n");

}

if(dptcnt>1)

{

result=result && false;

err.append("Decimal Point occurs more than once\n");

}

if(signcnt>2)

{

result=result && false;

err.append("'+' and '-' are more than twice");

}


return result;

}

}

Main.java
{ { 1. import java.util.*; 2. import java.lang.StringBuilder; 3. class Main 4. { 5. public static void main(String[] args) 6.

import java.util.*;

import java.lang.StringBuilder;

class Main

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

ValidateFloat cl1=new ValidateFloat();

while(true)

{

String var=sc.nextLine();

if(var.equals("quit"))

{

break;

}

StringBuilder err=new StringBuilder();

boolean result=cl1.isvalid(var, err);

if(result)

System.out.println(var+" is a valid Floating Point Number.\n");

else

System.out.println(var+" is not a valid Floating Point Number, because\n"+err);

}

}

}

Output

3.14159
3.14159 is a valid Floating Point Number.

-2.54
-2.54 is a valid Floating Point Number.

2.453E3
2.453E3 is a valid Floating Point Number.

66.3E-5
66.3E-5 is a valid Floating Point Number.

3.a134
3.a134 is not a valid Floating Point Number, because
Conatins Invalid characters

-2.5.4
-2.5.4 is not a valid Floating Point Number, because
Decimal Point occurs more than once

2.45E3E
2.45E3E is not a valid Floating Point Number, because
E occurs more than once

-3.45-34E+5
-3.45-34E+5 is not a valid Floating Point Number, because
'+' and '-' are more than twice
-2w..AEE+-1123
-2w..AEE+-1123 is not a valid Floating Point Number, because
Conatins Invalid characters
E occurs more than once
Decimal Point occurs more than once
'+' and '-' are more than twice
quit

Add a comment
Know the answer?
Add Answer to:
Using loops with the String and Character classes. You can also use the StringBuilder class to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions...

    This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program. Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class. The regular expression you will need for a Phone number is: "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$" and for an SSN: "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$" This exercise is meant to be done with the video listed in the...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • In Java Only can use class String length charAt class StringBuilder length charAt append toString class...

    In Java Only can use class String length charAt class StringBuilder length charAt append toString class Character any method Create the following methods nthWord takes an int and a String as input and returns a String: The input int represents a number n that is assumed to be positive, and the output string contains every nth word of the input string, starting with the first word, separated by a single space. {\em For this method, a word is defined to...

  • Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character...

    Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character class and String class to process text ? Be able to use the StringTokenizer and StringBuffer classes Introduction In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will...

  • You will be creating a driver class and 5 class files for this assignment. The classes...

    You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • Exercise 1: Adding a Test Harness to the Person class To make it easier to test...

    Exercise 1: Adding a Test Harness to the Person class To make it easier to test code that you have written for a Java class you can add to that class a main method that acts as a "test harness". A test harness is a main method that includes calls to methods that you wish to test. For convention this main method is the last method of the class. If you have a test harness, you do not need to...

  • *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount...

    *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount -Read the requirement of each part; write the pseudo-code in a word document by listing the step by step what you suppose to do in main() and then save it with the name as Lab4_pseudoCode_yourLastName *Step2: -start editor eClipse, create the project→project name: FA2019_LAB4PART1_yourLastName(part1) OR FA2019_LAB4PART2_yourLastName (part2) -add data type classes (You can use these classes from lab3) Account_yourLastName.java CheckingAccount_yourLastName.java SavingAccount_yourLastName.java -Add data structure...

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT