This is my first code with java .when I tested it on Ideone. It showed:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
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 Hw2_p4.main(Main.java:22)
I searched for answers but I didn't get the problem cause or how to fix it.
the code runs on eclipse normally
Here is the code
import java.util.Scanner;
class Hw2_p4 {
static void swap(String[] A, int a, int b) {
String temp = A[a];
A[a] = A[b];
A[b] = temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
int i, j, k, l, counter = 0;
String[] name = new String[16];
String[][] notalong = new String[120][2];
String[] temp = new String[120];
boolean[][] A = new boolean[120][2];
for (i = 0; i < n; i++) {
name[i] = sc.next();
}
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
notalong[i][j] = sc.next();
}
}
int flag = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
flag = 0;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[i][j] == false && A[k][l] == false) {
A[k][l] = true;
flag = 1;
}
}
}
if (flag == 1) {
A[i][j] = true;
counter++;
} else if (flag == 0 && A[i][0] == false && A[i][1] == false) {
A[i][j] = true;
counter++;
}
}
}
System.out.println(n - counter);
int x = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < 2; j++) {
if (A[i][j] == false) {
temp[x++] = notalong[i][j];
A[i][j] = true;
for (k = i + 1; k < m; k++) {
for (l = 0; l < 2; l++) {
if (notalong[i][j].compareToIgnoreCase(notalong[k][l]) == 0 && A[k][l] == false) {
A[k][l] = true;
}
}
}
}
}
}
//compare not along with names
int found = 1;
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < m; j++) {
for (k = 0; k < 2; k++) {
if (name[i].compareToIgnoreCase(notalong[j][k]) == 0) {
found = 1;
}
}
}
if (found == 0) {
temp[x++] = name[i];
}
}
//sorting lexicographically
boolean swapp = true;
for (i = 0; i < x && swapp; i++) {
swapp = false;
for (j = 0; j < x - i - 1; j++) {
if (temp[j].compareToIgnoreCase(temp[j + 1]) > 0) {
swap(temp, j, j + 1);
swapp = true;
}
}
}
for (i = 0; i < x; i++) {
System.out.println(temp[i]);
}
}
}
Ideone is not interactive. You have to click on Specify input and enter all of your input in there before you run the application. What you are seeing is an exception because System.in has an "end of file" status.
You ought to protect sc.next() with cs.hasNext():
while (!sc.hasNext()) {
Thread.sleep(100);
}
int n = sc.nextInt();
However since you read the input in many location throughout your code try using Scanner.nextLine() preferably printing to the console, what is the input you are expecting next. nextLine() will read all the input until the user presses ENTER, which is a nicer 'user journey' than simply constantly reading keyboard and passing it to System.in.
System.out.print("Please enter your favourite number : "); //NB print and space is last
String input = sc.nextLine();
int favNumber = Double.parseDouble(input);
the way that Ideone gives input to your code is different from the way your IDE(or you)gives input.
so you should probably figure out how does Ideone gives input and then fix your code to that way of inputs.
Related
import java.util.Scanner;
public class Note {
public static void main(String[] args) {
String etudiants[][] = new String[1][4];
Scanner saisie = new Scanner(System.in);
for (int i = 0; i < 1; i++) {
System.out.print("\n\nEtudiant BTI00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tCode de l'etudiant : ");
} else if (j == 1) {
System.out.print("\n\tNom etudiant : ");
} else if (j == 2) {
System.out.print("\n\tNote Maths : ");
} else if (j == 3) {
System.out.print("\n\tNote Francais : ");
} else {
System.out.print("\n\tChamps inexistant!");
}
etudiants[i][j] = saisie.nextLine();
}
}
System.out.print("\n\tEtudiants Enregistres : \n\n");
// System.out.print("\tCode\tNom\t\tMaths\tFrancais\n\n");
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + etudiants[i][j] + " ");
}
}
System.out.println();
System.out.print("\n\tEntrez code etudiant : ");
String recherche = saisie.nextLine();
boolean trouve = false;
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
System.out.print("\n\tCode etudiant correct!");
String math = etudiants[i][2];
String francais = etudiants[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(francais);
double francaisConv = f.doubleValue();
double moyenne = (mathConv + francaisConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + moyenne);
System.out.print("\n\tEtudiant : " + etudiants[i][j]);
if (moyenne <= 40) {
System.out.print("\n\tEchec!");
} else if (moyenne > 40 && moyenne < 70) {
System.out.print("\n\tReprise!");
} else {
System.out.print("\n\tSucces!");
}
} if (!trouve) {
System.out.print("\n\tCode etudiant incorrect!");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
The indexing variable (j) of the last inner for-loop (for (int j = 0; j < 4; j++) {) is never used inside the loop, so you actually do not need that loop at all. General styling issues of your sample aside, you should rewrite the last loop like this:
for (int i = 0; i < 1; i++) {
// throw away this
//for (int j = 0; j < 4; j++) {
if (recherche.equals(etudiants[i][0])) {
trouve = true;
// ... rest of the code like you currently have it
// You probably do not need this line too,
// because you have almost the same in your second loop
//System.out.print("\n\tEtudiant : " + etudiants[i][j]);
}
}
import java.util.Scanner;
public class Grade{
public static void main(String[] args) {
String students[][] = new String[2][4];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
System.out.print("\n\tRegistered Students : \n\n");
System.out.print("\tCODE\tFULL NAME\tMATHS\tFRENCH\n\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + students[i][j] + " ");
}
System.out.println();
}
//Ask for student code.
System.out.print("\n\tStudent Code : ");
String search= input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
// found= true;
if (search.equals(students[i][0])) {
found = true;
System.out.print("\n\tStudent Code Found!\n");
String math = students[i][2];
String french = students[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(french);
double frenchConv = f.doubleValue();
double average = (mathConv + frenchConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + average + "\n");
if (average <= 40) {
System.out.print("\n\tFailure!\n");
} else if (average > 40 && average < 70) {
System.out.print("\n\tReprisal!\n");
} else {
System.out.print("\n\tSuccess!\n");
}
}
else if (!search.equals(students[i][0])) {
found = false;
System.out.print("\n\tCode incorrect!\n");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
When you iterate for the search you don't need the second loop to iterate over the attributes, because you don't use it
System.out.print("\n\tStudent Code : ");
String search = input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 4; j++) { //you don't need this loop
Change this:
for (int j = 0; j < 4; j++)
to this:
for (int j = 0; j < 1; j++)
as we want to run that loop only once.
Alternatively, you can remove the j loop altogether.
it's recommended you use the length argument for those arrays, that way if you ever change the size, the loop won't break, in this case you can just get the first element of the array, and then iterate through that. So intead of
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
you can say
System.out.print("\n\nStudent 001" + "\n\n");
for (int j = 0; j < students[0].length; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[0][j] = input.nextLine();
}
This is the work that i done so far:I have to print diamond pattern which always starts with uppercase from string, which repeats,but not always starts from the beginning.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
userInput = Character.toUpperCase(userInput.charAt(0)) + userInput.substring(1);
int i;
int j;
if (userInput.length() % 2 != 0) {
for(i = 1; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
} else {
for(i = 2; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
}
}
For example my input is "Peter".
So my output is:
P
Pet
Peter
Pet
P
but it must be:
P
Ete
Rpete
Rpe
T
I dont know what to change to make this work
Here's a shorter version of your code:
public static void main(String[] args) {
String userInput = "Peter";
int length = userInput.length();
int m, j, i, n = 0;
for (m = length % 2 > 0 ? 1 : 2; m < length * 2; m += 2) {
i = m < length ? m : length * 2 - m;
for (j = 0; j < length - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
char c = userInput.charAt(n++ % length);
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
}
System.out.println("");
}
}
You need some few changes:
Declare int n=0; after int j;
Always print userInput.charAt(n++ % userInput.length()) instead of charAt(j)
In order to get only the first character in line in uppercase:
char c = userInput.charAt(n++ % userInput.length());
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
Check the modulo operator.
With these changes, you'll get this output:
P
Ete
Rpete
Rpe
T
Given the fact that the input itself gets printed in a cylic manner, we can make use out of it. My proposal would be to concatenate the input string and print out the substrings which are determined by the structure of the diamond pattern.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
String concatenated = userInput;
// build up the index array
int i, cumSum = 0;
ArrayList<Integer> helperIndex = new ArrayList<>();
for(i = 1; i < userInput.length(); i += 2) {
helperIndex.add(i);
cumSum += i;
}
for(i = userInput.length(); i > 0; i -= 2) {
helperIndex.add(i);
cumSum += i;
}
int numOfWordRepitition = cumSum / userInput.length() ;
for (i = 0; i < numOfWordRepitition; i++){
concatenated += userInput;
}
// print out diamond
String substr;
int prev = helperIndex.get(0);
int next = helperIndex.get(0);
substr = concatenated.substring(0 , helperIndex.get(0));
System.out.println(Character.toUpperCase(substr.charAt(0)) + substr.substring(1));
for(i = 1; i < userInput.length(); i++){
next += helperIndex.get(i);
substr = concatenated.substring(prev , next);
substr = Character.toUpperCase(substr.charAt(0)) + substr.substring(1);
System.out.println(substr);
prev = next;
}
}
Why is my code returning this error? I've made any mistake? I can not see my error in this code. Can anybody help me?
The code is bellow:
public static void minMax(int m[][])
{
int menor = 0;
int maior = 0;
int i = 0;
int j = 0;
int posicao = 0;
for(i = 0; i < 4;i++)
{
for(j = 0; j < 5; j++)
{
if((i == 0) && (j == 0))
{
menor = m[i][j];
posicao = i;
}
else
{
if(m[i][j] < menor)
{
menor = m[i][j];
posicao = i;
}
}
}
}
for (i = posicao;;)
{
for(j = 0; j < 5; j++)
{
if(j == 0)
{
maior = m[i][j];
}
else
{
if(m[i][j] > maior)
{
maior = m[i][j];
}
}
}
}
System.out.println("\n\nThe smallest element of the array: " + menor);
System.out.println("The line of the smallest element: " + posicao);
System.out.printf("MINMAX element %d encountered at the position: [%d][%d]", maior, i, j);
}
The error is pointed by Eclipse in this line:
System.out.println("\n\nThe smallest element of the array: " + menor);
Looks like your for loop defined as for (i = posicao;;) above your prints never finishes, there is no exit case
I am trying to solve a uva problem "Greedy Gift Givers" (problem number 119, Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=55 ). I am using java to solve that problem and my code works fine while running it on my computer, but the problem is when I submit it to uva online judge it shows me RUNTIME ERROR. I did not get where the problem is. I also have less idea about RUNTIME ERROR. My code is given below
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int n = 0;
String name[];
int cost[];
while (inp.hasNextLine()) {
n = inp.nextInt();
name = new String[n];
cost = new int[n];
for (int i = 0; i < n; i++) {
name[i] = inp.next();
}
for (int p = 0; p < n; p++) {
String temp_name = inp.next();
for (int i = 0; i < n; i++) {
if (temp_name.equals(name[i])) {
int gift = inp.nextInt(), num = inp.nextInt();
if (num != 0 && gift >= 0) {
cost[i] -= (int) (gift / num) * (int) num;
}
for (int k = 0; k < num; k++) {
String temp_name_2 = inp.next();
for (int j = 0; j < n; j++) {
if (temp_name_2.equals(name[j])) {
if (num != 0 && gift >= 0) {
cost[j] += gift / num;
}
break;
}
}
}
break;
}
}
}
for (int i = 0; i < n; i++) {
System.out.println(name[i] + " " + cost[i]);
}
}
}
}
I think the error is that you use System.in while the online judge gives a file to the input of your program. The last string og the input section:
The input consists of one or more groups and is terminated by end-of-file.