wood burning stoves 2.0*
The moose likes Java in General and the fly likes want some help for returning to main please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "want some help for returning to main please" Watch "want some help for returning to main please" New topic
Author

want some help for returning to main please

Muzafar Ali
Ranch Hand

Joined: Jun 11, 2009
Posts: 34
Hello i want help on this program , actually this program asks user to input number then input 2nd number
then it has to add them then print out result.
if user is not inputing numbers it will produce custom msg
now here i want help , when user not input number then it will shows him custom msg
then i want it to re ask user to input 1st number then 2nd number then result.
thanks in advance.

//it asks user to input 2 numbers, it will then add them and print result out.
//if user is puting Strings or chars then program will print out custom msg.

import java.util.Scanner;
public class Addition {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

int a;
int b;
int c;

try {
System.out.print("Enter Number: ");
a=input.nextInt();

System.out.print("Enter 2nd Number: ");
b=input.nextInt();

c = a + b;

System.out.println(a+" + "+b+" = "+c);
}catch(Exception e){
System.out.println("You Should Type Only Numbers!!!");
}
}
}


~When You Learn Anything, Learn by Heart !
Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25


Try-catch not necessary here. Read about Assertion and Try-catch. I think you are confused about their purposes.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Muzafar, please http://faq.javaranch.com/java/UseCodeTags.

Tim-Ting, never use == for comparing strings. Always use equals:
However, that won't work since both a and b are int values.

To catch that, you can use the following code that uses two loops:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Muzafar Ali
Ranch Hand

Joined: Jun 11, 2009
Posts: 34
Thanks friends but that was not my purpose

as i searched over internet there is a Goto method or function by which we can travel any section of program .
actually my question was when user input string instead of numbers then it has to show msg the Goto main section where it was asking for input number !


sorrry for bad english.
Shyam Ramath
Greenhorn

Joined: Jun 24, 2005
Posts: 21
Rob ,



there is no such method 'hasInt()' under Scanner .

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Sorry, that should be hasNextInt() of course. That's what I get for not checking the API...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38006
    
  22
There is a "goto" keyword in Java, but it is never used.

What you mean is you want a method which calls a Scanner, looks for an input from it, uses a hasNextXXX() method as Rob showed you, then returns the input.
You should write a utility class, give it a private constructor, put that sort of method (labelled public static) in the utility class, then you are in action
Muzafar Ali
Ranch Hand

Joined: Jun 11, 2009
Posts: 34
Campbell Ritchie wrote:There is a "goto" keyword in Java, but it is never used.

What you mean is you want a method which calls a Scanner, looks for an input from it, uses a hasNextXXX() method as Rob showed you, then returns the input.
You should write a utility class, give it a private constructor, put that sort of method (labelled public static) in the utility class, then you are in action




labelled method great so labeled method can be called great i will try for that method , i don't heared that method before thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want some help for returning to main please
 
Similar Threads
Making Decisions Lottery application
Remove duplicates in an Arrays
Need Help for an Array.
if statement problem
Need help with output of a program