4 Replies - 790 Views - Last Post: 28 June 2011 - 06:31 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

How to user-input a char so that i could convert it into int (JAVA) ?

Posted 23 June 2011 - 09:57 AM

I want to give a char or string to num1 and then check if it contains number. So i figured, if I was able to user-input a string or char in num1 and then convert it into an int. P.S Sorry if its easy, i'm new to computer science. :helpsmilie:

import java.util.Scanner;

public class Addition {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        int num1;
        int num2;
        int sum;

        System.out.print("Type your first number: ");
        num1 = input.nextInt();
        do {
            if (num1 < 0) {
                System.out.print("Please type a positive integer: ");
                num1 = input.nextInt();
            }
        } while (num1 < 0);

        System.out.print("Type your second number: ");
        num2 = input.nextInt();
        do {
            if (num2 < 0) {
                System.out.print("Please type a positive integer: ");
                num2 = input.nextInt();
            }
        } while (num2 < 0);

        sum = num1 + num2;
        System.out.print(sum + "\n");
    }
}

This post has been edited by deprosun: 23 June 2011 - 10:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to user-input a char so that i could convert it into int (JAVA) ?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: How to user-input a char so that i could convert it into int (JAVA) ?

Posted 23 June 2011 - 10:12 AM

Before calling nextInt you can call input.hasNextInt() which will return true if the user inputs an integer and false otherwise.

So you can do something like this:

System.out.print("Please type a positive integer: ");
if(input.hasNextInt()) {
    num1 = input.nextInt();
} else {
    System.out.println("That wasn't an integer, doofus!")
}


This post has been edited by sepp2k: 23 June 2011 - 10:14 AM

Was This Post Helpful? 2
  • +
  • -

#3 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: How to user-input a char so that i could convert it into int (JAVA) ?

Posted 27 June 2011 - 01:13 PM

Thanks for giving an idea for another way to do it, but i find try--catch more useful. I was trying to do it through try--catch statement. And I had been busting my head syntactical and logical wise to find out why my loop of try--catch statement wont stop when it gets a false statement. Here is the the output:
Type your first number: e
Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer. 
Type your first number: Invalid input! Please type an Integer......and so on 


here is the actual code:

import java.util.Scanner;

 

public class Addition {

 

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        int num1=0;

        int num2;

        int sum;
        boolean check=false;

 
        do{
        try{
            System.out.print("Type your first number: ");
            num1 = input.nextInt();
            check = true;
        }
        catch(java.util.InputMismatchException x){
            System.out.println("Invalid input! Please type an Integer. ");
            num1=0;
            check = false;
        }
        }while (check==false);

 

        System.out.print("Type your second number: ");

        num2 = input.nextInt();

        sum = num1 + num2;

       System.out.print(sum + "\n");

    }

}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to user-input a char so that i could convert it into int (JAVA) ?

Posted 27 June 2011 - 06:36 PM

better to:
    boolean numIsOk = false;
    while(!numIsOk) {
       System.out.print("Enter number: ");
       String line = input.nextLine();
       try {
         num1 = Integer.parseInt(line);
         numIsOk = num1 >= 0;
       }
       catch(Exception e) {}
       if(!numIsOk)
         System.out.println(str + " is not a valid integer");
    }



Also if you have 3 numbers to enter better to write a method

int getNum(String question) {
    while(true) {
       System.out.print(question);
       String line = input.nextLine();
       try {
         int num = Integer.parseInt(line);
         if(num > 0)
           return num;
       }
       catch(Exception e) {}
       System.out.println(str + " is not a valid integer");
    }
}


This post has been edited by pbl: 27 June 2011 - 06:48 PM

Was This Post Helpful? 2
  • +
  • -

#5 deprosun  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 292
  • Joined: 16-November 10

Re: How to user-input a char so that i could convert it into int (JAVA) ?

Posted 28 June 2011 - 06:31 AM

oh much better logic, thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1