Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java problem

  • 11-06-2014 4:30pm
    #1
    Closed Accounts Posts: 431 ✭✭


    //So i'm just learning java myself for the last few days and i have a basic program:

    import java.util.Scanner;

    public class test1 {
    public static void main(String args[]){
    int a = 10;
    int b = 20;

    Scanner v1 = new Scanner(System.in);
    System.out.println("Number for a is: " + a + v1.nextLine());

    Scanner v2 = new Scanner(System.in);
    System.out.println("Number for b is: " + b + v2.nextLine());

    System.out.println(a);
    System.out.println(b);

    }
    }

    //the program works but not as i intended it to, i want to have two integers a and b and i want to take user input (Scanner) and add the user input to the integer store it and print the stored value of a and b at the end. What am i doing wrong. Thanks for your help
    Tagged:


Comments

  • Registered Users, Registered Users 2 Posts: 13,080 ✭✭✭✭Maximus Alexander


    Both integers a and b are set to 0, you're printing those zero values along with the any input from the scanner. You never actually assign values to a or b.

    Change
    System.out.println("Number for a is: " + a + v1.nextLine());
    

    to
    a = v1.nextInt();
    System.out.println("Number for a is: " + a);
    


  • Closed Accounts Posts: 431 ✭✭whats newxt


    Both integers a and b are set to 0, you're printing those zero values along with the any input from the scanner. You never actually assign values to a or b.

    Change
    System.out.println("Number for a is: " + a + v1.nextLine());
    

    to
    a = v1.nextInt();
    System.out.println("Number for a is: " + a);
    


    Thanks it worked:
    import java.util.Scanner;
    
    public class test1 {
    	public static void main(String args[]){
    		int a = 10;
    		int b = 20;
    		
    		Scanner v1 = new Scanner(System.in);
    		a = a + v1.nextInt();
    		System.out.println("Number for a is: " + a);
    		
    		Scanner v2 = new Scanner(System.in);
    		b = b + v1.nextInt();
    		System.out.println("Number for b is: " + b);
    		
    		System.out.println("The stored value foa a is: " + a);
    		System.out.println("The stored value for b is: " + b);
    
    	}
    }
    
    [/QUOTE]


  • Registered Users, Registered Users 2 Posts: 13,080 ✭✭✭✭Maximus Alexander


    Thanks it worked:
    import java.util.Scanner;
    
    public class test1 {
    	public static void main(String args[]){
    		int a = 10;
    		int b = 20;
    		
    		Scanner v1 = new Scanner(System.in);
    		a = a + v1.nextInt();
    		System.out.println("Number for a is: " + a);
    		
    		Scanner v2 = new Scanner(System.in);
    		b = b + v1.nextInt();
    		System.out.println("Number for b is: " + b);
    		
    		System.out.println("The stored value foa a is: " + a);
    		System.out.println("The stored value for b is: " + b);
    
    	}
    }
    

    The important thing is though, do you understand why it worked? :) Because I just noticed that I misread your original intention.


  • Closed Accounts Posts: 431 ✭✭whats newxt


    I'm not to sure i think it's because when you use .nextLine it thinks you're working with a string and when your use .nextInt it knows you're working with integers?


  • Registered Users, Registered Users 2 Posts: 13,080 ✭✭✭✭Maximus Alexander


    Yes that's true, and more specifically, the + operator has two functions. On numeric types it performs addition, on Strings it performs concatenation.

    Because you originally had your addition (a + the value of nextLine()) as a parameter to the System.out.println() function, which takes a String, it just concatenated the string representation of a with whatever came in via the scanner. This would still have happened if you had used nextInt().

    For example if:

    a = 10
    v1.nextInt() = 10

    You'd have gotten 1010.


  • Advertisement
  • Closed Accounts Posts: 431 ✭✭whats newxt


    Yes that's true, and more specifically, the + operator has two functions. On numeric types it performs addition, on Strings it performs concatenation.

    Because you originally had your addition (a + the value of nextLine()) as a parameter to the System.out.println() function, which takes a String, it just concatenated the string representation of a with whatever came in via the scanner. This would still have happened if you had used nextInt().

    For example if:

    a = 10
    v1.nextInt() = 10

    You'd have gotten 1010.

    Thanks i see what was happening now, can you recommend any books for learning java most the tutorials on you tube skip over important parts.


  • Registered Users, Registered Users 2 Posts: 13,080 ✭✭✭✭Maximus Alexander


    I got the older 8th edition of this a few years ago and I would highly recommend it. Starts with the basics and works up, covers a lot of ground. It's a big book but it's quite comprehensive and covers the details well. It also has plenty of exercises.


Advertisement