Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Don't understand Java error code

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
[Image: GKQyhij.gif]
Do you mind posting your code
(Oct 13, 2016 05:56 PM)ka$h Wrote:  Do you mind posting your code

http://pastebin.com/Satfvryw
are u proficient in java?
[Image: GKQyhij.gif]
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
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
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.
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.

Drink all the booze, hack all the things.
hackirc.net
Plex. That is all.

(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by AzythumAug 2, 2024 10:19 AM 3 187 Aug 2, 2024 10:19 AM
Last Post: Perlo
[Java] by DimleadOct 13, 2018 10:17 AM 5 399 Oct 13, 2018 10:17 AM
Last Post: SaefkoTeam
[Java] by AutowallNov 24, 2017 09:03 PM 2 238 Nov 24, 2017 09:03 PM
Last Post: Marduno