0

I'm creating a program that will calculate he cost of traveling. Each segment has a cost, and user is asked to enter the cost for each segment, then enter 3 segment ID's (0-6). The cost of the 3 ID's is added to give a final price.

I need to repeat the program from the beginning if user enters >=1 (see comment at the end), how can I do this? Also, could I improve my program, and how?

import java.util.Scanner;

public class AssignmentArrays{
    public static void main(String[] args){


      Scanner seg0 = new Scanner(System.in);
      Scanner seg1 = new Scanner(System.in);
      Scanner seg2 = new Scanner(System.in);
      Scanner seg3 = new Scanner(System.in);
      Scanner seg4 = new Scanner(System.in);
      Scanner seg5 = 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 */

        while(data[0] == 0){

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

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

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

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

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

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

           /* Path inputs */

           Scanner node1 = new Scanner(System.in);
           Scanner node2 = new Scanner(System.in);
           Scanner node3 = new Scanner(System.in);

           int node1value;
           int node2value;
           int node3value;
           int pathCost;

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

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

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

          /* Path cost calculation */

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

          /* Repeate or end program */

           Scanner end = new Scanner(System.in);

           int userChoice;

           System.out.println("Enter 0 to exit or any other number to evaluate another path:");
              userChoice = end.nextInt();

           if (userChoice == 0){
              System.out.println("The program has ended");
              break;
              }
              else if(userChoice >= 1){
              /* REPEATE ALL OF THE ABOVE HERE */
              }
          }

     }

}
4
  • 1
    1) Why are there 6 scanners? 2) Please use a consistent and logical indent for code blocks. Commented Nov 16, 2012 at 8:13
  • 6 Scanners? 1 is more than enough. Also if you want to repeat the whole program on 1 as input. just enclose the relevant part of code that you want repeated inside a while loop that loops if userChoice is 1 Commented Nov 16, 2012 at 8:17
  • I think I understand. What about the scanners? How am I supposed to do it instead? Commented Nov 16, 2012 at 8:19
  • @Kronos, just initialize one and keep using the same one. Commented Nov 16, 2012 at 8:20

2 Answers 2

1

I would probably make the class more object oriented. However, you could simplify what you have by breaking up your code using methods:

import java.util.Scanner;

public class AssignmentArrays{

static int[] data = new int [6];

public static void getSegmentIDs() {
   ...
}

pubilc static int getUserMenuChoice() {
   ...
}

public static void main(String[] args){

 int exit = false;

 while(!exit) {
   getSegmentIDs();
   choice = getUserMenuChoice();
   if (choice == 0) exit = true;
 } 

}
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have divided the program into 3 methods, getIDs(); , pathCalc(); and the main method. Now I have a problem as the DATA variable is in getIDs(); and I require it in pathCalc. "Cannot find Symbol"
you could make it a static member of the AssignmentArrays class. I've updated my answer to reflect this.
Ok, that works now, however my program begins at pathCalc(); rather than getIDs();. How can I fix this?
0

I would put the whole thing into a do while statement by doing this:

  String answer ="";
  do {
  Scanner seg0 = new Scanner(System.in);
  Scanner seg1 = new Scanner(System.in);
  Scanner seg2 = new Scanner(System.in);
  YOUR CODE CONTINOUS

  /* Repeate or end program */
  System.out.println("would you like to redo? (Y/N)");
  answer = end.next();
  } while(answer.toLowerCase().contains("y"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.