Hack Forums
[Java] Don't understand Java error code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Don't understand Java error code (/showthread.php?tid=5432727)



Don't understand Java error code - Louis C.K. - Oct 13, 2016

http://prnt.sc/ctkjr5

write a program to calc are and c of a circle, user input c for circum a for area.
I need help, I will even pay for this. skype: eiffel.towerhf


* - ka$h - Oct 13, 2016

Do you mind posting your code


* - Louis C.K. - Oct 13, 2016

(Oct 13, 2016 05:56 PM)Ka$h Wrote:  Do you mind posting your code

http://pastebin.com/Satfvryw
are u proficient in java?


* - ka$h - Oct 13, 2016

Code
public static void main(String[] args) {
        // TODO code application logic here
        double pie = 3.14159;
        String input;
        char letter;
        Scanner key = new Scanner(System.in);
        System.out.println("Enter the radius of the circle: ");
        double radius;
        radius = key.nextDouble();
        System.out.println("Enter C to calculate circumference or enter A to calculate area of the circle: ");
        key.nextLine();
        input = key.nextLine();
        letter = input.charAt(0);
        System.out.println("Just to test >> " + letter);  
    }

notice the extra key.nextLine, I am not exactly sure how to explain the details behind why it happens hopefully this helps you:

http:// stackoverflow. com/questions/14452613/issues-with-nextline


* - shashwat97 - Oct 13, 2016

This works (changed just two things, the way of taking input, as already pointed out above, and added a break in switch case)
Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package circle;
import java.util.Scanner;
/**
*
* @author window user
*/
public class Circle {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        double pie = 3.14159;
        String input;
        char letter;
        Scanner key = new Scanner(System.in);
        System.out.println("Enter the radius of the circle: ");
        double radius = Double.parseDouble(key.nextLine());
        System.out.println("Enter C to calculate circumference or enter A to calculate area of the circle: ");
        input = key.nextLine();
        letter = input.charAt(0);
        switch (letter){
            case 'a':
            case 'A':
                double area = radius*radius*pie;
                System.out.println("Area of the circle is:  " + area);
                break;
            case 'c':
            case 'C':
                double circumference = 2*radius*pie;
                System.out.println("Circumference of the circle is " + circumference);
                break;
            default:
                System.out.println("Enter c to calculate circumference or enter A to calculate are of the circle");
        }
        
    }
    
}

Sample runs-

Code
me@kali:~$ java circle.Circle
Enter the radius of the circle:
10
Enter C to calculate circumference or enter A to calculate area of the circle:
C
Circumference of the circle is 62.8318
me@kali:~$ java circle.Circle
Enter the radius of the circle:
10
Enter C to calculate circumference or enter A to calculate area of the circle:
A
Area of the circle is:  314.159

me@kali:~$ java circle.Circle
Enter the radius of the circle:
10.001
Enter C to calculate circumference or enter A to calculate area of the circle:
A
Area of the circle is:  314.22183494159



* - randomized patterns - Oct 15, 2016

You should use toLowerCase and not have to worry about caps for your switch statement. It is more efficient and your switch statement will look clean.


* - Reece Morgan - Oct 16, 2016

You shouldn't be using nextLine in this scenario. You should be using next.

nextLine will move forward to the next line, and return anything that came before that next line. However the position of the scanner will still be set to the next line, so when you call charAt(0), your asking for the character at index 0 of the line AFTER your input (which is blank).

Edit: I think that explanation is correct, although I could be wrong as I didn't actually test my theory.


* - Stealth™ - Oct 16, 2016

(Oct 16, 2016 09:04 AM)Reece Morgan Wrote:  You shouldn't be using nextLine in this scenario. You should be using next.

nextLine will move forward to the next line, and return anything that came before that next line. However the position of the scanner will still be set to the next line, so when you call charAt(0), your asking for the character at index 0 of the line AFTER your input (which is blank).

Edit: I think that explanation is correct, although I could be wrong as I didn't actually test my theory.

Your explanation is only valid because OP is calling Scanner#nextLine twice for some reason. If he were calling it once, then everything would work :D