0

-So in case 2, i'm trying to have the user to input elements (N,Y,P,D,G) only with specific Array location. I used switch inside a switch, but how do i re-loop if he enters wrong elements?

-in case 3, i'm trying to print the whole 2D array, but the original array is filled with the element G.

-in case 4, i'm trying to change the map view to the symbols.

Just found this as a practice, trying to learn, to some point, beginner complex programming, so any help is appreciated and any criticism is highly appreciated because i want to have a good basic knowledge of java :)

import java.util.Scanner;

public class Studyone {

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    int row;
    int col;
    int row1; //for case 2, so it would allow the user to input the element in the request array
    int col1;
    System.out.println("Enter the row: ");// knowing the row and the col of the 2D array to base the rest on.
    row = input.nextInt();
    System.out.println("Enter the col: ");
    col = input.nextInt();
    String sentence;
    String sentence2;
    String sentence3;
    String tempelement;
    String N;
    String Y;
    String G;
    String P;
    String D;
    int choice;
    System.out.println("The menu\n1)Enter the Elements in order\n2)Enter the the Col and row number spec.\n3)Show the Orig. map as is\n4)Show symbolic map.");
    choice = input.nextInt();
    System.out.println(choice);
    String [][] map = new String[row][col];
    switch (choice)
    {
        case 1:
            sentence3=input.nextLine();  // for some reason it auto enters the first map [0][0] as null, this line allows the user to input the map[0][0]
            map[0][0]=sentence3;
            for (int i = 0; i < map.length; i++) 
            {
                for (int j = 0;j < map[i].length; j++) 
                {
                    System.out.println("Enter the element");
                    sentence2 = input.nextLine();
                    map[i][j]=sentence2;
                }
                System.out.println(map[1][1]);
                System.out.println(choice);

            }
            break;
        case 2:
            System.out.println("Enter the row");
            row1 = input.nextInt();
            System.out.println("Enter the col");
            col1 = input.nextInt();
            System.out.println("Enter the element you want to enter");
            tempelement = input.nextLine();
            switch (tempelement)
            {
                case "Y":
                    map[row1][col1] = tempelement;
                case "N":
                    map[row1][col1] = tempelement;
                case "P":
                    map[row1][col1] = tempelement;
                case "D":
                    map[row1][col1] = tempelement;
                case "G":
                    map[row1][col1] = tempelement;
            }

            System.out.println(tempelement);  // test,does not let me enter any temp elements, lets it empty in the input without letting the use enter anything. Figure out error
            System.out.println(choice); // test, prints the right choice, Correct.
            break;
        case 3:
            for (int i=0;i > map.length;i++)
            {
                for (int j=0; j > map[i].length; j++)
                {
                    map[i][j] = N;
                    // print statment, need to find how to do.
                }
            }**strong text**
        break;
        case 4:
            // having symbols instead of the letters, N=* , P= ~ , Y=. , G= #, D=@
            //
    }
}
}
Ankosh
  • 83
  • 1
  • 9

3 Answers3

1

Case 2:

I don't know why you're using a switch statement within to check if the cases are either N,Y,P,D or G. In order to check for correct inputs, you could use a while loop and break when the correct input is placed.

 System.out.println("Enter the row");
 row1 = input.nextInt();
 System.out.println("Enter the col");
 col1 = input.nextInt();

 while(true){
 System.out.println("Enter the element you want to enter");
 tempelement = input.nextLine();
 if(tempelement.equals("N") || tempelement.equals("Y") || tempelement.equals("P") || tempelement.equals("D") || tempelement.equals("G")){
     map[row1][col1] = tempelement;
     break;
  }
 }

Case 3:

To print a 2D array, you need a nested for loop. It requires 2 for loops.

for(int i=0; i<map.length; i++){
    for(int j=0; j<map[i].length; j++){
          System.out.print(map[i][j]);
     }
      System.out.print("\n");  // this is for spacing after a row of element prints
 }

Case 4:

This is the same as Case 3, just within the inner for-loop, you would have an if-else statement block or switch block to print out a symbol instead of a map. For example,

...
    for(int j=0; j<map[i].length; j++){
       switch(map[i][j]){
              case "N": System.out.print("Symbol"); break;
              case "P": ... code
             ... code
        }
    }
  • I tried it, it worked and i just looked it up, but i have a question. When i used it, it printed "Enter the element you want to enter" 3 times not once. – Ankosh Sep 24 '14 at 05:20
  • Never mind, figured it out. Thanks Man! really helped me a discovered some other ways in the process :) – Ankosh Sep 24 '14 at 05:23
  • @Ankosh I'm glad I could have been of help :) If you need help with anything else let me know. – Michael Halper Sep 24 '14 at 19:04
1

sentence3=input.nextLine(); // for some reason it auto enters the first map [0][0] as null, this line allows the user to input the map[0][0]

Use input.next() instead of nextLine().

Scanner.nextLine() as described in official document is:

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

After nextInt() is called it isn't properly terminating the allocated line of memory. So when nextLine() is called first time it is actually terminating the previous line that actually had value in it -- entered via nextInt() rather than taking in a new String value. That's why sentence3 is getting null value.

So in case 2, i'm trying to have the user to input elements (N,Y,P,D,G) only with specific Array location. I used switch inside a switch, but how do i re-loop if he enters wrong elements?

You can try in this way:

    boolean bVaildInput = true;
    do{
        System.out.println("Enter the element you want to enter");
        tempelement = input.next();
    switch (tempelement)
        {
        case "Y":
            map[row1][col1] = tempelement;
            break;
        case "N":
            map[row1][col1] = tempelement;
            break;
        case "P":
            map[row1][col1] = tempelement;
            break;
        case "D":
            map[row1][col1] = tempelement;
            break;
        case "G":
            map[row1][col1] = tempelement;
            break;
        default :
            System.out.println("Invalid Input");
            bValidInput = false;
        }
   }while(!bVaildInput);

in case 3, i'm trying to print the whole 2D array, but the original array is filled with the element G.

        for (int i=0;i < map.length;i++)
        {
            for (int j=0; j < map[i].length; j++)
            {
                System.out.print(map[i][j]+"\t");
            }
            System.out.print("\n");
        }

in case 4, i'm trying to change the map view to the symbols.

Show some effort.

Vishal K
  • 12,676
  • 1
  • 22
  • 37
0

Since you are using this as practice, I'm not going to answer your problems but provide some insight into what it is that is going on and some links to look into:

Case 2: You don't really need a second switch here. Look at the code for each case in the switch, it is exactly the same. What you want to use is a while loop and test that the input is valid. That way if the user enters invalid input it will ask them to try again. See Example 3 in this post

Case 3: There are plenty of examples out there of how to print 2D arrays - it depends on how you want it to look. I Suggest looking at this example

Case 4: This really is just an extension of Case 3 except rather then printing out the stored Input letter, print out the symbol. This is where a second switch statement maybe useful.

Community
  • 1
  • 1
Java Devil
  • 9,835
  • 7
  • 30
  • 44