I have a simple Java program using java.util.Scanner as follows:
package com.company;
import java.io.IOException;
import java.util.Scanner;
public class Favorite_Number {
public static void main(String[] args) throws IOException {
int X,sum = 0,rem = 0,t;
Scanner s = new Scanner(System.in);
t = s.nextInt();
while(t!=0) {
s.reset(); // <-- what does it do?
X = s.nextInt();
while (X > 0) {
rem = X % 10;
if (rem == 5) {
sum++;
}
X = X / 10;
}
System.out.println(sum);
sum = 0;
t--;
}
}
}
What does s.reset() do? If I remove it, the program still works fine.
reset() is explained here with examples
and as per documents purpose stated as:
On resetting a scanner discards all of its explicit state information which may have been changed by invocations of useDelimiter(java.util.regex.Pattern), useLocale(java.util.Locale), or useRadix(int).
Docs reference:
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#reset--
Related
i'm trying to write a program that reads a file and then prints it out and then reads it again but only prints out the lines that begin with "The " the second time around. it DOES print out the contents of the file, but then it doesn't print out the lines that begin with "The " and i can't figure out why. it prints out the println line right before the loop, but then it ignores the for-loop completely. the only difference between my findThe method and my OutputTheArray method is the substring part, so i think that's the problem area but i don't know how to fix it.
import java.util.*;
import java.io.*;
public class EZD_readingFiles
{
public static int inputToArray(String fr[], Scanner sf)
{
int max = -1;
while(sf.hasNext())
{
max++;
fr[max] = sf.nextLine();
}
return max;
}
public static void findThe(String fr[], int max)
{
System.out.println("\nHere are the lines that begin with \"The\": \n");
for(int b = 0; b <= max; b++)
{
String s = fr[b].substring(0,4);
if(s.equals("The "))
{
System.out.println(fr[b]);
}
}
}
public static void OutputTheArray(String fr[], int max)
{
System.out.println("Here is the original file: \n");
for(int a = 0; a <= max; a++)
{
System.out.println(fr[a]);
}
}
public static void main(String args[]) throws IOException
{
Scanner sf = new Scanner(new File("EZD_readme.txt"));
String fr[] = new String[5];
int y = EZD_readingFiles.inputToArray(fr,sf);
EZD_readingFiles.OutputTheArray(fr,y);
int z = EZD_readingFiles.inputToArray(fr,sf);
EZD_readingFiles.findThe(fr,z);
sf.close();
}
}
this is my text file with the tester data (EZD_readme.txt):
Every man tries as hard as he can.
The best way is this way.
The schedule is very good.
Cosmo Kramer is a doofus.
The best movie was cancelled.
Try cloning sf and passing it to the other function.
Something like this:
Scanner sf = new Scanner(new File("EZD_readme.txt"));
Scanner sf1 = sf.clone();
int y = EZD_readingFiles.inputToArray(fr,sf);
EZD_readingFiles.OutputTheArray(fr,y);
int z = EZD_readingFiles.inputToArray(fr,sf1);
EZD_readingFiles.findThe(fr,z);
sf.close();
sf1.close();
First time writing something here.
Why does my Subroutine not work?
I am trying to open the subroutine in the main function to get a boolean.
import java.util.Scanner;
public class Aufgabe1 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int eingabe = 0;
int zahl = 0;
boolean primzahl = false;
eingabe = scan.nextInt();//Input 1
if (eingabe == 1) {
zahl = scan.nextInt(); //Input 2
unterprogramm1(zahl);
}
public static boolean unterprogramm1(boolean primzahl) {
for (int i = 0; i < zahl; i++) {
if (zahl % i == 0) {
primzahl = true;
}
}
return primzahl;
}
You should write boolean primzahl in the parameter list, instead of just primzahl and move the method outside of the main function.
Hi there I am super new to coding and I keep getting a '.class' error when I try to run the code below. What am I missing?
import java.util.Scanner;
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner (System.in);
userWeight = new int[5];
int i = 0;
userWeight[0] = 0;
userWeight[1] = 5;
userWeight[2] = 6;
userWeight[3] = 7;
userWeight[4] = 9;
System.out.println("Enter weight 1: ");
userWeight = scnr.nextInt[];
return;
}
}
This is the problem
userWeight = scnr.nextInt[];
Solve this by:
userWeight[0] = scnr.nextInt(); //If you intended to change the first weight
OR
userWeight[1] = scnr.nextInt(); //If you intended to change the value of userWeight at index 1 (ie. the second userWeight)
Should work
PS: As a precaution do not import the Scanner class twice. Doing it once would be enough
I understood your intension and below are two possible ways to implement your thought:
I see you are giving values manually as userWeight[0]=0;
If you wanna give manually I suggest not to go with scanner as below.
public static void main(String[] args) {
int[] userWeight={0, 5, 6,7,9};
System.out.println("Weights are" +userWeight);//as you are giving values.
}
If your intension is to get values at run time or from user, please follow below approach
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("This is runtime and you need to enter input");
int[] userWeight = new int[5];
for (int i= 0; i < userWeight.length; i++) {
userWeight[i] = sc.nextInt();
System.out.println(userWeight[i]);
}
}
PS:
I seen you are using util package import for two times, instead you may import all at once as import java.util.*;
Also you are trying to return. Please note for void methods its not need for return values. VOID excepts nothing in return.
First of all do not import packages more than once, now lets go to the actual "bugs".
Here:
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner (System.in);
int userWeight[] = new int[5];//You need to declare the type
//of a variable, in this case its int name[]
//because its an array of ints
int i = 0;
userWeight[0] = 0;
userWeight[1] = 5;
userWeight[2] = 6;
userWeight[3] = 7;
userWeight[4] = 9;
System.out.println("Enter weight 1: ");
userWeight[0] = scnr.nextInt();//I belive that you wanted to change
// the first element of the array here.
//Also nextInt() is a method you can't use nextInt[]
//since it doesn't exists
//return; You dont need it, because the method is void, thus it doesnt have to return anything.
}
}
Also instead of this:
userWeight[0] = 0;
userWeight[1] = 5;
userWeight[2] = 6;
userWeight[3] = 7;
userWeight[4] = 9;
you can do this during the declaration of an array:
int userWeight[] = {0,5,6,7,9};//instantiate it with 5 integers
Here is my code:
It is not working for greater numbers to find factorial using recursion
import java.util.*;
import java.lang.*;
class Main
{
static String fib(int f)
{
if(f!=1)
return ""+(f*(Integer.parseInt(fib(f-1))));
else
return "1";
}
public static void main(String[] args) throws java.lang.Exception
{
Scanner sc= new Scanner(System.in);
int f= sc.nextInt();
int a[]=new int[f];
int i;
for( i=0;i<f;i++)
a[i]=sc.nextInt();
for( i=0;i<f;i++)
System.out.println(fib(a[i]));
}
}
It's a problem of the range of the type integer (2147483647). If you just replace int with long (range 9223372036854775807) like this
import java.util.*;
class Main
{
static String fib(long f)
{
if(f!=1)
return ""+(f*(Long.parseLong(fib(f-1))));
else
return "1";
}
public static void main(String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int f = sc.nextInt();
long a[] = new long[f];
int i;
for(i = 0; i<f; i++)
a[i] = sc.nextLong();
for(i = 0; i<f; i++)
System.out.println(fib(a[i]));
}
}
then the program can already calculate everything under 21!. But if you use double then it should work with higher numbers, because double has a range of 1.7976931348623157E308. With double the max working factorial calculation is 170!.
PS: I'm German so if i wrote incorrect english i'm sorry
There is no error in the logic of your code. Since you use int, whose range is shorter than the factorial of numbers beyond 25, you get faulty output. Instead, use long or, more preferably, BigInteger.
NOTE: You have to import the java.math package that contains the BigInteger class to use this type.
static BigInteger fib(BigInteger f) {
if (!f.equals(BigInteger.ONE))
return f.multiply(fib(f.subtract(BigInteger.ONE)));
else
return BigInteger.ONE;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int f = sc.nextInt();
int a[] = new int[f];
int i;
for (i = 0; i < f; i++)
a[i] = sc.nextInt();
for (i = 0; i < f; i++)
System.out.println(fib(new BigInteger(String.valueOf(a[i]))));
}
}
So im trying to do cross-sums: 321=6 because 3+2+1=6 however ive run into a problem that i can locate with my code and was hoping if anyone else could help me to locate it
import java.util.*;
public class Thewierdo {
public static void main(String[] args) {
Scanner Tinput = new Scanner (System.in);
System.out.print("input number for cross-sum examination: ");
Double Tin = Tinput.nextDouble();
int m = 0;
if (Tin.isNaN()){
System.out.print("actual whole number please: ");
Tin = Tinput.nextDouble();
}else{
int cool = Tin.intValue();
String gotcha= String.valueOf(cool);
int heads = gotcha.length();
while(heads >= 0){
System.out.println(gotcha.charAt(1));
char Tails=gotcha.charAt(heads);
int finald = Character.getNumericValue(Tails);
heads=heads-1;
m += finald;
//made by Christian Risom
}
System.out.print(m);
}
}
}
That's my solution, i'm sure you could make it more simple, but there you go:
public static int cross_sums(int input) {
int output = 0;
do {
output += input % 10;
input /= 10;
} while (input > 0);
if (output > 9) {
output = cross_sums(output);
}
return output;
}
The way you are checking if it is a valid number doesn't work. An error will be thrown on Tinput.nextDouble() if the input can't be interpreted as a double.
As others are saying, read your errors and debug. You likely have other problems as well.