4 Replies - 254 Views - Last Post: 02 December 2017 - 04:29 AM Rate Topic: -----

#1 AnonMom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 17

Error: Comma not followed by an integer.

Posted 29 November 2017 - 07:22 PM

I am working on a data visualizer assignment and I am having problems with my code that compiles whether there is an integer followed by a comma. And when I enter -1 to quit I am getting this error. Thanks in advance

Exception in thread "main" java.lang.NumberFormatException: For input string: "seventythree"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at DataVisualizer.main(DataVisualizer.java:
import java.util.Scanner;
import java.util.ArrayList;

public class DataVisualizer {
   public static void main(String[] args) {
      /* Type your code here. */
      
    String title;
      String coln1,coln2;

      //Arraylist for column1 value and column2 value

      ArrayList<String> colnVal1=new ArrayList<>();
      ArrayList<Integer> colnVal2=new ArrayList<>();

      Scanner scnr=new Scanner(System.in);

      //get title

      System.out.println("Enter a title for the data:");
      title=scnr.nextLine();

      System.out.println("You entered: "+title);
      System.out.println();

      //get coln1

      System.out.println("Enter the column 1 header:");
      coln1=scnr.nextLine();

      System.out.println("You entered: "+coln1);
      System.out.println();
      
      //get coln2

      System.out.println("Enter the column 2 header:");
      coln2=scnr.nextLine();

      System.out.println("You entered: "+coln2);
      System.out.println();
      
      //Get values until -1
      boolean isNumeric;

      while(true)
      {

      System.out.println("Enter a data point (-1 to stop input):");
      String dataPoint=scnr.nextLine();

      if(dataPoint.equals("-1"))

                   break;

      int commaCnt=0;

      //Loop to count comma

      for(int i=0;i<dataPoint.length();i++)
         {
          if(dataPoint.charAt(i)==',')
          commaCnt++;
         }

      //If no-comma

         if(commaCnt==0)
         {
            System.out.println("Error: No comma in string.");
            System.out.println();
            continue;
         }

       //If more than one-comma

         if(commaCnt>1)
         {  
            System.out.println("Error: Too many commas in input.");
            System.out.println();
            continue;

         }

      String[] split=dataPoint.split(",");

      //if integer value is not in the string
       
         //if(!split[1].matches("\\d"))
         for(int i = 0; i < coln1.length(); ++i)
         {
         if(Character.isDigit(coln1.charAt(i)))
         {
             isNumeric = false;
             System.out.println("Error: Comma not followed by an integer.");
             System.out.println();
             continue;
          }
         }
            
         colnVal1.add(split[0]);
         colnVal2.add(Integer.parseInt(split[1]));
         
         System.out.println("Data string: " + split[0]);
         System.out.println("Data integer: " + split[1]);
         
         //System.out.println("Data string: " + coln1);
         //System.out.println("Data integer: " + coln2);

      }
      

         System.out.printf("%27s%n",title);

         System.out.printf("%-25s|%25s%n",coln1,coln2);
         for (int i = 0; i < 51; i++)
         {
            System.out.print("-");
         }
            System.out.println();

         //System.out.println(colnVal1.toString());

      //Loop to print table

      for(int table=0;table<colnVal1.size();table++)
      { 
        // System.out.printf("%c-%");
         System.out.printf("%-25s|%25d%n",colnVal1.get(table), colnVal2.get(table));

      }
        System.out.println();
      
      //Loop to print histogram
      
      for(int gram=0;gram<colnVal1.size();gram++)
      {
       // Display column 1 in right justify

            String column1 = colnVal1.get(gram);

            for (int j = 0; j < (20 - column1.length()); j++)

            {
                System.out.print(" ");
            }

            System.out.print(column1 + " ");

            // Display column 2 in left justify

            int column2 = colnVal2.get(gram);

            for (int j = 0; j < column2; j++)

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

   }




Is This A Good Question/Topic? 0
  • +

Replies To: Error: Comma not followed by an integer.

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 858
  • View blog
  • Posts: 3,429
  • Joined: 13-June 14

Re: Error: Comma not followed by an integer.

Posted 29 November 2017 - 11:43 PM

The exception itself is pretty clear: the string "seventythree" was entered and it couldn't be converted to a number (while the text represents a number to a human, it's just a sequence of character to the computer). You also haven't shown the entire stack trace, which will tell you on which line of the file the problem occurred.
Was This Post Helpful? 0
  • +
  • -

#3 AnonMom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-November 17

Re: Error: Comma not followed by an integer.

Posted 01 December 2017 - 08:33 PM

I have made some corrections to the code above and want to see if anyone can help me figure out how to output the error message and just prompt again for data. My code is throwing an error when some one enters "seven" instead of 7.
The error message is displayed correctly but then this error is thrown
Exception in thread "main" java.lang.NumberFormatException: For input string: "seven"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at datavisualizer.DataVisualizer.main(DataVisualizer.java:122)
I have tried to take the boolean out of the statement and also tried to compare to a string. I am using zybooks so the program only accepts certain ways to use the error catch. Wondering if someone could help out? Thanks in advance
import java.util.Scanner;
import java.util.ArrayList;

public class DataVisualizer {
   public static void main(String[] args) {
      /* Type your code here. */
      
     String title;
      String coln1,coln2;

      //Arraylist for column1 value and column2 value

      ArrayList<String> colnVal1=new ArrayList<>();
      ArrayList<Integer> colnVal2=new ArrayList<>();

      Scanner scnr=new Scanner(System.in);

      //get title

      System.out.println("Enter a title for the data:");
      title=scnr.nextLine();

      System.out.println("You entered: "+title);
      System.out.println();

      //get coln1

      System.out.println("Enter the column 1 header:");
      coln1=scnr.nextLine();

      System.out.println("You entered: "+coln1);
      System.out.println();
      
      //get coln2

      System.out.println("Enter the column 2 header:");
      coln2=scnr.nextLine();

      System.out.println("You entered: "+coln2);
      System.out.println();
      
      //Get values until -1
      boolean comma = true;
      while(true)
      {

      System.out.println("Enter a data point (-1 to stop input):");
      String dataPoint=scnr.nextLine();

      if(dataPoint.equals("-1"))

                   break;

      int commaCnt=0;

      //Loop to count comma

      for(int i=0;i<dataPoint.length();i++)
         {
          if(dataPoint.charAt(i)==',')
          commaCnt++;
         }

      //If no-comma

         if(commaCnt==0)
         {
            System.out.println("Error: No comma in string.");
            System.out.println();
            continue;
         }

       //If more than one-comma

         if(commaCnt>1)
         {  
            System.out.println("Error: Too many commas in input.");
            System.out.println();
            continue;

         }

      String[] split=dataPoint.split(",");

      //if integer value is not in the string
      
    boolean isNumeric = true;
        for(int i = 0; i < split[1].length(); ++i)
         {
         if(!Character.isDigit(split[1].charAt(i)))
         {
             
             System.out.println("Error: Comma not followed by an integer.");
             System.out.println();
             isNumeric = false;
             break;
          }
         
         }
      
         colnVal1.add(split[0]);
         colnVal2.add(Integer.parseInt(split[1]));
         
         System.out.println("Data string: " + split[0]);
         System.out.println("Data integer: " + split[1]);
      }
  
     
         System.out.printf("%33s%n",title);

         System.out.printf("%-20s|%23s%n",coln1,coln2);
         for (int i = 0; i < 44; i++)
         {
            System.out.print("-");
         }
            System.out.println();

         //System.out.println(colnVal1.toString());

      //Loop to print table

      for(int table=0;table<colnVal1.size();table++)
      { 
        // System.out.printf("%c-%");
         System.out.printf("%-20s|%23d%n",colnVal1.get(table), colnVal2.get(table));

      }
        System.out.println();
      
      //Loop to print histogram
      
      for(int gram=0;gram<colnVal1.size();gram++)
      {
       // Display column 1 in right justify

            String column1 = colnVal1.get(gram);

            for (int j = 0; j < (20 - column1.length()); j++)

            {

                System.out.print(" ");

            }

            System.out.print(column1 + " ");

            // Display column 2 in left justify

            int column2 = colnVal2.get(gram);

            for (int j = 0; j < column2; j++)

            {

                System.out.print("*");

            }

            System.out.println();

        }

      }
   }
   



Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 858
  • View blog
  • Posts: 3,429
  • Joined: 13-June 14

Re: Error: Comma not followed by an integer.

Posted 01 December 2017 - 11:55 PM

You need to learn about exception handling.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,236
  • Joined: 20-September 08

Re: Error: Comma not followed by an integer.

Posted 02 December 2017 - 04:29 AM

Quote

My code is throwing an error when some one enters "seven" instead of 7.

At the risk of stating the obvious - you do realize that Java does not, and cannot parse numbers expressed in words, don't you? (Unless of course you write all the code to do that yourself)

Quote

I am using zybooks so the program only accepts certain ways to use the error catch.

What is that and what do you mean by that?

This post has been edited by g00se: 02 December 2017 - 04:31 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1