Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By Ronin
  • 1 Post By Ronin

Thread: Converting a String to Integer

  1. #1
    Diyanosh is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Question Converting a String to Integer

    Hi my friends ...
    I've started writing a new program that Scans for some strings. I want to specify a random Integer to those Strings in order to do my desired idea. what should I do?!! my codes are here :
    import java.util.Random;
    import java.util.Scanner;


    public class Draw {

    public static void main(String[] args) {
    System.out.println("This Program is written to solve little problems in families cause of doing unwanted works!!");
    System.out.println("Please number of peoples you want to draw! (maximum 5 peoples!)");
    Scanner nS = new Scanner(System.in);
    int n = nS.nextInt();
    for (int i=1;i<=n;i++){
    if (i==1) {
    System.out.println("Please enter the name of person " + i);
    Scanner oneS = new Scanner(System.in);
    String one = oneS.next();
    }
    else if (i==2) {
    System.out.println("Please enter the name of person " + i);
    Scanner twoS = new Scanner(System.in);
    String two = twoS.next();
    }
    else if (i==3) {
    System.out.println("Please enter the name of person " + i);
    Scanner threeS = new Scanner(System.in);
    String three = threeS.next();
    }
    else if (i==4) {
    System.out.println("Please enter the name of person " + i);
    Scanner fourS = new Scanner(System.in);
    String four = fourS.next();
    }
    else if (i==5) {
    System.out.println("Please enter the name of person " + i);
    Scanner fiveS = new Scanner(System.in);
    String five = fiveS.next();
    }
    }


    }
    }


    now I want to Specify an Integer to each person that has been scanned! for example if the first person is " David " , which is a String, in the next step , my desired idea is :

    Random randomNumber = new Random();
    randomNumber.NextInt(101);
    int David = randomNumber.NextInt(101);

    I want to what should I do??!!!
    Thanks ...

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Converting a String to Integer

    First, please place your code between [code][/code] tags.

    You talk about your "idea" but have not really explained what you want to do. Nor do I understand the
    purpose of your loop. I believe that you just need to assign five people a name. Second, if you want to
    associate a value with that name you would need to use a HashMap.

    Regards,
    Jim
    Diyanosh likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Diyanosh is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Converting a String to Integer

    Thank you dear Jim,
    I appreciate for your quick response and thanks for the comment : " First, please place your code between tags. " . I would do so.

    The simple Scenario :
    There is a problem between two brothers ( David & John ) for washing dinner dishes. They write a program that specifies a random number to them and each time the loop continues, a random number adds to their number. the loop terminates in 1000. in that point, one's number is bigger, he is winner!!

    Java Code:
    import java.util.Random;
    
    public class DrawSimple {
    
    	public static void main(String[] args) {
    	Random rS = new Random();
    	rS.nextInt(101);
    	int david,john;
    	for (david=rS.nextInt(101),john=rS.nextInt(101); david <=1001 || john<=1001 ; david += david=rS.nextInt(11),
    			john += john=rS.nextInt(11)); {
    				System.out.println("david's number is :" + david + " and john's number is " + john);
    			}
    	if (david >john){
    		System.out.println("david  is winner");
    	}
    		else if (david <john){
    			System.out.println("john is winner");	
    		}
    		else {
    			System.out.println("Upssss!!!Try another time!!!");
    		}
    			
    		}
    	
    	}

    now I want to do this work for 5 custom people, by getting their names first and then doing same operations as above.
    cause I'm beginner, would you please show me using HashMap?
    Thanks alot ...

  4. #4
    Diyanosh is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Converting a String to Integer

    and my former post's code is here :
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class Draw {
    
    	public static void main(String[] args) {
    		System.out.println("This Program is written to solve little problems in families cause of doing unwanted works!!");
    		System.out.println("Please number of peoples you want to draw! (maximum 5 peoples!)");
    		Scanner nS = new Scanner(System.in);
    		int n = nS.nextInt();
    		for (int i=1;i<=n;i++){
    			if (i==1) {
    				System.out.println("Please enter the name of person " + i);
    				Scanner oneS = new Scanner(System.in);
    				String one = oneS.next();
    			}
    			else if (i==2) {
    				System.out.println("Please enter the name of person " + i);
    				Scanner twoS = new Scanner(System.in);
    				String two = twoS.next();
    			}
    			else if (i==3) {
    				System.out.println("Please enter the name of person " + i);
    				Scanner threeS = new Scanner(System.in);
    				String three = threeS.next();
    			}
    			else if (i==4) {
    				System.out.println("Please enter the name of person " + i);
    				Scanner fourS = new Scanner(System.in);
    				String four = fourS.next();
    		}
    			else if (i==5) {
    				System.out.println("Please enter the name of person " + i);
    				Scanner fiveS = new Scanner(System.in);
    				String five = fiveS.next(); 	
    	     }
        }
    	
    		
    	}
    }

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Converting a String to Integer

    Diyanosh,

    You would be better declaring an array to hold the names and populate this though the use of a loop. With time I'm sure you will come to understand your current solution creates a substantial amount of unnecessary work.

    Using a single loop to accept user input would also reduce the amount of code in your program and make it more readable.

    Regards.
    Diyanosh likes this.

  6. #6
    Diyanosh is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Converting a String to Integer

    Thank you my friend ... I'm Amateur! Of course you are right ... I'll do my best.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Converting a String to Integer

    Going back to your initial idea of having values assigned to each 'player', Jim has already suggested the use of a Hashmap. I'm not going to regurgitate code which can be found after a quick google search but have a look at the Hashmap API. Pay attention to the put and replace methods.

    I have to question your motive behind the use of the random incrimination. I'm not saying its wrong, I'm genuinely curious. A simple random generator could be easily created by using Math.random().

    Regards.
    Diyanosh likes this.

Similar Threads

  1. converting binary to integer
    By Olive in forum New To Java
    Replies: 2
    Last Post: 09-08-2014, 06:20 AM
  2. Converting string to integer
    By Thibisan in forum New To Java
    Replies: 5
    Last Post: 05-18-2012, 11:10 AM
  3. Exception while converting string to integer
    By bharatgs7 in forum New To Java
    Replies: 3
    Last Post: 01-11-2012, 05:06 AM
  4. Converting J001 to integer
    By jaylimix in forum New To Java
    Replies: 4
    Last Post: 12-15-2011, 11:08 PM
  5. Converting an integer into a BigInteger
    By mzjazzygirl in forum New To Java
    Replies: 27
    Last Post: 09-23-2010, 06:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •