The following block of code showing this error "Runtime Exception- feature not implemented yet" in jelliot, when it reaches the line
char arr [] = w.toCharArray();
And in other compilers,it won't take the number of input it's supposed to take. If I set n=4, it only takes 2 inputs.
Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
char arr [] = w.toCharArray();
if(arr.length > 4){
System.out.print(arr[0]);
System.out.print(arr.length-2);
System.out.print(arr[arr.length-1]);
}
else{
System.out.println(w);
}
System.out.println();
}
The exception indicates that this particular method (toCharArray()) of the String class isn't implemented for the (presumably non-standard) implementation of java that you're using. You can work around this by not using a char array, and instead using the String methods length() and charAt(). Try this:
Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
if(w.length() > 4){
System.out.print(w.charAt(0));
System.out.print(w.length()-2);
System.out.print(w.chatAt(w.length()-1));
}
else{
System.out.println(w);
}
System.out.println();
}
Related
I am trying to add the 1st and last digit of a no but after the programs gets to while loop it starts asking for more inputs,like an infinite loop .
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();//no of testcases
int n[]=new int[t];
int last=0,first = 0;
for(int i=0;i<t;i++){
n[i]=sc.nextInt();
sc.nextLine();
last=n[i]%10;
while(n[i]>=10){
first=n[i]/10;
}
System.out.println(first+last);
}
This is how I did it.
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String temp = t+"";
String[] arr = temp.split("");
int x = Integer.parseInt(arr[0]) + Integer.parseInt(arr[arr.length - 1]);
System.out.println(x);
}
That is because you are not updating the value of n[i] with every iteration.
This is my proposed solution for your while loop:
while(n[i]>=10){
n[i]=n[i]/10;
}
first = n[i]
In while loop you need to update the value of n[i] by using n[i]=n[i]/10; which is causing while loop to become infinite loop
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();//no of testcases
int n[]=new int[t];
int last=0,first = 0;
for(int i=0;i<t;i++){
n[i]=sc.nextInt();
sc.nextLine();
last=n[i]%10;
while(n[i]>=10){
n[i]=n[i]/10;
}
first=n[i]/10;
System.out.println(first+last);
}
I am a beginner in Java programming. I am trying to write a simple program to take size of input followed by list of numbers separated by spaces to compute the sum.
The first input is getting in fine for the second one system shows error as it is trying to parse a blank string into integer. Can you please help with the mistake I am making?
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(" Enter size of input ");
int num = scan.nextInt();
System.out.println("Enter data separated by spaces: ");
String line = scan.nextLine();
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}
The reason you get an exception in your code is because int num = scan.nextInt(); does not process the newline character after the number.
So when the statement String line = scan.nextLine(); is used, it processes the newline character and hence you get an empty string ""
You can either fetch the entire line and parse it to Integer, like this:
int num = Integer.parseInt(scan.nextLine());
or you can go with using nextInt() and then use a blank scan.nextLine() to process the new line after the number, like this:
int num = scan.nextInt();
scan.nextLine();
Your Program has only one error that you were making only one scan object of scanner class, you have to make two scanner class object one will help in getting array size while another will help in getting array element.
import java.util.Scanner;
public class InputStringforarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Scanner scan1 = new Scanner(System.in); // change 1
System.out.print(" Enter size of input ");
int num = scan.nextInt();`enter code here`
System.out.println("Enter data separated by spaces: ");
String line = scan1.nextLine();// change 2
String[] str = line.split(" ");
int[] A = new int[num];
int sum = 0;
for (int i = 0; i < num; i++)
A[i] =Integer.parseInt(str[i]);
for (int i = 0; i < num; i++)
sum = sum + A[i];
System.out.println("Sum is " + sum);
}
}
I need to read user inputs line by line (may be included spaces). I had already read Scanner doesn't see after space question. So I used with scanner.nextLine() but this method does not help my program. Below is my sample program ....
public class TestingScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many friends do you have ?");
int size = sc.nextInt();
String[] names = new String[size];
for (int i = 0; i < size; i++) {
System.out.println("Enter your name of friend" + (i + 1));
names[i] = sc.nextLine();
}
System.out.println("*****************************");
System.out.println("Your friends are : ");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
System.out.println("*****************************");
}
}
While runnig my program , after inserted no: of friend , my program produces two lines as below. How can I solve it ? Thanks.
After int size = sc.nextInt(); use sc.nextLine() to consume the newline characters, otherwise, they will be consumed by nextLine() in your loop and that is not what you want.
Solution
int size = sc.nextInt();
sc.nextLine();
I know there's some way to change a string into an integer but it's not really working out for me when I try to do it.
I was asked to take in an integer 'n' and 'a' string 's' and print 's' 'n' times
Here's my code and my main question / question is how do I easily turn the string into an integer so I can multiply the two together:
public static void main(String args[])
{
System.out.println("Enter the number of times you want to print a string");
Scanner n = new Scanner(System.in);
int x = n.nextInt();
System.out.println("Enter the string you want printed");
Scanner y = new Scanner(System.in);
String s = y.nextLine();
}
You only need one Scanner, and if I understand your question then you might use a loop like, also an int n.
System.out.println("Enter the number of times you want to "
+ "print a string");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the string you want printed");
String s = scan.nextLine();
for (int i = 0; i < n; i++) {
System.out.print(s);
}
System.out.println();
Of course, you could put the loop in a method like
private static String multiply(String str, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(str);
}
return sb.toString();
}
Then you could call it like,
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println("Enter the string you want printed");
String s = scan.nextLine();
System.out.println(multiply(s, n));
I was wondering how to load up an array (with user input) using a while loop. The code below prints a 0.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 0;
int n = 0;
int[] myArray = new int[10];
System.out.printf("enter a value>>");
while (scan.nextInt() > 0) {
for (i = 0; i > 0; i++) {
myArray[i] = scan.nextInt();
}
System.out.printf("enter a value>>");
}
System.out.printf("array index 2 is %d", myArray[2]);
}
There are multiple things wrong with your code:
First of all
while(scan.nextInt() > 0){
Scanner.nextInt() returns an int from your standard input so you actually have to pick up that value. You are checking here what the user typed but then not using it at all and storing the next thing that the user types by saying:
myArray[i] = scan.nextInt();
You don't really need the outer while loop, just use the for loop, its enough.
However, your for loop is off as well:
for(i = 0; i > 0; i++){
It starts at i equal to 0 and runs while i is greater than 0. This means it will never actually run the code within the loop because 0 is never greater than 0. And if it did run (you started it at some number < 0), you would end up in an infinite loop because your condition i > 0 is always true for positive numbers.
Change the loop to:
for(i = 0; i < 10; i++){
Now, your loop could look like:
for(i = 0; i < 10; i++){ // do this 10 times
System.out.printf("enter a value>>"); // print a statement to the screen
myArray[i] = scan.nextInt(); // read an integer from the user and store it into the array
}
one other way to do it
Scanner scan = new Scanner(System.in);
List list = new ArrayList();
while(true){
System.out.println("Enter a value to store in list");
list.add(scan.nextInt());
System.out.println("Enter more value y to continue or enter n to exit");
Scanner s = new Scanner(System.in);
String ans = s.nextLine();
if(ans.equals("n"))
break;
}
System.out.println(list);
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
int[] arr=new int[4];
int i;
for(i=0;i<4;i++)
{
System.out.println("Enter the number: ");
arr[i]=input.nextInt();
}
for(i=0;i<4;i++)
{
System.out.println(arr[i]);
}
}
Hope this code helps.