Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java problem

  • 11-06-2014 05: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