Question: The following is for java. Hi, I am a beginner at coding java and I'm having trouble with my code repeating until the user stops.
The following is for java.
Hi, I am a beginner at coding java and I'm having trouble with my code repeating until the user stops. My code example is posted below. The do-while loop and the switch cases do not work. Can I get help to make the code repeat until the number 5 is entered or a number other than 1-5 is entered(so they can enter the correct choices)? Currently, it only runs once. Thanks
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; import java.util.*; import java.util.InputMismatchException;
public class wrs{ public static void main(String[] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); Scanner Scanner = new Scanner(System.in); File reviewFile = new File("movieReviews.txt");
Scanner scanner = new Scanner(reviewFile); int rs; String rt; String word; int wordAmount = 0; double score = 0; double wordavg = 0; int countword = 0; System.out.println("what would you like to do?"); System.out.println("1"); System.out.println("2: "); System.out.println("3:"); System.out.println("4"); System.out.println("5"); System.out.println(""); String userval = Scanner.next();
boolean valid = false ;
do{ valid = false; switch(userval){ case "1":
System.out.println("Enter a word."); word = keyboard.nextLine(); while(scanner.hasNext()) {
rs = scanner.nextInt();
rt = scanner.nextLine();
if(rt.contains(word)) { wordAmount++; score = (score + rs); } } double wordcount = wordAmount; score = score/wordcount; System.out.println(word + " appears " + wordAmount + " times."); System.out.println("Average Score : "+score); break;
case "2": System.out.print("Enter name:"); String filename = keyboard.nextLine(); Scanner filewords = new Scanner(new File(filename)); Scanner rsp2 = new Scanner(new File("ms.txt")); ArrayList
for (String line : lines) {
if(line.contains(fwords)){
count += 1;
average += Integer.parseInt(line.trim().substring(0,1)); } } countword++; average = average/count; wordavg = (wordavg + average);
} double sentiment; sentiment = wordavg/countword; System.out.println(sentiment); if(sentiment<=1.99){ System.out.println(" "+filename + " "); } else{ System.out.println(""+filename + " "); } break; case "3": System.out.print("Enter textfile :"); String filenamep3 = keyboard.nextLine(); Scanner filewordsp3 = new Scanner(new File(filenamep3)); Scanner rsp3 = new Scanner(new File("ms.txt")); ArrayList
average += Integer.parseInt(line.trim().substring(0,1)); } } average = average/count; wordavg = (wordavg + average);
System.out.println(fwords+": "+average); } break;
case "4": System.out.println(" ec"); break;
case "5": valid = true; break;
default: System.out.println("error"); valid=true; break; } } while(valid); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
