Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: help the noob pls

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default help the noob pls

    hi guys, i just started learning Java few days ago, so as u can guess i'm struggling. i wrote a small code but for some reason it aint working:

    import java.util.Scanner;
    public class Noob{
    static Scanner userInput= new Scanner(System.in);
    public static void main(String[] args){
    System.out.print("whats the shape? ");
    String a= userInput.nextLine();
    if(a.equals("triangle")){
    System.out.print("what's the length? ");
    int x= userInput.nextInt();
    System.out.print("what's the width? ");
    int y= userInput.nextInt();
    System.out.print("what u wanna know? ");
    String b= userInput.nextLine();
    if(b.equals("territory")){
    triTerr(x,y);
    }}}
    public static void triTerr(int x, int y){
    x= x*y/2;
    System.out.println("territory of triangle is "+ x);
    }}
    so as u can see it supposed to ask u the shape (triangle), then the length and the width, what u wanna know(territory) and it supposed to calculate and give u the territory of triangle. but for some reason it stuck and doesn't let u answer for "what u wanna know?"... what am i doing wrong??
    thnx for any help guys, it supposed to be a realy easy one.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: help the noob pls

    EDIT: I realized that you are leaving the newline character in the buffer.

    To fix it, put a userInput.nextLine(); after "what you wanna know?"
    Last edited by camel-man; October 8th, 2014 at 08:23 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help the noob pls

    Quote Originally Posted by camel-man View Post
    EDIT: I realized that you are leaving the newline character in the buffer.

    To fix it, put a userInput.nextLine(); after "what you wanna know?"
    Sorry i don't get it....can you type it?

  4. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help the noob pls

    System.out.print("what u wanna know? ");
    userInput.nextLine();
    String b = userInput.nextLine();

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help the noob pls

    Quote Originally Posted by Synbios View Post
    System.out.print("what u wanna know? ");
    userInput.nextLine();
    String b = userInput.nextLine();
    sorry i missed ur answer, thnx. but i i didn't have to use "userInput.nextLine();" when dealing with String a, int x and int y, why do i have to use it with String b???
    Last edited by sash25; October 9th, 2014 at 03:39 AM.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help the noob pls

    @sash25: Please post your code using code or highlight tags as explained near the top of this topic.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help the noob pls

    import java.util.Scanner;
    public class Noob{
    static Scanner userInput= new Scanner(System.in);	
    public static void main(String[] args){
    System.out.print("whats the shape? ");
    String a= userInput.nextLine();
    if(a.equals("triangle")){
    System.out.print("what's the length? ");
    int x= userInput.nextInt();
    System.out.print("what's the width? ");
    int y= userInput.nextInt();
    System.out.print("what u wanna know? ");
    String b= userInput.nextLine();
    if(b.equals("territory")){
    triTerr(x,y);
    }}}
    public static void triTerr(int x, int y){
    x= x*y/2;
    System.out.println("territory of triangle is "+ x);
    }}

Similar Threads

  1. hi everyone Can someone please help Warning NOOB here
    By Jploganlant in forum Member Introductions
    Replies: 1
    Last Post: April 12th, 2014, 05:00 PM
  2. I'm a noob, noob questions.
    By Mickyy in forum Java Theory & Questions
    Replies: 6
    Last Post: October 25th, 2013, 07:27 AM
  3. complete noob, direction needed pls.
    By delamer in forum Java Theory & Questions
    Replies: 3
    Last Post: October 13th, 2012, 01:54 PM
  4. Most noob question ever.
    By SkyAphid in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2011, 08:14 AM
  5. Noob needs help!
    By kram in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2011, 05:03 AM