'How do I validate an integer input number to be only from 1 to 8 when "enter an option" or option is a String?

while (option < 1 || option > 8) {

    System.out.print("Invalid input! Enter an option>");

    option = scanner.nextLine();

}

This is the error I'm getting when I validate:

error: bad operand types for binary operator '<'

while (option < 1 || option > 8) {

               ^

       first type:  String

       second type: int


error: bad operand types for binary operator '>'

 while (option < 1 || option > 8) {

                           ^

  first type:  String

  second type: int

2 errors

BELOW IS THE CODE:


import java.util.Scanner;

public class Demo {



public static void main(String[] args) {



Playlist play = new Playlist();

String option = "";

Scanner scanner = new Scanner(System.in);



String songName, albumName, artistName;

int length;



Song currentSong = null;



menu();

System.out.print("Enter an option> ");

option = scanner.nextLine();



while (!option.equals("8")){



switch (option){



case "1":

System.out.print("What is the name of the Song> ");

songName = scanner.nextLine();

System.out.print("What is the name of the Album> ");

albumName = scanner.nextLine();

System.out.print("What is the name of the Artist> ");

artistName = scanner.nextLine();

System.out.print("What is the length of the song in seconds> ");

length = scanner.nextInt();

play.insertSong(new Song(songName, artistName, albumName, length));

System.out.println( "\n---------------" + songName + " has been added to the playlist!" + "---------------\n") ;

break;



case "3":

System.out.println("\n\t\tPRINTING PLAYIST\n------------------------------------");

play.printList();

break;

case "4":

if(currentSong == null){



if(play.getHead() != null){



currentSong = play.getHead();

System.out.println(currentSong.getName() + " " + currentSong.getArtist());



}else{



System.out.println("\nEmpty Playlist");

}



System.out.println();



}else{





System.out.println(currentSong.getName() + " " + currentSong.getArtist());

}



break;



case "6":



if(currentSong != null){





if(!play.comapreSongObj(play.getTail(), currentSong)){





currentSong = currentSong.getNext();

}else{





currentSong = play.getHead();

}



}

break;



case "7":



if(currentSong != null){





if(!play.comapreSongObj(play.getHead(), currentSong)){





currentSong = currentSong.getPrevious();

}else{





currentSong = play.getTail();

}



}

break;





default:

System.out.println("ERROR");

break;



}



menu();

//System.out.println();

System.out.print("Enter an option> ");

option = scanner.nextLine();



}



}



private static void menu(){



System.out.println("1 - Add a song to playlist\n2- Insert a New Song after the Song that is currently playing" +

"\n3 - Print Contents of the Playlist\n4 - Display the current song\n" +

"5 - Remove the current song\n6 - Skip to next song\n7 - Return to previous\n" +

"8 - Exit\n");

}

}


Solution 1:[1]

Currently, your variable option is a String, meaning it can store letters and other characters (including numbers). The rest of your code also treats option as a String, so I'm assuming that's what you would like to go with.

In order to find the number inside a String, you can use the built-in Java method Integer.parseInt(yourString), which returns the number value inside the yourString variable. For example, if String s was "123", the method call Integer.parseInt(s) would return 123 (notice there's no quotation symbols, which means it isn't a String).

Here's how you could implement it in your code:

while (Integer.parseInt(option) < 1 || Integer.parseInt(option) > 8)
{
    System.out.print("Invalid input! Enter an option> ");

    option = scanner.nextLine();
}

Keep in mind that this will cause an error if the user enters a value that contains any non-numeric characters, such as "ab123".

Documentation for Integer.parseInt(yourString)

Solution 2:[2]

Change option to be int and parse an int from the line:

int option = 0;

while (option < 1 || option > 8) {
    System.out.print("Enter an option 1-8:");
    option = Integer.parseInt(scanner.nextLine());
}

Solution 3:[3]

Change var option to int and use nextInt() instead of nextLine() in your scans for that variable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 Bohemian
Solution 3 nikosidij