Java program Prompt the user for a title for data Output the

Java program:

Prompt the user for a title for data. Output the title.

Prompt the user for the headers of two columns of a table. Output the column headers.    

Prompt the user for data points in this format: string, int.

Store the information before the comma in a string variable and the information after the comma into an integer.

The user will enter -1 when they have finished entering data points. Output the data points.

Store the string components of the data points in an ArrayList of strings.

Store the integer components of the data points in a second ArrayList of integers.   

Perform error checking for the data point entries.

If any of the following errors occur, output an error message and prompt again for data points.

• If entry has no comma, output should be: Error - No comma in string.
• If entry has more than one comma, output should be: Error - Too many commas in input.
• If entry after the comma is not an integer, output should be: Error - Comma not followed by an integer.

Output the information in a formatted table.

The title is right justified with a minimum of 33 characters.

Column 1 is left justified with a minimum of 20 characters.

Column 2 is right justified with a minimum of 23 characters.       

Solution

Note : If user enter -1 to stop taking input.

/* Program code begin */

import java.lang.*;
import java.io.*;
import java.util.*;

public class a
{  
        static ArrayList<String> al=new ArrayList<String>();
       static ArrayList<Integer> arrlist = new ArrayList<Integer>(100);
       static String title=\"\";
          
       public static void main(String[] args) throws Exception
   {
          
           a ob=new a();
           InputStreamReader r=new InputStreamReader(System.in);
           BufferedReader br=new BufferedReader(r);
           System.out.println(\"Enter Title of Table \ \");
           title=br.readLine();
           System.out.printf( \"%33s\",title);
          
           int validbit;//=validcheck(d1d2);
           while (1==1)
           {

                   System.out.println(\"Enter headers of two columns of a table :Ex : String,int format or Enter -1 to exit \ \");
                    String d1d2=br.readLine();
                   validbit=validcheck(d1d2);
                   if(validbit==-1)
                       break;
                   if(validbit==3)
                   System.out.println(\"Error - Too many commas in input.\");
                   if(validbit==1)
                   System.out.println(\"No comma in string.\");
                   if(validbit==-2)
                   System.out.println(\"Error - Comma not followed by an integer.\");
                   if(validbit==2)
                   {
                       String delimiter = \",\";
                       String[] tokensVal = d1d2.split(delimiter);
                       int x=0;
                       for(String token : tokensVal)
                       {
                           if(x==0)
                               al.add(token);
                           else
                           {
                               int num=Integer.parseInt(token);
                               arrlist.add(num);
                           }
                           x++;
                      
                       }
                      
                   }

           }
           display();

   }
   public static int validcheck(String d12)
   {
       if( d12.equals(\"-1\"))
       {
           return -1;
       }
       String delimiters = \",\";
       String[] tokensVal = d12.split(delimiters);
       System.out.println(\"Count of tokens = \" + tokensVal.length);
      
       if( tokensVal.length>2)
       {
           return 3;
       }
       if( tokensVal.length<2)
       {
           return 1;
       }
       if(tokensVal.length==2)
       {
               String x=\"\";
               for(String token : tokensVal)
               {
                   x=token;
               }
                try
               {
                   int num=Integer.parseInt(x);
               }
               catch(NumberFormatException e)
               {
                  //If number is not integer,you wil get exception and exception message will be printed
                  System.out.println(\"Second Datapart is not integer\"+e.getMessage());
                  return -2;
               }
       }
       return 2;

   }

   public static void display()
   {


       a ob=new a();
       String a1=\"Author Name\",b1=\"Number of Novels\";
       Iterator itr1=al.iterator();
       Iterator itr2=arrlist.iterator();
               System.out.printf(\"33%s\ \",title);
               System.out.printf(\"20%s | -23%s\ \",a1,b1);

               while((itr1.hasNext())&&(itr2.hasNext()))
           {
                   //Integer number : arrlist;
               System.out.printf(\"20%s | -23%s\ \",itr1.next(),itr2.next());
           }

           Iterator itr3=al.iterator();
       Iterator itr4=arrlist.iterator();
       while((itr3.hasNext())&&(itr4.hasNext()))
           {
                   //Integer number : arrlist;
               System.out.printf(\"%s\",itr3.next());
               int y=(Integer)itr4.next();
               for(int i=0;i<y;i++)
               {
                   System.out.printf(\"*\");
               }
               System.out.printf(\"\ \");
           }
   }
}

Input :

Enter Title of Table

Table Name

                       Table Name Enter headers of two columns of a table :Ex : String,int format or Enter -1 to exit

asd,1

Count of tokens = 2

Enter headers of two columns of a table :Ex : String,int format or Enter -1 to exit

wer,2

Count of tokens = 2

Enter headers of two columns of a table :Ex : String,int format or Enter -1 to exit

tyu,4

Count of tokens = 2

Enter headers of two columns of a table :Ex : String,int format or Enter -1 to exit

Output :

                       Table Name

Author Name | Number of Novels

asd                | 1

wer               | 2

tyu                | 4

asd    *

wer    **

tyu      ****

Java program: Prompt the user for a title for data. Output the title. Prompt the user for the headers of two columns of a table. Output the column headers. Prom
Java program: Prompt the user for a title for data. Output the title. Prompt the user for the headers of two columns of a table. Output the column headers. Prom
Java program: Prompt the user for a title for data. Output the title. Prompt the user for the headers of two columns of a table. Output the column headers. Prom
Java program: Prompt the user for a title for data. Output the title. Prompt the user for the headers of two columns of a table. Output the column headers. Prom

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site