1

I am learning reading and writing excel files with java. i have written code for creating the bar chart from the excel sheet and i m facing the error that " Cannot get a NUMERIC value from a STRING cell". i am not understanding how to resolve the issue .. Is anyone could help me getting resolve the issue .

here is code .

> import java.io.*; import java.util.*;
> 
> import org.jfree.data.*; import org.jfree.chart.JFreeChart; import
> org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities;
> import org.jfree.chart.plot.PlotOrientation; import
> org.apache.poi.hssf.usermodel.HSSFRow; import
> org.apache.poi.hssf.usermodel.HSSFCell; import
> org.apache.poi.hssf.usermodel.HSSFSheet; import
> org.apache.poi.hssf.usermodel.HSSFWorkbook; import
> org.jfree.data.category.DefaultCategoryDataset; import
> org.apache.poi.ss.usermodel.Cell;
> 
> 
> public class BARCHART{ public static void main(String[]args){ short
> a=0;  short b=1;  int i=0;  ArrayList<String> list1=new
> ArrayList<String>(); ArrayList<Integer> list2=new
> ArrayList<Integer>();
> 
> String x; int y=0;  String filename ="Student Grading Report.xls"; 
> 
> 
> if(filename != null && !filename.equals("")){ try{  FileInputStream fs
> =new FileInputStream(filename);  HSSFWorkbook wb = new HSSFWorkbook(fs);  for(int k = 0; k < wb.getNumberOfSheets(); k++){ 
> int j=i+1;  HSSFSheet sheet = wb.getSheetAt(k);  int rows =
> sheet.getPhysicalNumberOfRows();  for(int r = 1; r < rows; r++){ 
> HSSFRow row = sheet.getRow(r);  int cells =
> row.getPhysicalNumberOfCells();  HSSFCell cell1 = row.getCell(a); 
> 
> x =(String) cell1.getStringCellValue();  HSSFCell cell2 =
> row.getCell(b);  y =(int) cell2.getNumericCellValue(); 
> 
> list1.add(new String(x)); list2.add(new Integer(y));  }  i++;  } 
> }catch(Exception e){  System.out.println(e);
> 
> }
> 
> } DefaultCategoryDataset dataset = new DefaultCategoryDataset();
> for(int j=0;j<list2.size();j++){
> dataset.setValue((double)list2.get(j), "Marks",
> list1.get(j).toString()); } JFreeChart chart =
> ChartFactory.createBarChart("BarChart","Marks", "St_Id", dataset, 
> PlotOrientation.VERTICAL, false,true, false); try {
> ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart,400, 300);
> } catch (IOException e) { System.out.println("Problem in creating
> chart."); } } }

anyone one who can help me in resolving such issue? Thankyou :)

1
  • 1
    Reformat your code to make it readable first ;) Commented Jun 15, 2017 at 19:55

2 Answers 2

1

If you try to save a string into a double or integer in Java, it will reject this as it is a strongly typed language. You can instead use the lines of code

Integer.parseInt(*desiredVariable*)

or

Double.parseDouble(*desiredVariable*)

to change these strings into the right data type.

Sign up to request clarification or add additional context in comments.

4 Comments

It is not working because method parse is not applicable for the ArrayList.. any other suggestions??
Can you point out the specific line in the question which is causing the error? It should tell you in your console.
When i used the parse method it displays an error that parse is not applicable for String Array list as in used to declare the list1 as array list in start of program. and when i changed the declaration of array list string to simple array it displays error in line 46,47 and 48 that add method is not applicable for simple strings.. Cannot help with this..
The method get(int) is undefined for the type String ... this is the error that displays
0
   HSSFCell yourCell = sheet.getRow(row).getCell(column);
   yourCell.setCellType(Cell.CELL_TYPE_STRING);
   String data = yourCell.getStringCellValue();

1 Comment

How to use this in my code? because m not getting it. if you kindly edit the code? thankyou

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.