In: Computer Science
Is there anyway to solve below problem in Java?
Wherever there is int it gives an error! It's not only line 20! it give an runtime error everywhere get int after string! or even before read string!
cities[i] = sc.next(); //This way does not work!!!!
==============
input
San Francisco Las Vegas 8 San Diego Los Angeles San Francisco San Jose Sacramento Santa Barbara Las Vegas Pheonix 8 19 0 1 0 3 1 0 1 2 1 4 1 3 2 1 2 5 2 6 2 7 3 7 3 0 3 1 4 7 4 1 5 2 6 2 7 2 7 3
==============
Error:
==============
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Solution.main(myFuncs.java:20)
=======================
==============
import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class myFuncs{
public static void main(String arg[]) {
Scanner sc = new
Scanner(System.in);
String s1, s2;
s1 = sc.nextLine();
//first city
s2 = sc.nextLine();
//second city
int n = sc.nextInt();
//total number of cities
String cities[] = new
String[n];
for (int i = 0; i <
n; i++) {
cities[i] = sc.nextLine(); //getting each city and storing
them
}
n = sc.nextInt();
int e =
sc.nextInt();
int adj[][] = new
int[n][e];
for (int i = 0; i <
n; i++)
for (int j = 0; j < e; j++)
adj[i][j] = 0;
for (int i = 0; i <
e; i++) {
int c1 = sc.nextInt();
int c2 = sc.nextInt();
adj[c1][c2] = 1;
adj[c2][c1] = 1;
}
int index1 = -1, index2 = -1;
for (int i = 0; i
< n; i++) {
if (cities[i].equals(s1)) index1 = i; //storing the index of first
city
if (cities[i].equals(s2)) index2 = i; //storing the index of second
city
}
adj[index1][index2] =
0;
adj[index2][index1] =
0;
System.out.println(cities[index1] + " " + index1);
System.out.println(cities[index2] + " " + index2);
if (bfs(adj, index1,
index2, n)) System.out.println("true");
else
System.out.println("false");
}
public static boolean bfs(int adj[][], int source, int dest, int V) {
boolean visited[] = new boolean[V];
LinkedList < Integer > queue = new LinkedList < Integer > ();
visited[source] =
true;
//visited[dest]=true;
queue.add(source);
while (queue.size()
!= 0) {
int s = queue.poll();
if (queue.size() != 0)
queue.remove();
System.out.print(s + " ");
for (int i = s; i < V; i++) {
if (adj[s][i] == 1 && !visited[i]) {
visited[i] = true;
queue.add(i);
if (i == dest) return true;
}
}
}
return false;
}
}
In case of any query, do comment. Please rate answer as well. Thanks
There are two ways:
First Way: Actually you are using int n = sc.nextInt() in line 15 and pressing enter so it is taking first item of cities array as blank. So either you press only space , it will work for you. So when you use nextInt() you have to just press space after that.
I have only provided the space after third input then your old program also worked. Please see the output.
Second way: or modify the line 20 and other lines where you are taking integer input as below, then if you press enter it will work for you:
int n = Integer.valueOf(sc.nextLine());
Output: