dimanche 10 octobre 2021

Why does my input into scanner give me a different option then intended?

No matter what input I give to scanner for 'menu', it still just chooses the first option. This is a tester class so I've cut out some of the menu inner options to lower reading time. This is also a homework assignment that I'm completing but this tester is really kicking my butt.

At the very bottom of the code is a method called 'test2', its something our teacher added for us to use to showcase different comparison methods. I don't think it has anything to do with this error but it just might help to include still. This is a tester class so i've cut out some of the menu inner options to lower reading time.

I've spent most of today looking at Java 11 documentation and post on here but I haven't been successful in solving this issue. Any and all help would be greatly appreciated.

import java.util.Scanner;
public class TestSortedDoubleList {
    public static <T> void main(String args[]) throws Exception {
        SortedDoubleList<Integer> Tester = new SortedDoubleList<Integer>();
    
        Tester.insert(2); //creating test list
        Tester.insert(20);
        Tester.insert(3);
        Tester.insert(1);
        Tester.insert(2);
        Tester.printList(); //printing for my visual verifications
        
        String input;
        Scanner one = new Scanner(System.in);
        System.out.println("Would you like use a Interger(1) or String(2)?");

        input = one.nextLine();
        int val = test2(input); //this part is working fine so far
        if(val == 1){// integer option 
          System.out.println("Integer picked..."); //menu interface options
          System.out.println("1.   Insert element");
          System.out.println("2.   Print list size");
          System.out.println("3.   Is the list empty?");
          System.out.println("4.   Print first element");
          System.out.println("5.   Print last element");
          System.out.println("6.   Count elements with a particular value");
          System.out.println("7.   Remove first");
          System.out.println("8.   Remove last");
          System.out.println("9.   Remove all elements with a particular value1");
          
          String input2 = one.nextLine();    
          int menu = test2(input2);
          //when I input any number, it still runs through menu == 1  
          if(menu == 1) {
              System.out.println("1");
              System.out.println("What Integer element would you like to insert?:");
              int input3 = Integer.parseInt(one.nextLine());
              Tester.insert(input3);
              Tester.printList();
              return;
          }
          else if(menu == 2) {
              System.out.println("2");
            return;
          }
          if(menu == 3) {
              System.out.println("3");
          }
          if(menu == 4) {
          }
          if(menu == 5) {
          }
          if(menu == 6) {
          }
          if(menu == 7) {
          }
          if(menu == 8) {
          }
          if(menu == 9) {
          }
          else{
            System.out.println("error");
            return;
          }
        }
        else if(val ==2){//string option

public static int test2(String input){ //Have to use this to test the values against eachother
if(input.matches("-?(0|[1-9]\\d*)")){
    return 1;
}
else{
  return 2;
}

Aucun commentaire:

Enregistrer un commentaire