Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public static void main(String[] args)  {
        double [] Array1 = new double [100];
        try{
            Scanner in = new Scanner(new File ("weights.txt"));
            int count = 0;
            while (in.hasNext())
            {
            String [] stone = in.nextLine().split("");
            for (String temp:stone)
            {
             double KG = (Double.valueOf(temp))* 6.35029318 ;
              Array1 [count] = KG;
              count++ ;
            }
            }
            double [] Array2 = new double [count];
            copyArray (Array1 , Array2 , count);
            in.close();
            in = new Scanner (System.in);


What I have tried:

Java
public static void main(String[] args)  {
        double [] Array1 = new double [100];
        try{
            Scanner in = new Scanner(new File ("weights.txt"));
            int count = 0;
            while (in.hasNext())
            {
            String [] stone = in.nextLine().split("");
            for (String temp:stone)
            {
             double KG = (Double.valueOf(temp))* 6.35029318 ;
              Array1 [count] = KG;
              count++ ;
            }
            }
            double [] Array2 = new double [count];
            copyArray (Array1 , Array2 , count);
            in.close();
            in = new Scanner (System.in);
            
            String choice = " " ;
            DecimalFormat df = new DecimalFormat("0.00");
            while (!choice.equalsIgnoreCase("Q")) {
                System.out.println("menu\np.print the file\r\n"
                        + "D.display sorted list\r\n"
                        + "S.search \r\n"
                        + "Q. Quit ");
                System.out.print("Enter your choice");
                choice = in.nextLine();
                switch(choice.toUpperCase().charAt(0))
                {
                    case 'p' :
                        for(int i=0; i<count;i++){
                            System.out.printf("%.2f",Array2[i]);
                }
                }
                   break;
                   case 'D' :
                   Arrays.sort(Array2);
                   for (int i =0 ; i<count ; i++){
                   System.Out.printf("%.2f", Array2[i]);
            }
            break;
                   case 'S' :
                   System.Out.println("Enter a number ");
                   double number = in.nextDouble();
                   in.nxetLine();
                   boolean tempfound = false ;
                   for (int i=0 ; i <count ; i++){
                   String num = df.format(Array2[i]);
                   if(String.valueOf(number).equalsignoreCase(num)){
                   System.Out.printLn("The given value is found");
                   tempfound = true ;
            }
                   }
            
                
              if(!tempfound)
              System.out.printIn("The given value is not found ");
                 break;
                 case 'Q' :
                 System.out.printIn("End");
                 break;
                 default :
                 System.out.printIn("invalid choice");
            }
              System.out.print("\n");
            } catch (FileNotFoundException ex) {
            Logger.getLogger(Filll.class.getName()).log(Level.SEVERE, null, ex);
        }
    
}

    
   private static void copyArray (double[] Array1 ,double [] Array2 , int count ) {
            for (int i=0 ; i<count ; i++){
                Array2[i] = Array1[i];
            }
          }
Posted
Updated 8-Jan-22 7:25am
v3

Take a look at the braces after your switch statement:
Java
switch(choice.toUpperCase().charAt(0))
{
    case 'p' :
        for(int i=0; i<count;i++){
            System.out.printf("%.2f",Array2[i]);
        }
}
break;
case 'D' :
Clearly, case D is outside the code block following the switch statement, so is "orphaned". This might have been obvious to you if you were to indent your code consistently. Even with the odd indentation shown above, it should be clear that case 'D': etc were outside the scope of the switch block.

If you're calling toUpperCase() on choice, then clearly case 'p' won't ever get selected, so that looks like its a bug, to me.

In future, when asking a question, please actually ask a question. Don't just blindly dump source code and hope someone can figure out from the question title what's going on. In this case something like
I am getting the following error message from the java compiler : some err message with line number. I'm not sure what that means. Can someone please explain it to me?
 
Share this answer
 
v3
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Java
switch (choice.toUpperCase().charAt(0)) {
    case 'p':
        for (int i = 0; i < count; i++) {
            System.out.printf("%.2f", Array2[i]);
        }
} // guilty line
break;
case 'D':
    Arrays.sort(Array2);
    for (int i = 0; i < count; i++) {
        System.Out.printf("%.2f", Array2[i]);
    }
    break;
case 'S':
    System.Out.println("Enter a number ");
    double number = in .nextDouble(); in .nxetLine();
    boolean tempfound = false;
    for (int i = 0; i < count; i++) {
        String num = df.format(Array2[i]);
        if (String.valueOf(number).equalsignoreCase(num)) {
            System.Out.printLn("The given value is found");
            tempfound = true;
        }
    }

Indentation style - Wikipedia[^]
Online Java Formatter[^]
https://codebeautify.org/javaviewer[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900