How to load String by BufferedReader? - java

I need to load in a given String which will be type in as an input Stringļ¼š
20 6
....................
..XXXXX..XXX.XXX..X.
..X.X.X..X.XXX.X..X.
..XXXXX..XX.X..X....
..XX......XXXXXX..X.
....................
something like that. It contains 2 integers and a String with "." and "X"
Now I just want to ask 2 questions:
1)I need to load the 2 integers first,but how can get the first two integers by BufferedReader?(the 2 int is divide by space between each other and the rest)
2)Then after loading the two integers,how can I load the following rest string char by char?(Like everytime I need to just load one char,then I go to some function,then come back and load the next char;and between there is no blank space)
Here is part of my code:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
number1 = Integer.parseInt();
number2 = Integer.parseInt();
And now I don't know how to continue...Anyone can help me to load it?

For 1) you just need to do a split
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
String ints[] = str.split(" ");
number1 = Integer.parseInt( ints[0] );
number2 = Integer.parseInt( ints[1] );
Then for 2) once you have a String you can have it length and so get them char by char ;)
String lol = "......XXX..XX...";
for( int i = 0; i < lol.length(); i++ )
System.out.println(lol.charAt(i));
With this you will get all your string char by char

It is much better if you use Scanner then you can read int and the String easier.
I suppose you input two integers as col and row.
Example like this:
Scanner scan = new Scanner(System.in);
int col = scan.nextInt();
int row = scan.nextInt(); //input two int first
scan.nextLine();
for(int i = 0; i < row; i++) {
String s = scan.nextLine();
for(int j = 0; j < col; j++) {
char c = s.charAt(j);
//your code here
}
}

Related

Java Mult Array From File

I have been having problem trying to get the program to work. The first row in the array is set blank and it keep pushing out the last row in the array.
Scanner console = new Scanner(System.in);
System.out.println("please enter the file name");
String name = console.next();
Scanner input = new Scanner(new File(name));
int length = input.nextInt();
int lengt = input.nextInt();
char[][] array = new char[length][lengt];
for(int i = 0; i < length; i++) {
array[i] = input.nextLine().toCharArray();
}
for(int k = 0; k < array.length; k++){
for(int s = 0; s < array[k].length; s++) {
System.out.print(array[k][s]);
}
System.out.println();
}
input.close();
System.out.println();
Try the adding the line input.nextLine(); to your code like so:
int length = input.nextInt();
int lengt = input.nextInt();
input.nextLine();
char[][] array = new char[length][lengt];
I believe what is happening is you are reading the int's to get length and lengt, but you are leaving the rest of that line. When you call array[i] = input.nextLine().toCharArray(); the first time you are pushing the rest or the line with the length and lengt int's. Since that is counted as the first line in your for loop you are not iterating over the last line.

Java exception error for input string

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);
}
}

How to convert a string to a char?

I'm trying to take a string from a user input using a scanner, and evaluate each character and add 2 to its ASCII value, if I put input of abc, I would like it to output cde. I tried the code below, and got a cannot convert char to int error.
String inputString;
System.out.println("Input: ");
Scanner sc = new Scanner(System.in);
inputString = sc.nextLine();
sc.close();
int len=inputString.length();
char[] c = inputString.toCharArray();
for(int i = 0; i < len; i++)
{
c[i] +=2;
c = inputString.toCharArray();
}
System.out.println(c);
}
You cannot just write += 2 with an array (of any kind) and have Java change the whole array. You will have to do something like
for (int i = 0; i < len; i++) {
name[i] += 2;
}
Additionally, you are initializing every character in name to the first character of the input string, so it will just be {'a', 'a', 'a'}. You should either change the initialization to be name[i] = inputString.charAt(i), or just make name = inputString.toCharArray() to do it all in one go.
Finally, you cannot print arrays in Java like you're trying to do. You'll need to write something like System.out.println(String.valueOf(name)).

Why an empty space is stored in the first cell of Array?

Can some body explain why an empty space is stored in first cell of my String Array? I am trying to store each line inputted through console into a String array.
import java.util.Scanner;
public class ClassNameHere {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int numberOfLines = in.nextInt();
String[] lines = new String[numberOfLines];
for( int i = 0; i < numberOfLines; i++ ){
lines[i] = in.nextLine();
}
for( int i = 0; i < numberOfLines; i++ ){
System.out.println(lines[i]);
}
}
}
After you call in.nextInt(), the first call to in.nextLine() returns the rest of that same line, and therefore it's empty.
You should add a call to in.nextLine() before the loop to consume that empty line.
int numberOfLines = in.nextInt();
String[] lines = new String[numberOfLines];
in.nextLine(); // add this
for( int i = 0; i < numberOfLines; i++ ){
lines[i] = in.nextLine();
}
for( int i = 0; i < numberOfLines; i++ ){
System.out.println(lines[i]);
}
in.nextInt() reads only the int value not the newline character after that. So that newline will be read by your in.nextLine() so your first index is empty. To eliminate this problem add a dummy in.nextLine() after reading the int.
int numberOfLines = in.nextInt();
in.nextLine();
Another method is to use nextLine() instead of nextInt() to read the and parse the input to int.
int numberOfLines = Integer.parseInt(in.nextLine());
Below code will work for you.
int numberOfLines = in.nextInt();
in.nextLine(); //Add this.
in.nextInt(); reads the integer value and then you click enter which is read by in.nextLine();
The relevant code is
// this reads a number, not the new line after the number
int numberOfLines = in.nextInt();
// reads the rest of the first line you typed.
lines[i] = in.nextLine();
If you want to ignore the rest of the line after the number, add in.nextLine();
You can use nextLine() instead of int numberOfLines = in.nextInt();
I guess it's because method nextInt() reads only the number (3) and the newline that follows is picked up by the first call to nextLine().

How to multiply a String by an Integer?

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));

Categories

Resources