Question

Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

Solve it by JAVA please.

Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total.

Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it is added to the integer accumulator. The accumulator keeps a running total of all the integers which have been seen so far. Likewise, there is an accumulator for floating point values. It starts at zero, but adds every floating point value which is seen. Finally, there is a string accumulator. It starts out as an empty string, but appends to the string every time another string token is read. Your class will need to be able to decide the type of each input token (hint: a Scanner has the methods hasNextInt(), hasNextDouble(), and hasNext()) as well as to keep track of the running totals (hint: you can use instance variables).

The only import statement you may use in this implementation is the Scanner class. Any data elements should be declared private, although this will not be graded. If you think it is necessary you may include helper methods of your own. The class should implement the following public methods:

  • public InputSplitter() this constructor will initialize the three accumulators as well as the Scanner which is used for input.
  • public void next() read the next token, and print its type and value. This method will use the input Scanner object which was initialized earlier to read the next token from the input. Depending on the type of the input token, method will print the value of the token in one of the following three formats:
  • In addition to the printed output, the value will be added (appended, in the case of a string) to the accumulator for the specific type.
  • public int getIntTotal() retrieves the total value summed in the integer accumulator.
  • public double getDoubleTotal() retrieves the total value summed in the floating point accumulator.
  • public String getStringTotal() retrieves the complete value concatenated in the string accumulater.
  • public String toString() returns the current contents of each of the three accumulators as a single string in the following format
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi there,

Following is the code,If you still have any queries,feel free to ask in the comments box.

Code:

import java.util.Scanner;

public class InputSplitter {
     int inttotal;
   double doubletotal;
    String stringtotal;
  
    static Scanner sc=new Scanner(System.in);
  
    InputSplitter()//constructor
    {
       inttotal=0;
       doubletotal=0;
       stringtotal="";
      
    }
  
  
    public void next()
    {    if(sc.hasNextInt())//if it is an integer
        {
       int a=sc.nextInt();
       System.out.println("You entered a Integer value : "+a);
        inttotal+=a;//adding to integer acc
        }
     
       else if(sc.hasNextDouble())//if it is a double
       {
           double d=sc.nextDouble();
           System.out.println("You entered a Double value : "+d);
           doubletotal+=d;//adding to double acc.
       }
  
       else if(sc.hasNext())//if it is a string
       {   String s=sc.next();
           System.out.println(" You entered a String value : "+s);
           stringtotal+=s;//adding to string acc.
       }
      
      
    }
  
  
  
  
   public int getInttotal() {
       return inttotal;
   }


   public double getDoubletotal() {
       return doubletotal;
   }


   public String getStringtotal() {
       return stringtotal;
   }

   public String toString()//display result
   {
       return "Integer Total : "+inttotal+"\nDouble Total : "+doubletotal+"\nString Total : "+stringtotal;
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       int choice=1;
       InputSplitter i=new InputSplitter();
      
       while(choice==1)//loop to accept till user wishes
       {   System.out.println("Enter something! ");
           i.next();
           System.out.println("Do you wish to continue ? (-1 for no and 1 for yes)");
           choice=sc.nextInt();
       }
      
       System.out.println(i);//print details using tostring method
      
  
      
      
   }

}


Output::

Enter something!
John
You entered a String value : John
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
12
You entered a Integer value : 12
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
13
You entered a Integer value : 13
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
James
You entered a String value : James
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
12.3
You entered a Double value : 12.3
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
5.6
You entered a Double value : 5.6
Do you wish to continue ? (-1 for no and 1 for yes)
1
Enter something!
USA
You entered a String value : USA
Do you wish to continue ? (-1 for no and 1 for yes)
-1
Integer Total : 25
Double Total : 17.9
String Total : JohnJamesUSA

Add a comment
Know the answer?
Add Answer to:
Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...
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
  • Please USE JAVA only Write a class named InputSplitter. This class will take input from the...

    Please USE JAVA only Write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it is added...

  • Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...

    Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...

  • The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

    The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that is an ArrayList of Token objects. Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content. In this case there will only be one keyword: public. Tokenizer should have a default constructor that initializes the ArrayList of Token objects Tokenizer should have a public method called: tokenizeFile tokenizeFile should...

  • Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use...

    Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. CODE PROVIDED: import java.util.ArrayList; import java.util.Scanner; public class Problem3 {    public static void main( String [] args ) {        Scanner in = new Scanner( System.in );        //PLEASE START YOUR WORK HERE        //**********************************************************                                                  //**********************************************************        // PLEASE END YOUR WORK HERE        System.out.print("END OF OUTPUT");    } } 20 points! PROBLEM 3: EXTENDED FAMILY Complete this...

  • Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

    Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase of program compilation Assignment: The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser. Write a Java, program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described...

  • JAVA

    Write a Java class (program) which inputs a simple integer and outputs a list of values from the input variable down to 0 (not including 0). You should only show every other value in the output, starting with the input value. You must use a for loop.For example, if the input is 23, the output should be:The input value was 2323 21191715131197531Code given:import java.util.Scanner;/**  * This program counts down from the input number down to (but not including)   * 0, skipping...

  • A class Scanner in Java can be used to get user input and its methods can...

    A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the...

  • The Scanner class in Java has several methods for reading values entered from the keyboard. The...

    The Scanner class in Java has several methods for reading values entered from the keyboard. The methods include next, nextInt, and nextLine. Which of the following statements is INCORRECT about the methods? Pick the best applicable answer Assuming that the user has typed: Hello World! If you invoke the next method for the first time, it will return the string "Hello", and if you invoked next method for the second time, it will return the string "World!" If you want...

  • (a) A class Scanner in Java can be used to get user input and its methods...

    (a) A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

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