I am trying to teach myself java and I have a book but it doesnt have how to parse an int into a string. So are there any ways of either doing this or getting round it so I can display my answer? I put in bold the place where the difficulty is. Once I have got this done I can put it on JPanels etc. I am trying to have a goal achieve it and then have another goal with the same programme. Also if you have any tips of teaching yourself java I would be interested to know.

import javax.swing.*;
class percentbible
{

 public static void Main (String[]args)
  {
    String a;
    //enter in number for a string
    a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE); 
    //turn string into number
    int num = Integer.parseInt(a);
    int chapno = 1189;
    int calc= num/chapno*100;   
    String show; 
    //convert integer into string
   [B] show = String.parseString(calc);[/B]
    //display calculation
    show= JOptionPane.showMessageDialog(null,"percentage of chapters read","",JOptionPane.INFORMATION_MESSAGE);
  }




}

Recommended Answers

All 8 Replies

show = String.valueOf(calc);

or
show = Integer.toString(calc);

Now I am getting an error message incompatable types message for the last line which doesnt make sense cus show is a string.

import javax.swing.*;
class percentbible
{

 public static void Main (String[]args)
  {
    String a;
    //enter in number for a string
    a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE); 
    //turn string into number
    int num = Integer.parseInt(a);
    int chapno = 1189;
    int calc= num/chapno*100;   
    String show; 
    //convert integer into string
    show = Integer.toString(calc);
    //display calculation

    show = JOptionPane.showMessageDialog(null,"percentage of chapters read","",JOptionPane.INFORMATION_MESSAGE);
  }




}

Check the API on JOptionPane. showMessageDialog() is a void return - you can't assign it to a variable.

your right I couldnt assign a variable to it so I thought if I made it inherit from the main section I thought I would solve it. My use of technical language with java is poor, I think I have made a superclass that inherits from a subclass but I dont know. What I do know is that I cant get the return type to work and the issue is sumfin 2 do with the 2 lines highlighted in bold.

import javax.swing.*;
class percentbible
{
    public class Calculation
  {
    public static getCalculation(String show)
    {String a;
    //enter in number for a string
    a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE); 
    //turn string into number
    int num = Integer.parseInt(a);
    int chapno = 1189;
    int calc= num/chapno*100;   

    //convert integer into string
    show = Integer.toString(calc);
    //display calculation

    show = JOptionPane.showMessageDialog(null,"percentage of chapters read","",JOptionPane.INFORMATION_MESSAGE);
    return getCalculation(show);
    }
  }

    public static void main (String []args)
    {
    Calculation myCalculation = new Calculation();

    }

}

You need to declare the method like

public static [B]int [/B]getCalculation(){
   // do your calcs

[B]  return calc;[/B]
}

and you can show it like so

int percentComplete = getCalculation();
JOptionPane.showMessageDialog(null,"percentage of chapters read: "+percentComplete,"",JOptionPane.INFORMATION_MESSAGE);

its so frustrating I cant escape this inconpatable types thing. By the way I really appreciate all the help you are giving me. I didnt know how to encorporate the last thing you said aoubt the int percentComplete thing. This is what I have now and the bold line is the incompatable variable type.

import javax.swing.*;
class percentbible
{
    public class Calculation
  {
    public int getCalculation()
    {String a;
    //enter in number for a string
    a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE); 
    //turn string into number
    int num = Integer.parseInt(a);
    int chapno = 1189;
    int calc= num/chapno*100;   
     String show;
    //convert integer into string
    show = Integer.toString(calc);
    //display calculation
    show = JOptionPane.showMessageDialog(null,"percentage of chapters read","",JOptionPane.INFORMATION_MESSAGE);
    return calc; 
    }
  }

    public void main (String []args)
    {
    Calculation myCalculation = new Calculation();

    }

}

I got some help from a lecturer and this what the result. Thanks to you for helping.

import javax.swing.*;
public class PercentBible
{

public static void main (String[]args)
{
String a;
//enter in number for a string
a=JOptionPane.showInputDialog(null,"enter chapters of bible read","",JOptionPane.INFORMATION_MESSAGE);
//turn string into number
int num = Integer.parseInt(a);
int chapno = 1189;

double calc= ((double)num/(double)chapno)*100.0;
String show;
//convert integer into string
show = Double.toString(calc);
//display calculation

JOptionPane.showMessageDialog(null,show,"percentage of chapters read",JOptionPane.INFORMATION_MESSAGE);
}


}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.