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 lines = new ArrayList<>(); while(rsp2.hasNextLine()){ lines.add(rsp2.nextLine()); } while(filewords.hasNext()){ String fwords = filewords.next(); int count = 0; double average = 0;

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 linesp3 = new ArrayList<>(); while(rsp3.hasNextLine()){ linesp3.add(rsp3.nextLine()); } while(rsp3.hasNextLine()){ linesp3.add(rsp3.nextLine()); } while(filewordsp3.hasNext()){ String fwords = filewordsp3.next(); int count = 0; double average = 0; int smallest = Integer.MAX_VALUE; //for loop for array for (String line : linesp3) { //if the word is found in the file if(line.contains(fwords)){ //storing the count count += 1;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!