Results 1 to 5 of 5
  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Input from different classes.

    Hi Guys,

    I am working on a program where I want the user to input mulitple classes.
    One int, and one String.
    Can this be done? if so, how?

    I have a tried to get input from both, like in the code below:

    Java Code:
    import java.util.Scanner;
    
    public class ForumFlowchart {
    	public static void main(String[]args){
    		
    		//Creating scanner.
    		Scanner input = new Scanner(System.in);
    
    		//Get information about job
    		System.out.println("Type in Int");
    		int i1 = input.nextInt();
    		System.out.println("Type in String");
                    String s1 = input.nextLine();
    This only lets me input my int, and I do not get the chance to put in my String.

    Java Code:
    import java.util.Scanner;
    
    public class ForumFlowchart {
    	public static void main(String[]args){
    		
    		//Creating scanner.
    		Scanner input = new Scanner(System.in);
    		//Get information about job
    		System.out.println("Type in String");
    		String s1 = input.nextLine();
    		Int i1 = (int)s1;
    		
    		System.out.println("Type in String again");
    		String s2 = input.Line();

    If I keep my input in the same class, I get the error "Can not cast int to string".

    My question is, it is possible to get an input from both an Int and a String in the same program?

    I appreciate all the help :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Input from different classes.

    The Scanner class's nextLine() method returns what currently remains in the class's buffer which can be the line end from the press of Enter following reading a numeric entry.

    With the first program, try entering two items on the line before pressing Enter: 1 name
    and see what is read by the two Scanner methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Input from different classes.

    After I typed in 1 name, I am still not able to make another input.
    Can you post the code and the contents of the command prompt window that shows what you are talking about?

    With the input of: 1 name
    I expected the variable i1 to have the value 1 and s1 the value "name". Is that what happened?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Input from different classes.

    Quote Originally Posted by Norm View Post
    The Scanner class's nextLine() method returns what currently remains in the class's buffer which can be the line end from the press of Enter following reading a numeric entry.

    With the first program, try entering two items on the line before pressing Enter: 1 name
    and see what is read by the two Scanner methods.
    Thank you for your reply.
    What do you mean by "see what is read by the two Scanner methods"?

    After a little back and forth It does work if I write the String first, it let me continue to do the Int next:

    Java Code:
    import java.util.Scanner;
     
    public class ForumFlowchart {
        public static void main(String[]args){
             
            //Creating scanner.
            Scanner input = new Scanner(System.in);
     
            //Get information about job
    
             System.out.println("Type in String");
             String s1 = input.nextLine();
    
             System.out.println("Type in Int");
             int i1 = input.nextInt();
    How ever if I do want the int to be first, I had to change the input.nextLine to input.next();
    This causes a problem if my string is a sentence and not only a word.



    Java Code:
    import java.util.Scanner;
    
    public class forumscannerclasses {
    	public static void main(String[]args){
    	  
    			//Creating scanner.
    		        Scanner input = new Scanner(System.in);
    		 
    		        //Get information about job
    		        System.out.println("Type in Int");
    		        int i1 = input.nextInt();   
    		       
    		        System.out.println("Type in String");
    		        String s1 = input.next();  
    		       
    		        System.out.println(s1);
    So my problem is how to first get input from a int, then a string which is a sentence.


    In my last code;

    1) if I change String s1 = input.next(); to String s1 = input.nextLine(); . Then I am not able to type in any value for the s1 variable after I have typed for the int and pressed enter.

    2) If I keep my code as in the last one, the output for the s1 will only be one word and not the full sentence.
    Last edited by Thammer; 10-18-2014 at 09:15 PM.

  5. #5
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Input from different classes.

    Quote Originally Posted by Norm View Post
    Can you post the code and the contents of the command prompt window that shows what you are talking about?

    With the input of: 1 name
    I expected the variable i1 to have the value 1 and s1 the value "name". Is that what happened?
    yes it is what happend, but it results in me having the same problem when my s1 is a sentence and not only a word.
    Thanks alot for the help!

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  2. Replies: 0
    Last Post: 10-14-2011, 12:33 AM
  3. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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