3 Replies - 632 Views - Last Post: 07 July 2015 - 02:53 AM Rate Topic: -----

#1 simonang92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 15

beginner question ~ struggling

Posted 07 July 2015 - 12:57 AM

Sorry, I'm new to Java (but I've learn C++ before so Java seems to be familiar)
I facing this problem as shown in Figure below
I want to divide the two values but it seem something goes wrong (without error)
but if I change the sign to "+" or "*" , they seem to perform perfectly.
Can anyone tells me whats wrong with the "/"?
By the way, there's a light bulb beside it saying that Resource leak, may I know what's wrong as well?
One more question, I've tried another method using

public class PrintSquare {
public static void main(String[] args) {
int userInput; 
int square; 
System.out.print("Please type a number: ");
userInput = TextIO.getlnInt();
square = userInput * userInput;
System.out.print("The square of that number is ");
System.out.println(square);
}
}


Everything seems fine except for the line userInput = TextIO.getlnInt();
The error says that TextIO can't be resolved.
May I know what's problem with it as well?

This post has been edited by ChrisNt: 07 July 2015 - 03:39 AM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: beginner question ~ struggling

#2 simonang92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 15

Re: beginner question ~ struggling

Posted 07 July 2015 - 01:05 AM

Posted Image

Sorry I have problem uploading photos so I guess I have to copy paste all code

import java.util.Scanner;
public class PrintSquare {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter first number: ");
      int firstNumber = s.nextInt();
      System.out.print("Enter second number: ");
      int secondNumber = s.nextInt();
      int sum = firstNumber * firstNumber;
      int sum2 = secondNumber *secondNumber;
      int sum3 = sum/sum2;
      System.out.println("The result is " + sum3);
   }
} 


Output:
Enter first number: 5
Enter second number: 9
The result is 0

Sorry I forget to mention that I'm using Eclipse IDE

This post has been edited by ChrisNt: 07 July 2015 - 03:40 AM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#3 simonang92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 07-July 15

Re: beginner question ~ struggling

Posted 07 July 2015 - 01:54 AM

Sorry Case Closed Found the reason behind already
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: beginner question ~ struggling

Posted 07 July 2015 - 02:53 AM

In case it isn't obvious, you're doing integer division, which will lose anything after the decimal point. You therefore need to make one of the operands a floating point value.

Also, when posting code here, please put it within code tags - see the text in the box where you type your post, as it tells you what to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1