wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help" Watch "help" New topic
Author

help

tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
i am new in java programming
and i need help in this proplem

when iwrote this statment in my program
int aMark1=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the first subject"));

if i insert a text here the program will stop

what shall i do to pass this broplem in my program?

thanks alot
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'm sorry, but I don't understand your question. Can you please post the complete method that contains this line of code? I think that will help clarify what you are asking for. Also it will help if you describe precisely what your program does and how it differs from what you want or expect.

One last suggestion: use a descriptive subject for your question. The word "help" is mostly meaningless here. Most questions on this forum require help. However, something like "Help with JOptionPane" or "Help with parseInt()" or just "Help with my first Java program" is a little more helpful for us to understand what kind of help you are looking for.

Layne


Java API Documentation
The Java Tutorial
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
I am assuming that a NumberFormatException was thrown and the program didn't really just "stop". More details would definately be better.

showInputDialog returns a string, is the string an integer value? If not that is what is causing the problem.
[ January 17, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
import javax.swing.*;
public class Hindi

{

public static void main(String[]args)

{

String aName=JOptionPane.showInputDialog("Enter your name here");
String aDep=JOptionPane.showInputDialog("Enter your school name here");

String aSubject1=JOptionPane.showInputDialog("Enter the name of the first subject");
int aMark1=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the first subject"));

String aSubject2=JOptionPane.showInputDialog("Enter the name of the second subject");
int aMark2=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the second subject"));

String aSubject3=JOptionPane.showInputDialog("Enter the name of the third subject");
int aMark3=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the third subject"));

String aSubject4=JOptionPane.showInputDialog("Enter the name of the fourth subject");
int aMark4=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the fourth subject"));

String aSubject5=JOptionPane.showInputDialog("Enter the name of the fifth subject");
int aMark5=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the fifth subject"));

String aEstimation1=aMark1<50?"faild":aMark1>=50&&aMark1<60?"pass":aMark1>=60&&aMark1<70?"meduim":aMark1>=70&&aMark1<80?"good":aMark1>=80&&aMark1<90?"VGood":aMark1>=90?"excelent":"";
String aEstimation2=aMark2<50?"faild":aMark2>=50&&aMark2<60?"pass":aMark2>=60&&aMark2<70?"meduim":aMark2>=70&&aMark2<80?"good":aMark2>=80&&aMark2<90?"VGood":aMark2>=90?"excelent":"";
String aEstimation3=aMark3<50?"faild":aMark3>=50&&aMark3<60?"pass":aMark3>=60&&aMark3<70?"meduim":aMark3>=70&&aMark3<80?"good":aMark3>=80&&aMark3<90?"VGood":aMark3>=90?"excelent":"";
String aEstimation4=aMark4<50?"faild":aMark4>=50&&aMark4<60?"pass":aMark4>=60&&aMark4<70?"meduim":aMark4>=70&&aMark4<80?"good":aMark4>=80&&aMark4<90?"VGood":aMark4>=90?"excelent":"";
String aEstimation5=aMark5<50?"faild":aMark5>=50&&aMark5<60?"pass":aMark5>=60&&aMark5<70?"meduim":aMark5>=70&&aMark5<80?"good":aMark5>=80&&aMark5<90?"VGood":aMark5>=90?"excelent":"";

double aAverage=(aMark1+aMark2+aMark3+aMark4+aMark5)/5;
String TotalAverag=aAverage<50?"faild":aAverage>=50&&aAverage<60?"pass":aAverage>=60&&aAverage<70?"meduim":aAverage>=70&&aAverage<80?"good":aAverage>=80&&aAverage<90?"VGood":aAverage>=90?"excelent":"";


System.out.println("your name is : "+aName);

System.out.println("______________________");

System.out.println("Subject : "+aSubject1+" "+aSubject2+" "+aSubject3+" "+aSubject4+" "+aSubject5);
System.out.println("Mark : "+aMark1+" "+aMark2+" "+aMark3+" "+aMark4+" "+aMark5);
System.out.println("Estimation : "+aEstimation1+" "+aEstimation2+" "+aEstimation3+" "+aEstimation4+" "+aEstimation5);
System.out.println("______________________");
System.out.println("Your total mark average is : "+aAverage);
System.out.println("You are: "+TotalAverag);


System.exit(0);


}

}


when i insert text in the Integer.parseInt the program is stobed ?

so whats to do???
[ January 17, 2006: Message edited by: tayy abdelqader ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
create a boolean variable, declaring it with a value = false
boolean validEntry = false;

now you create a while loop, in case the user enters non-integer values
while(validEntry == false)
{
}

inside the while loop you have your statement, but note it is this (no 'int' type)
aMark1=Integer.parseInt(JOptionPane.showInputDialog("Enter the mark of the first subject"));

the declaration
int aMark1;
needs to be outside the while loop (before it), so you can use it elsewhere in your program

depending on where you have it declared, it may also need to be initialised
int aMark1 = 0;

inside the while loop, you put a try/catch block around the line with
Integer.parseInt()

also in the try section, after the Integer.parseInt() line, you add
validEntry = true;

now, if the user enters valid data, the while condition will be false (validEntry is now true)
and the program will continue onto the lines after the loop

in the catch section, you could have an error message displayed, with a 'try again'
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
thank u Rusty Shackleford

it is the exact broplem

so what to do to avoid this broplem in my program ???
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Michael Dunn gave the solution. Use a loop, with try/catch blocks.
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
thank u all for your care

but i do not know how to make aloop or blocks

I hope I will try

tanx a lot
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
loops are extremely important. Are you teaching yourself or taking a class? This is why I think learning GUI stuff when learning the basics is a really bad idea. GUI's get in the way of learning because more advanced issues pop up and you don't yet have the tools or knowlege to deal with it or truly understand what is going on. Either way, don't worry about it until you learn about it. But, it will look something like this:


[ January 17, 2006: Message edited by: Rusty Shackleford ]
tayy abdelqader
Greenhorn

Joined: Dec 11, 2005
Posts: 20
thanks a lot i will try to learn more
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Rusty Shackleford:
This is why I think learning GUI stuff when learning the basics is a really bad idea....


I really doubt the OP is "learning GUI stuff" here. It looks like the only "GUI stuff" he is using is JOptionPane, which is a rather convenient way of getting user input. I think it would be even more difficult to try to use the java.io package to try to get user input at this point. I think this is a considerable challenge that most instructors face when teaching Java to students that have never used it before.

Layne
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Before 1.5.0, I think IO was a rough spot for beginners, but is much easier with the Scanner class. I haven't used it much, but it seems to be tailor-made for beginning programmers. It seems to abstract alot of what made IO a headache for newbies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help
 
Similar Threads
java.net.SocketException: IP_ADD_MEMBERSHIP failed
modification is not reflecting in server
Programing windows services in java
secure my code
calculate current window