Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:03:26+00:00 2026-06-14T10:03:26+00:00

I cant seem to get my main method working as it should 100%. It

  • 0

I cant seem to get my main method working as it should 100%. It is good but I need fix a looping problem.

My main goal was to get the program to repeat method pathCalc() once user enters any number >=1 and to end the program when user enters 0. However, when user enters >= 1 to repeat the program, program does repeat, but when it gets to the point to ask user if to repeat or exit again, and user enters 0 to exit, the program repeats method pathCalc() instead of exiting.

How do I get this to work after repeating method, so that the user enters >= 1 to repeat method or 0 to exit?

    import java.util.Scanner;

      public class AssignmentArrays{
         static int[] data = new int [6];

            public static void getIDs(){

               Scanner seg = new Scanner(System.in);

               int[] data = new int [6];
               data[0] = 0;
               data[1] = 0;
               data[2] = 0;
               data[3] = 0;
               data[4] = 0;
               data[5] = 0;

              /* Segment values */



               System.out.println("Enter cost for segment 0:");
               data[0] = seg.nextInt();

               System.out.println("Enter cost for segment 1:");
               data[1] = seg.nextInt();

               System.out.println("Enter cost for segment 2::");
               data[2] = seg.nextInt();

               System.out.println("Enter cost for segment 3:");
               data[3] = seg.nextInt();

               System.out.println("Enter cost for segment 4:");
               data[4] = seg.nextInt();

               System.out.println("Enter cost for segment 5:");
               data[5] = seg.nextInt();

   }

     public static void pathCalc(){     

     /* Path inputs */

     Scanner node1 = new Scanner(System.in);


     int pathCost;

     System.out.println("Enter ID of segment 0 of path:");
     int node1value = node1.nextInt();

     System.out.println("Enter ID of segment 1 of path:");
     int node2value = node1.nextInt();

     System.out.println("Enter ID of segment 2 of path:");
     int node3value = node1.nextInt();

     /* Path cost calculation */

     pathCost = data[node1value] + data[node2value] + data[node3value];
     System.out.println("The cost of the trip is: $" + pathCost);
     }

     public static void main(String[] args){
      getIDs();
      pathCalc();
      System.out.println("Enter 0 to exit or any other number"+
                                        " to evaluate another path:");
         int choice;
         choice = end.nextInt();

         while(choice != 0){
         getIDs();
         pathCalc();
         System.out.println("Enter 0 to exit or any other number"+
                                        " to evaluate another path:");
         choice = end.nextInt();
         }
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:03:27+00:00Added an answer on June 14, 2026 at 10:03 am

    You are reading the choice tow times within the while loop. Move one instance out as below:

        getIDs();
        pathCalc();
        System.out.println("Enter 0 to exit or any other number"+
                                                " to evaluate another path:");
        int choice = end.nextInt();
        while(choice != 0){
             //getIDs();
             pathCalc();
             System.out.println("Enter 0 to exit or any other number"+
                                                " to evaluate another path:");
             choice = end.nextInt();
        }
    

    Also, there is not need of extra flag. you an use choice itself in the condition as mentioned above.

    Your while loop will not start, if user enter 0 in the beginning; and the loop will terminate automatically when user enters 0 afterwards.

    EDIT: Updated program.

    public class AssignmentArrays {
        static int[] data = new int[6];
        static Scanner seg;
    
        public static void getIDs() {
            int[] data = new int[6];
            data[0] = 0;
            data[1] = 0;
            data[2] = 0;
            data[3] = 0;
            data[4] = 0;
            data[5] = 0;
    
            /* Segment values */
    
            System.out.println("Enter cost for segment 0:");
            data[0] = seg.nextInt();
    
            System.out.println("Enter cost for segment 1:");
            data[1] = seg.nextInt();
    
            System.out.println("Enter cost for segment 2::");
            data[2] = seg.nextInt();
    
            System.out.println("Enter cost for segment 3:");
            data[3] = seg.nextInt();
    
            System.out.println("Enter cost for segment 4:");
            data[4] = seg.nextInt();
    
            System.out.println("Enter cost for segment 5:");
            data[5] = seg.nextInt();
    
        }
    
        public static void pathCalc() {
            int pathCost;
    
            System.out.println("Enter ID of segment 0 of path:");
            int node1value = seg.nextInt();
    
            System.out.println("Enter ID of segment 1 of path:");
            int node2value = seg.nextInt();
    
            System.out.println("Enter ID of segment 2 of path:");
            int node3value = seg.nextInt();
    
            /* Path cost calculation */
    
            pathCost = data[node1value] + data[node2value] + data[node3value];
            System.out.println("The cost of the trip is: $" + pathCost);
        }
    
        public static void main(String[] args) {
            seg = new Scanner(System.in);
            getIDs();
            pathCalc();
            System.out.println("Enter 0 to exit or any other number"
                    + " to evaluate another path:");
            int choice;
            choice = seg.nextInt();
    
            while (choice != 0) {
                getIDs();
                pathCalc();
                System.out.println("Enter 0 to exit or any other number"
                        + " to evaluate another path:");
                choice = seg.nextInt();
            }
            seg.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cant seem to get the following to find the 'skull' button when the 'heart'
I cant seem to get VB to focus on a textbox correctly at the
I cant seem to get the Facebook like button to include my image, checked
Revising for php and cant seem to get this to print the values out
Hope someone can help i cant seem to get my head around this, i
Ok, after battling for the entire afternoon, I cant seem to get the right
I cant seem to find out how to get my ListView (OnListItemClick), to open
Can't seem to get this jquery gallery working. I've been able to get the
Can't seem to get my font working in firefox, seems to work fine in
I'm using an stl unordered_map, and I can't seem to get the count method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.