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 4 of 4

Thread: Arrays (address book)

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Arrays (address book)

    I am trying to use an if statement in a do while. I have placed a menu inside the do while and then want to use the if statment to deal with the four menu options. The first menu option is to enter Name: Age: and Hometown:. The problem I have is that I want to take the input from the user but the if statement is only printing out the Name: etc and not allowing for user input. I feel like I can't move forward so any help is really really appreciated. As i'm learning at mo and I am really stuck.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,309
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: Arrays (address book)

    Please post your code that shows the problem. Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrays (address book)

    Hiya I dont know what code wrapping is, sorry I'm really new to this. Appreciate the help.
    Here is my code

    import java.util.Scanner;
    import java.util.ArrayList;

    public class addressbook1
    {
    public static void main (String args[])
    {
    Scanner scan = new Scanner(System.in);
    String [] Name= new String[100];
    int [] Age = new int[100];
    String [] Hometown =new String[100];
    int index = 0;
    int option = 0 ;
    String search = "";
    boolean end = false;


    do {
    index ++;
    System.out.println("======MENU======");
    System.out.println("1: NEW Record Add. ");
    System.out.println ("2: SEARCH Record by Name. ");
    System.out.println("3: PRINT All Records. ");
    System.out.println("4: EXIT ");
    System.out.println("What is your option (1-4)? ");
    option = scan.nextInt();


    if (option == 1)
    { System.out.println("You selected option 1");
    System.out.println("--- Adding a Record --- ");

    System.out.println("Name: ");
    Name[index] = scan.nextLine();

    System.out.println("Age " + index + ": ");
    Age [index] = scan.nextInt();

    System.out.println("Hometown " + index + ": ");
    Hometown [index] = scan.nextLine();

    }
    else if(option == 2)
    {
    System.out.println("You selected to search for a Record. ");
    }
    else if (option == 3)
    {
    System.out.println("You chose Print ALL Records ");
    }
    else if (option == 4)
    {
    System.out.println("EXIT");
    }
    else
    {
    end = true;
    System.out.println("Program has now ended!");
    }



    }while (end == false);

    }
    }

    --- Update ---

    Still working on the first part as you can see I cannot get the program to take input for each variable and print it to screen

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,309
    Thanks
    54
    Thanked 2,635 Times in 2,585 Posts

    Default Re: Arrays (address book)

    what code wrapping is,
    Use the values in [] shown in this:
    Please edit your post and wrap your code with code tags:

    [code] <<<<<<<<< BEFORE THE CODE
    **YOUR CODE GOES HERE**
    [/code] <<<<<<<<<<<<< AFTER THE CODE

    to get highlighting and preserve formatting.
    One goes before the code and one after.

    Also post the contents of the console from when the program is compiled and executed to show what the problem is.
    Be sure to add comments in the posted text to show where the unexpected values are.

    Your problem is with the way the Scanner class's methods work.
    Here is a description: https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Address Book program in Java
    By DaveNAchill in forum Paid Java Projects
    Replies: 3
    Last Post: January 6th, 2012, 04:03 AM
  2. Address Book Program in java
    By DaveNAchill in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:25 AM
  3. Address Book Program Issues
    By Gamb1t in forum What's Wrong With My Code?
    Replies: 208
    Last Post: August 25th, 2011, 08:43 PM
  4. Address Book Help
    By clevel211 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 21st, 2010, 09:42 PM
  5. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 03:08 AM

Tags for this Thread