Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want some help for returning to main please

 
Muzafar Ali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!");
}
}
}
 
Tim-Ting Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try-catch not necessary here. Read about Assertion and Try-catch. I think you are confused about their purposes.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Muzafar Ali
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob ,



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

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that should be hasNextInt() of course. That's what I get for not checking the API...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic