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 Help

  • 25-11-2012 10:24pm
    #1
    Registered Users, Registered Users 2 Posts: 82 ✭✭


    A simple enough question.Write a program that inputs three numbers and determines and prints a number that can be divided by 3 or 5.If a number can be divided by both 3 and 5 then this will also be displayed.Unsure of how to get my original input back out?:confused: ie the empty parentheses of System.out.print("");....I have tried x and input.nextInt() ;


    import java.util.Scanner ;
    public class CanANumberBeDividedBy3or5
    {
    public static void main(String [] args)
    {
    Scanner input=new Scanner(System.in) ;

    int x,y,z ;

    System.out.print("Enter first integer");
    x=input.nextInt() ;

    System.out.print("Enter second integer");
    y=input.nextInt() ;

    System.out.print("Enter third integer");
    z =input.nextInt() ;

    if (x%3 ==0)
    System.out.print("");
    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 931 ✭✭✭aperture_nuig


    Isn't it just:

    System.out.print(x)


    ?

    make sure you haven't written "x" in the brackets, or it'll just print the character x to the output.


  • Banned (with Prison Access) Posts: 1,435 ✭✭✭areyawell


    import java.util.Scanner ;
    public class CanANumberBeDividedBy3or5
    {
    public static void main(String [] args)
    {
    Scanner input=new Scanner(System.in) ;

    int x,y,z ;

    System.out.print("Enter first integer");
    x=input.nextInt() ;

    System.out.print("Enter second integer");
    y=input.nextInt() ;

    System.out.print("Enter third integer");
    z =input.nextInt() ;


    if (x%3==0 && x%5==0){
    System.out.println(x);

    }
    if (y%3 ==0 && y%5==0){
    System.out.println(y);
    }
    if (z%3 ==0 && z%5==0){
    System.out.println(z);
    }
    }}
    Thinks that what your trying to do


  • Registered Users, Registered Users 2 Posts: 82 ✭✭ted9308


    yep that works.Can you why it's (x) not "x" ?Do all print statements need to be in quotes?


  • Closed Accounts Posts: 1,990 ✭✭✭JustAddWater


    ted9308 wrote: »
    yep that works.Can you why it's (x) not "x" ?Do all print statements need to be in quotes?

    Anything in " " is a string and that's what will be displayed

    x assuming it is a variable will contain a value assigned to it, so printing x will print its value

    Printing "x" will print exactly that... An x

    What's in quotes is what is printed

    You could say
    System.out.print("the value of x is " + x);

    Output:
    the value of x is 5


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    quotes means don't interpret this it's text.
    without the quotes the jvm will print out the value if it's a primitive or call the objects tostring method if it's an object.


  • Advertisement
  • Banned (with Prison Access) Posts: 1,435 ✭✭✭areyawell


    ted9308 wrote: »
    yep that works.Can you why it's (x) not "x" ?Do all print statements need to be in quotes?

    No you don't need quotes in the system.out statements.
    Only need them if you want to right a string in output.
    When declaring a String you put the word in commas, dont need too for numbers
    String name = "John";
    Int number = 445;

    System.out.println("Your name is" +name);
    System.out.println("Your number is" +number);

    This will give the output
    Your name is John
    Your number is 445

    System.out.println(name);
    System.out.println(number);

    This will give the output
    John
    445

    System.out.println("name");
    System.out.println("number");

    This will give the output
    name
    number

    quotes are generally just used for Strings in declaring them. Also you can do this
    System.out.println("your name is" +name+ "and your number is" +number);
    This will give the output
    Your name is John and your number is 445

    Just think that if you are righting a string you need to put that in inverted commas. Nothing else


Advertisement